Add pubkeyBanPolicy, improve tests

This commit is contained in:
Alex Gleason 2023-03-26 15:37:24 -05:00
parent 184c013b2e
commit a8c55053b4
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
6 changed files with 74 additions and 16 deletions

View File

@ -5,6 +5,7 @@ import {
hellthreadPolicy, hellthreadPolicy,
noopPolicy, noopPolicy,
pipeline, pipeline,
pubkeyBanPolicy,
rateLimitPolicy, rateLimitPolicy,
readStdin, readStdin,
writeStdout, writeStdout,
@ -16,6 +17,7 @@ for await (const msg of readStdin()) {
[hellthreadPolicy, { limit: 100 }], [hellthreadPolicy, { limit: 100 }],
[antiDuplicationPolicy, { ttl: 60000, minLength: 50 }], [antiDuplicationPolicy, { ttl: 60000, minLength: 50 }],
[rateLimitPolicy, { whitelist: ['127.0.0.1'] }], [rateLimitPolicy, { whitelist: ['127.0.0.1'] }],
[pubkeyBanPolicy, ['e810fafa1e89cdf80cced8e013938e87e21b699b24c8570537be92aec4b12c18']],
]); ]);
writeStdout(result); writeStdout(result);

1
mod.ts
View File

@ -1,6 +1,7 @@
export { default as antiDuplicationPolicy } from './src/policies/anti-duplication-policy.ts'; export { default as antiDuplicationPolicy } from './src/policies/anti-duplication-policy.ts';
export { default as hellthreadPolicy } from './src/policies/hellthread-policy.ts'; export { default as hellthreadPolicy } from './src/policies/hellthread-policy.ts';
export { default as noopPolicy } from './src/policies/noop-policy.ts'; export { default as noopPolicy } from './src/policies/noop-policy.ts';
export { default as pubkeyBanPolicy } from './src/policies/pubkey-ban-policy.ts';
export { default as rateLimitPolicy } from './src/policies/rate-limit-policy.ts'; export { default as rateLimitPolicy } from './src/policies/rate-limit-policy.ts';
export { default as readOnlyPolicy } from './src/policies/read-only-policy.ts'; export { default as readOnlyPolicy } from './src/policies/read-only-policy.ts';

View File

@ -0,0 +1,15 @@
import { assert } from '../deps.ts';
import { buildEvent, buildInputMessage } from '../test.ts';
import pubkeyBanPolicy from './pubkey-ban-policy.ts';
Deno.test('blocks banned pubkeys', async () => {
const msgA = buildInputMessage({ event: buildEvent({ pubkey: 'A' }) });
const msgB = buildInputMessage({ event: buildEvent({ pubkey: 'B' }) });
const msgC = buildInputMessage({ event: buildEvent({ pubkey: 'C' }) });
assert((await pubkeyBanPolicy(msgA, [])).action === 'accept');
assert((await pubkeyBanPolicy(msgA, ['A'])).action === 'reject');
assert((await pubkeyBanPolicy(msgC, ['B', 'A'])).action === 'accept');
assert((await pubkeyBanPolicy(msgB, ['B', 'A'])).action === 'reject');
});

View File

@ -0,0 +1,22 @@
import { Policy } from '../types.ts';
/** Ban individual pubkeys from publishing events to the relay. */
const pubkeyPolicy: Policy<string[]> = ({ event: { id, pubkey } }, pubkeys = []) => {
const isMatch = pubkeys.includes(pubkey);
if (isMatch) {
return {
id,
action: 'reject',
msg: 'Pubkey is banned.',
};
}
return {
id,
action: 'accept',
msg: '',
};
};
export default pubkeyPolicy;

View File

@ -1,23 +1,10 @@
import { assert } from '../deps.ts'; import { assert } from '../deps.ts';
import { buildInputMessage } from '../test.ts';
import readOnlyPolicy from './read-only-policy.ts'; import readOnlyPolicy from './read-only-policy.ts';
Deno.test('always rejects', async () => { Deno.test('always rejects', async () => {
const result = await readOnlyPolicy({ const msg = buildInputMessage();
event: { const result = await readOnlyPolicy(msg);
kind: 1,
id: '',
content: '',
created_at: 0,
pubkey: '',
sig: '',
tags: [],
},
receivedAt: 0,
sourceInfo: '127.0.0.1',
sourceType: 'IP4',
type: 'new',
});
assert(result.action === 'reject'); assert(result.action === 'reject');
}); });

31
src/test.ts Normal file
View File

@ -0,0 +1,31 @@
import type { Event, InputMessage } from './types.ts';
/** Constructs a fake event for tests. */
function buildEvent(attrs: Partial<Event> = {}): Event {
const event: Event = {
kind: 1,
id: '',
content: '',
created_at: 0,
pubkey: '',
sig: '',
tags: [],
};
return Object.assign(event, attrs);
}
/** Constructs a fake strfry input message for tests. */
function buildInputMessage(attrs: Partial<InputMessage> = {}): InputMessage {
const msg = {
event: buildEvent(),
receivedAt: 0,
sourceInfo: '127.0.0.1',
sourceType: 'IP4',
type: 'new',
};
return Object.assign(msg, attrs);
}
export { buildEvent, buildInputMessage };