@chatwarden/core 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -2
- package/dist/src/audit.js +2 -2
- package/dist/src/audit.js.map +1 -1
- package/dist/src/chat.d.ts +48 -0
- package/dist/src/chat.d.ts.map +1 -1
- package/dist/src/chat.js +94 -10
- package/dist/src/chat.js.map +1 -1
- package/dist/src/chatwarden.d.ts +26 -1
- package/dist/src/chatwarden.d.ts.map +1 -1
- package/dist/src/chatwarden.js +12 -1
- package/dist/src/chatwarden.js.map +1 -1
- package/dist/src/crypto.d.ts +22 -0
- package/dist/src/crypto.d.ts.map +1 -0
- package/dist/src/crypto.js +193 -0
- package/dist/src/crypto.js.map +1 -0
- package/dist/src/decide.d.ts.map +1 -1
- package/dist/src/decide.js +2 -4
- package/dist/src/decide.js.map +1 -1
- package/dist/src/hasher.js +2 -2
- package/dist/src/hasher.js.map +1 -1
- package/dist/src/index.d.ts +2 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/log.d.ts +16 -0
- package/dist/src/log.d.ts.map +1 -1
- package/dist/src/log.js +34 -0
- package/dist/src/log.js.map +1 -1
- package/dist/src/notices.js +2 -2
- package/dist/src/notices.js.map +1 -1
- package/dist/src/reports.js +2 -2
- package/dist/src/reports.js.map +1 -1
- package/dist/src/sanctions.js +3 -3
- package/dist/src/sanctions.js.map +1 -1
- package/dist/src/strikes.js +2 -2
- package/dist/src/strikes.js.map +1 -1
- package/dist/src/subjects.js +2 -2
- package/dist/src/subjects.js.map +1 -1
- package/package.json +4 -1
- package/src/audit.ts +2 -2
- package/src/chat.ts +109 -9
- package/src/chatwarden.ts +34 -2
- package/src/crypto.ts +206 -0
- package/src/decide.ts +2 -4
- package/src/hasher.ts +2 -2
- package/src/index.ts +2 -2
- package/src/log.ts +37 -0
- package/src/notices.ts +2 -2
- package/src/reports.ts +2 -2
- package/src/sanctions.ts +3 -3
- package/src/strikes.ts +2 -2
- package/src/subjects.ts +2 -2
package/src/crypto.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// Isomorphic crypto for @chatwarden/core: the same code runs in Node and in a browser.
|
|
2
|
+
//
|
|
3
|
+
// Randomness comes from the Web Crypto global (`globalThis.crypto.getRandomValues`), present in Node >= 19 and in
|
|
4
|
+
// every browser. SHA-256 and HMAC-SHA-256 use the node crypto built-in when this runtime has it (looked up through
|
|
5
|
+
// `process.getBuiltinModule`, so there is never a static or dynamic `import` of a node built-in and a browser
|
|
6
|
+
// bundler has nothing to resolve), and otherwise a small pure-JS implementation. Both paths give byte-identical
|
|
7
|
+
// output: identity hashes (see hasher.ts) are stored by Moxgate and must never change.
|
|
8
|
+
//
|
|
9
|
+
// Nothing in this file is exported from the package index; it is for the engine's own modules.
|
|
10
|
+
|
|
11
|
+
/** The slice of the node crypto built-in this file uses. Declared here so no type import of it is needed either. */
|
|
12
|
+
interface NodeCrypto {
|
|
13
|
+
createHash(algorithm: 'sha256'): { update(data: string): { digest(encoding: 'hex'): string } };
|
|
14
|
+
createHmac(algorithm: 'sha256', key: string): { update(data: string): { digest(encoding: 'hex'): string } };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let forcePure = false;
|
|
18
|
+
/** `undefined`: not looked up yet. `null`: looked up, this runtime has no usable node crypto. */
|
|
19
|
+
let nativeCache: NodeCrypto | null | undefined;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Test seam: `true` makes every hash use the pure-JS code even where the built-in exists, `false` goes back to
|
|
23
|
+
* detection. Either call also forgets the cached lookup.
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
export function __forcePureJs(force: boolean): void {
|
|
27
|
+
forcePure = force;
|
|
28
|
+
nativeCache = undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function native(): NodeCrypto | null {
|
|
32
|
+
if (forcePure) return null;
|
|
33
|
+
if (nativeCache === undefined) {
|
|
34
|
+
nativeCache = null;
|
|
35
|
+
try {
|
|
36
|
+
const proc = (globalThis as { process?: { getBuiltinModule?: (id: string) => unknown } }).process;
|
|
37
|
+
const mod = proc?.getBuiltinModule?.('node:crypto') as Partial<NodeCrypto> | undefined;
|
|
38
|
+
if (mod && typeof mod.createHash === 'function' && typeof mod.createHmac === 'function') {
|
|
39
|
+
nativeCache = mod as NodeCrypto;
|
|
40
|
+
}
|
|
41
|
+
} catch {
|
|
42
|
+
// No process, no getBuiltinModule, or it refused: use the pure-JS path.
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return nativeCache;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
49
|
+
// Randomness
|
|
50
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
const B64URL = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
53
|
+
/** `getRandomValues` refuses more than 65536 bytes per call. */
|
|
54
|
+
const MAX_FILL = 65_536;
|
|
55
|
+
|
|
56
|
+
/** The one Web Crypto method used. Declared here because the build has no DOM lib. */
|
|
57
|
+
interface WebCrypto {
|
|
58
|
+
getRandomValues(array: Uint8Array | Uint32Array): unknown;
|
|
59
|
+
}
|
|
60
|
+
function webCrypto(): WebCrypto {
|
|
61
|
+
const c = (globalThis as { crypto?: WebCrypto }).crypto;
|
|
62
|
+
if (!c || typeof c.getRandomValues !== 'function') throw new Error('chatwarden: no secure random source (globalThis.crypto)');
|
|
63
|
+
return c;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** `bytes` random bytes as unpadded base64url: 16 bytes give 22 characters, like `randomBytes(16).toString('base64url')`. */
|
|
67
|
+
export function randomId(bytes = 16): string {
|
|
68
|
+
if (!Number.isInteger(bytes) || bytes < 0) throw new RangeError('randomId: bytes must be a non-negative integer');
|
|
69
|
+
const raw = new Uint8Array(bytes);
|
|
70
|
+
for (let off = 0; off < bytes; off += MAX_FILL) webCrypto().getRandomValues(raw.subarray(off, Math.min(bytes, off + MAX_FILL)));
|
|
71
|
+
let out = '';
|
|
72
|
+
let i = 0;
|
|
73
|
+
for (; i + 2 < bytes; i += 3) {
|
|
74
|
+
const n = (raw[i]! << 16) | (raw[i + 1]! << 8) | raw[i + 2]!;
|
|
75
|
+
out += B64URL[n >> 18]! + B64URL[(n >> 12) & 63]! + B64URL[(n >> 6) & 63]! + B64URL[n & 63]!;
|
|
76
|
+
}
|
|
77
|
+
if (bytes - i === 1) {
|
|
78
|
+
const n = raw[i]! << 16;
|
|
79
|
+
out += B64URL[n >> 18]! + B64URL[(n >> 12) & 63]!;
|
|
80
|
+
} else if (bytes - i === 2) {
|
|
81
|
+
const n = (raw[i]! << 16) | (raw[i + 1]! << 8);
|
|
82
|
+
out += B64URL[n >> 18]! + B64URL[(n >> 12) & 63]! + B64URL[(n >> 6) & 63]!;
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const TWO_32 = 4_294_967_296;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A uniform integer in `[0, n)` for `1 <= n <= 2**32`. Rejection sampling over 32-bit draws: a draw in the uneven
|
|
91
|
+
* tail (`>= floor(2**32 / n) * n`) is thrown away, so there is no modulo bias.
|
|
92
|
+
*/
|
|
93
|
+
export function randomIntBelow(n: number): number {
|
|
94
|
+
if (!Number.isInteger(n) || n < 1 || n > TWO_32) throw new RangeError('randomIntBelow: n must be an integer in [1, 2**32]');
|
|
95
|
+
const limit = Math.floor(TWO_32 / n) * n;
|
|
96
|
+
const draw = new Uint32Array(1);
|
|
97
|
+
for (;;) {
|
|
98
|
+
webCrypto().getRandomValues(draw);
|
|
99
|
+
const v = draw[0]!;
|
|
100
|
+
if (v < limit) return v % n;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
105
|
+
// SHA-256 and HMAC-SHA-256, pure JS (FIPS 180-4, RFC 2104)
|
|
106
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
107
|
+
|
|
108
|
+
/** Round constants: the first 32 bits of the fractional parts of the cube roots of the first 64 primes. */
|
|
109
|
+
// prettier-ignore
|
|
110
|
+
const K = new Uint32Array([
|
|
111
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
112
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
113
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
114
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
115
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
116
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
117
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
118
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
119
|
+
]);
|
|
120
|
+
|
|
121
|
+
/** Initial hash value: the first 32 bits of the fractional parts of the square roots of the first 8 primes. */
|
|
122
|
+
const H0 = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19] as const;
|
|
123
|
+
|
|
124
|
+
const rotr = (x: number, n: number): number => (x >>> n) | (x << (32 - n));
|
|
125
|
+
|
|
126
|
+
/** SHA-256 of `msg`: 32 bytes. */
|
|
127
|
+
export function __pureSha256(msg: Uint8Array): Uint8Array {
|
|
128
|
+
const len = msg.length;
|
|
129
|
+
// Pad: 0x80, zeros up to 56 mod 64, then the bit length as a 64-bit big-endian integer.
|
|
130
|
+
const total = Math.ceil((len + 9) / 64) * 64;
|
|
131
|
+
const buf = new Uint8Array(total);
|
|
132
|
+
buf.set(msg);
|
|
133
|
+
buf[len] = 0x80;
|
|
134
|
+
const view = new DataView(buf.buffer);
|
|
135
|
+
view.setUint32(total - 8, Math.floor((len * 8) / TWO_32));
|
|
136
|
+
view.setUint32(total - 4, (len * 8) >>> 0);
|
|
137
|
+
|
|
138
|
+
const h = Uint32Array.from(H0);
|
|
139
|
+
const w = new Uint32Array(64);
|
|
140
|
+
for (let off = 0; off < total; off += 64) {
|
|
141
|
+
for (let i = 0; i < 16; i++) w[i] = view.getUint32(off + i * 4);
|
|
142
|
+
for (let i = 16; i < 64; i++) {
|
|
143
|
+
const w15 = w[i - 15]!;
|
|
144
|
+
const w2 = w[i - 2]!;
|
|
145
|
+
const s0 = rotr(w15, 7) ^ rotr(w15, 18) ^ (w15 >>> 3);
|
|
146
|
+
const s1 = rotr(w2, 17) ^ rotr(w2, 19) ^ (w2 >>> 10);
|
|
147
|
+
w[i] = w[i - 16]! + s0 + w[i - 7]! + s1; // Uint32Array stores modulo 2**32
|
|
148
|
+
}
|
|
149
|
+
let a = h[0]!, b = h[1]!, c = h[2]!, d = h[3]!, e = h[4]!, f = h[5]!, g = h[6]!, hh = h[7]!;
|
|
150
|
+
for (let i = 0; i < 64; i++) {
|
|
151
|
+
const t1 = (hh + (rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)) + ((e & f) ^ (~e & g)) + K[i]! + w[i]!) | 0;
|
|
152
|
+
const t2 = ((rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) | 0;
|
|
153
|
+
hh = g;
|
|
154
|
+
g = f;
|
|
155
|
+
f = e;
|
|
156
|
+
e = (d + t1) | 0;
|
|
157
|
+
d = c;
|
|
158
|
+
c = b;
|
|
159
|
+
b = a;
|
|
160
|
+
a = (t1 + t2) | 0;
|
|
161
|
+
}
|
|
162
|
+
h[0] = h[0]! + a; h[1] = h[1]! + b; h[2] = h[2]! + c; h[3] = h[3]! + d;
|
|
163
|
+
h[4] = h[4]! + e; h[5] = h[5]! + f; h[6] = h[6]! + g; h[7] = h[7]! + hh;
|
|
164
|
+
}
|
|
165
|
+
const out = new Uint8Array(32);
|
|
166
|
+
const ov = new DataView(out.buffer);
|
|
167
|
+
for (let i = 0; i < 8; i++) ov.setUint32(i * 4, h[i]!);
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const BLOCK = 64;
|
|
172
|
+
|
|
173
|
+
/** HMAC-SHA-256 (RFC 2104): a key longer than the 64-byte block is hashed first, a shorter one is zero-padded. */
|
|
174
|
+
export function __pureHmacSha256(key: Uint8Array, data: Uint8Array): Uint8Array {
|
|
175
|
+
const k = new Uint8Array(BLOCK);
|
|
176
|
+
k.set(key.length > BLOCK ? __pureSha256(key) : key);
|
|
177
|
+
const inner = new Uint8Array(BLOCK + data.length);
|
|
178
|
+
const outer = new Uint8Array(BLOCK + 32);
|
|
179
|
+
for (let i = 0; i < BLOCK; i++) {
|
|
180
|
+
inner[i] = k[i]! ^ 0x36;
|
|
181
|
+
outer[i] = k[i]! ^ 0x5c;
|
|
182
|
+
}
|
|
183
|
+
inner.set(data, BLOCK);
|
|
184
|
+
outer.set(__pureSha256(inner), BLOCK);
|
|
185
|
+
return __pureSha256(outer);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const utf8 = new TextEncoder();
|
|
189
|
+
|
|
190
|
+
function toHex(bytes: Uint8Array): string {
|
|
191
|
+
let s = '';
|
|
192
|
+
for (let i = 0; i < bytes.length; i++) s += bytes[i]!.toString(16).padStart(2, '0');
|
|
193
|
+
return s;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** SHA-256 of the UTF-8 text, as 64 lowercase hex characters. */
|
|
197
|
+
export function sha256Hex(data: string): string {
|
|
198
|
+
const nc = native();
|
|
199
|
+
return nc ? nc.createHash('sha256').update(data).digest('hex') : toHex(__pureSha256(utf8.encode(data)));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** HMAC-SHA-256 of the UTF-8 text under the UTF-8 key, as 64 lowercase hex characters. */
|
|
203
|
+
export function hmacSha256Hex(key: string, data: string): string {
|
|
204
|
+
const nc = native();
|
|
205
|
+
return nc ? nc.createHmac('sha256', key).update(data).digest('hex') : toHex(__pureHmacSha256(utf8.encode(key), utf8.encode(data)));
|
|
206
|
+
}
|
package/src/decide.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { sha256Hex } from './crypto.js';
|
|
2
2
|
import { safeWarn } from './log.js';
|
|
3
3
|
import { ownLines } from './notices.js';
|
|
4
4
|
import type { NoticeService } from './notices.js';
|
|
@@ -269,9 +269,7 @@ function keyOf(n: Normal): string {
|
|
|
269
269
|
includeIp: a.includeIp,
|
|
270
270
|
lineIds: a.lineIds,
|
|
271
271
|
}));
|
|
272
|
-
return
|
|
273
|
-
.update(`${n.reportId}\u0000${n.verdict}\u0000${canonicalJSON(actions)}`)
|
|
274
|
-
.digest('hex');
|
|
272
|
+
return sha256Hex(`${n.reportId}\u0000${n.verdict}\u0000${canonicalJSON(actions)}`);
|
|
275
273
|
}
|
|
276
274
|
|
|
277
275
|
/**
|
package/src/hasher.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// byte-compatible with Moxgate's identity hasher. The internal kind strings
|
|
3
3
|
// 'playerKey' and 'ipHash' are Moxgate's and must not change, or existing
|
|
4
4
|
// hashes stop matching.
|
|
5
|
-
import {
|
|
5
|
+
import { hmacSha256Hex } from './crypto.js';
|
|
6
6
|
import type { Hasher, Identifiers, RawWho } from './types.js';
|
|
7
7
|
|
|
8
8
|
// 128 bits of the HMAC: plenty to avoid collisions, short enough to index.
|
|
@@ -14,7 +14,7 @@ export function makeHasher(secret: string): Hasher {
|
|
|
14
14
|
// The kind prefix keeps the two spaces apart: a client key that happened to
|
|
15
15
|
// equal an IP string must not produce the same value in both columns.
|
|
16
16
|
const mac = (kind: string, input: string): string =>
|
|
17
|
-
|
|
17
|
+
hmacSha256Hex(secret, `${kind}:${input}`).slice(0, HASH_HEX_LEN);
|
|
18
18
|
return {
|
|
19
19
|
clientKey: (raw) => mac('playerKey', raw),
|
|
20
20
|
ip: (raw) => mac('ipHash', raw),
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const VERSION = '0.1.
|
|
1
|
+
export const VERSION = '0.1.1';
|
|
2
2
|
|
|
3
3
|
export type {
|
|
4
4
|
Clock,
|
|
@@ -70,7 +70,7 @@ export type { StrikeDecision } from './strikes.js';
|
|
|
70
70
|
export { slidingWindow } from './rateLimit.js';
|
|
71
71
|
export type { SlidingWindow } from './rateLimit.js';
|
|
72
72
|
export { ChatService } from './chat.js';
|
|
73
|
-
export type { ChatResult, ChatServiceDeps, RefuseReason } from './chat.js';
|
|
73
|
+
export type { ChatResult, ChatServiceDeps, RefuseReason, RestrictedAnswer } from './chat.js';
|
|
74
74
|
export {
|
|
75
75
|
REPORT_CATEGORIES,
|
|
76
76
|
REPORT_NOTE_MAX,
|
package/src/log.ts
CHANGED
|
@@ -39,3 +39,40 @@ export function errorClass(err: unknown): string {
|
|
|
39
39
|
return 'unknown';
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A warning for a path that can fail on every call at high frequency (a typing indicator during a store outage): the
|
|
45
|
+
* first failure is logged, then at most one more per `intervalMs`, which carries `suppressed`, the number of failures
|
|
46
|
+
* held back since the last line. The clock is the kit's, so a test or a replay controls it.
|
|
47
|
+
*/
|
|
48
|
+
export class ThrottledWarn {
|
|
49
|
+
private last: number | null = null;
|
|
50
|
+
private held = 0;
|
|
51
|
+
|
|
52
|
+
private readonly logger: Logger | undefined;
|
|
53
|
+
private readonly clock: { now(): number };
|
|
54
|
+
private readonly intervalMs: number;
|
|
55
|
+
|
|
56
|
+
constructor(logger: Logger | undefined, clock: { now(): number }, intervalMs = 60_000) {
|
|
57
|
+
this.logger = logger;
|
|
58
|
+
this.clock = clock;
|
|
59
|
+
this.intervalMs = intervalMs;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
warn(event: string, detail: Detail): void {
|
|
63
|
+
let now: number;
|
|
64
|
+
try {
|
|
65
|
+
now = this.clock.now();
|
|
66
|
+
} catch {
|
|
67
|
+
now = 0;
|
|
68
|
+
}
|
|
69
|
+
if (this.last !== null && now - this.last < this.intervalMs && now >= this.last) {
|
|
70
|
+
this.held++;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const out: Detail = this.held > 0 ? { ...detail, suppressed: this.held } : detail;
|
|
74
|
+
this.last = now;
|
|
75
|
+
this.held = 0;
|
|
76
|
+
safeWarn(this.logger, event, out);
|
|
77
|
+
}
|
|
78
|
+
}
|
package/src/notices.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomId } from './crypto.js';
|
|
2
2
|
import { safeWarn } from './log.js';
|
|
3
3
|
import { maskSpans } from './mask.js';
|
|
4
4
|
import { parseRuleRef } from './rules.js';
|
|
@@ -163,7 +163,7 @@ export interface NoticeServiceDeps {
|
|
|
163
163
|
notify?: (subject: SubjectId, event: { type: 'notice_pending'; count: number }) => unknown;
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
-
const newId = (): string =>
|
|
166
|
+
const newId = (): string => randomId(16);
|
|
167
167
|
|
|
168
168
|
const errName = (err: unknown): string => {
|
|
169
169
|
try {
|
package/src/reports.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomId } from './crypto.js';
|
|
2
2
|
import type { EvidenceBuffer, RoomRoster } from './evidence.js';
|
|
3
3
|
import type { Limits } from './limits.js';
|
|
4
4
|
import { safeWarn } from './log.js';
|
|
@@ -92,7 +92,7 @@ export interface ReportServiceDeps {
|
|
|
92
92
|
limits?: Limits['reports'];
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
const newId = (): string =>
|
|
95
|
+
const newId = (): string => randomId(16);
|
|
96
96
|
|
|
97
97
|
const errName = (err: unknown): string => (err instanceof Error ? err.name : typeof err);
|
|
98
98
|
|
package/src/sanctions.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomId, randomIntBelow } from './crypto.js';
|
|
2
2
|
import type { Identifiers, SanctionKind, SanctionRow, Source, Store, SubjectId } from './types.js';
|
|
3
3
|
import type { RuleRef } from './rules.js';
|
|
4
4
|
import type { SubjectIndex } from './subjects.js';
|
|
@@ -99,12 +99,12 @@ export interface ActiveSanction {
|
|
|
99
99
|
own: boolean;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
const newId = (): string =>
|
|
102
|
+
const newId = (): string => randomId(16);
|
|
103
103
|
|
|
104
104
|
/** `M-` plus 5 characters from an unambiguous alphabet. Uniqueness is enforced at write time, not here. */
|
|
105
105
|
export function refCode(): string {
|
|
106
106
|
let out = 'M-';
|
|
107
|
-
for (let i = 0; i < 5; i++) out += REF_ALPHABET[
|
|
107
|
+
for (let i = 0; i < 5; i++) out += REF_ALPHABET[randomIntBelow(REF_ALPHABET.length)];
|
|
108
108
|
return out;
|
|
109
109
|
}
|
|
110
110
|
|
package/src/strikes.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomId } from './crypto.js';
|
|
2
2
|
import type { Limits } from './limits.js';
|
|
3
3
|
import { safeWarn } from './log.js';
|
|
4
4
|
import type { SubjectIndex } from './subjects.js';
|
|
@@ -26,7 +26,7 @@ export const REPEAT_RESTRICT_MS = 24 * 3_600_000;
|
|
|
26
26
|
/** An earlier automatic chat restriction that started within this long (7 days) makes the next one longer. */
|
|
27
27
|
export const REPEAT_LOOKBACK_MS = 7 * 24 * 3_600_000;
|
|
28
28
|
|
|
29
|
-
const newId = (): string =>
|
|
29
|
+
const newId = (): string => randomId(16);
|
|
30
30
|
|
|
31
31
|
const present = (id: SubjectId | null): id is SubjectId => typeof id === 'string' && id !== '';
|
|
32
32
|
|
package/src/subjects.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomId } from './crypto.js';
|
|
2
2
|
import { safeWarn } from './log.js';
|
|
3
3
|
import type {
|
|
4
4
|
Clock,
|
|
@@ -34,7 +34,7 @@ function rowKey(subjectId: SubjectId, kind: SubjectIdKind, value: string): strin
|
|
|
34
34
|
|
|
35
35
|
function newSubjectId(): SubjectId {
|
|
36
36
|
// 16 random bytes -> 22 base64url characters. Never derived from an identifier.
|
|
37
|
-
return `s_${
|
|
37
|
+
return `s_${randomId(16)}`;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
function emptyOwned(): Owned {
|