Skip to content

Commit

Permalink
Merge pull request #40 from hyphacoop/validate-blocklist
Browse files Browse the repository at this point in the history
Add validation to blocklists, test that adding doesn't break stuff
  • Loading branch information
RangerMauve authored Feb 9, 2024
2 parents 0567efd + 1a1be8b commit 893a428
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/server/api/blockallowlist.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { APIConfig, FastifyTypebox } from '.'
import { Type } from '@sinclair/typebox'

import createError from 'http-errors'

import Store from '../store/index.js'
import ActivityPubSystem from '../apsystem.js'

export function validateBlocklistFormat (mention: string): void {
const sections = mention.split('@')
const correctSections = sections.length === 3
const startsEmpty = sections[0] === ''
const hasUserAndDomain = (sections[1] !== '') && (sections[2] !== '')
const isValid = correctSections && hasUserAndDomain && startsEmpty
if (!isValid) {
throw createError(400, `Invalid account format: ${mention}. Please use the syntax @username@domain`)
}
}

export const blockAllowListRoutes = (cfg: APIConfig, store: Store, apsystem: ActivityPubSystem) => async (server: FastifyTypebox): Promise<void> => {
// Get global list of blocked users/instances as newline delimited string
server.get('/blocklist', {
Expand Down Expand Up @@ -45,7 +58,8 @@ export const blockAllowListRoutes = (cfg: APIConfig, store: Store, apsystem: Act
return await reply.code(403).send('Not Allowed')
}

const accounts = request.body.split('\n')
const accounts = request.body.trim().split('\n')
accounts.map(validateBlocklistFormat)
await store.blocklist.add(accounts)
return await reply.send({ message: 'Added successfully' })
})
Expand All @@ -70,7 +84,8 @@ export const blockAllowListRoutes = (cfg: APIConfig, store: Store, apsystem: Act
return await reply.code(403).send('Not Allowed')
}

const accounts = request.body.split('\n')
const accounts = request.body.trim().split('\n')
accounts.map(validateBlocklistFormat)
await store.blocklist.remove(accounts)
return await reply.send({ message: 'Removed successfully' })
})
Expand Down Expand Up @@ -115,7 +130,8 @@ export const blockAllowListRoutes = (cfg: APIConfig, store: Store, apsystem: Act
return await reply.code(403).send('Not Allowed')
}

const accounts = request.body.split('\n')
const accounts = request.body.trim().split('\n')
accounts.map(validateBlocklistFormat)
await store.allowlist.add(accounts)
return await reply.send({ message: 'Added successfully' })
})
Expand All @@ -140,7 +156,8 @@ export const blockAllowListRoutes = (cfg: APIConfig, store: Store, apsystem: Act
return await reply.code(403).send('Not Allowed')
}

const accounts = request.body.split('\n')
const accounts = request.body.trim().split('\n')
accounts.map(validateBlocklistFormat)
await store.allowlist.remove(accounts)
return await reply.send({ message: 'Removed successfully' })
})
Expand Down Expand Up @@ -200,7 +217,8 @@ export const blockAllowListRoutes = (cfg: APIConfig, store: Store, apsystem: Act
return await reply.code(403).send('Not Allowed')
}

const accounts = request.body.split('\n')
const accounts = request.body.trim().split('\n')
accounts.map(validateBlocklistFormat)
await store.forActor(actor).blocklist.add(accounts)
return await reply.send('Added successfully')
})
Expand Down Expand Up @@ -232,7 +250,8 @@ export const blockAllowListRoutes = (cfg: APIConfig, store: Store, apsystem: Act
return await reply.code(403).send('Not Allowed')
}

const accounts = request.body.split('\n')
const accounts = request.body.trim().split('\n')
accounts.map(validateBlocklistFormat)
await store.forActor(actor).blocklist.remove(accounts)
return await reply.send('Removed successfully')
})
Expand Down Expand Up @@ -292,7 +311,8 @@ export const blockAllowListRoutes = (cfg: APIConfig, store: Store, apsystem: Act
return await reply.code(403).send('Not Allowed')
}

const accounts = request.body.split('\n')
const accounts = request.body.trim().split('\n')
accounts.map(validateBlocklistFormat)
await store.forActor(actor).allowlist.add(accounts)
return await reply.send({ message: 'Added successfully' })
})
Expand Down Expand Up @@ -324,7 +344,8 @@ export const blockAllowListRoutes = (cfg: APIConfig, store: Store, apsystem: Act
return await reply.code(403).send('Not Allowed')
}

const accounts = request.body.split('\n')
const accounts = request.body.trim().split('\n')
accounts.map(validateBlocklistFormat)
await store.forActor(actor).allowlist.remove(accounts)
return await reply.send({ message: 'Removed successfully' })
})
Expand Down
11 changes: 11 additions & 0 deletions src/server/store/AccountListStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ test('AccountListStore - list patterns', async t => {
t.deepEqual(accounts, ['@[email protected]'], 'Only @[email protected] should remain after removal of @[email protected]')
})

test("AccountListStore - adding doesn't replace existing", async t => {
const store = newAccountListStore()
const patterns = ['@[email protected]', '@[email protected]']
const otherPatterns = ['@[email protected]', '@[email protected]']
await store.add(patterns)
await store.add(otherPatterns)

const accounts = await store.list()
t.deepEqual(accounts.sort(), patterns.concat(otherPatterns).sort(), 'All patterns should be listed after addition')
})

test('AccountListStore - match all wildcard', async t => {
const store = newAccountListStore()
const patterns = ['@*@*']
Expand Down

0 comments on commit 893a428

Please sign in to comment.