@steve31415/baselib 5.4.0 → 5.5.0
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/dist/auth.js +1 -1
- package/dist/connector.js +3 -3
- package/dist/http.js +4 -3
- package/dist/oauth-resource.d.ts +1 -1
- package/dist/oauth-resource.js +4 -3
- package/dist/s2s.js +6 -4
- package/dist/socket-age.js +1 -1
- package/dist/sync/http-events.d.ts +6 -4
- package/dist/sync/http-events.js +23 -4
- package/dist/sync/types.d.ts +7 -0
- package/package.json +1 -1
package/dist/auth.js
CHANGED
|
@@ -125,7 +125,7 @@ export function requireIdentity(opts) {
|
|
|
125
125
|
// working as designed (an s2s grant denial is ERROR because it usually
|
|
126
126
|
// means a missing grant during development).
|
|
127
127
|
const accessDenied = (c, user) => {
|
|
128
|
-
opts.logger?.warn('user access denied', { email: user.email, policy, path: c.req.path });
|
|
128
|
+
opts.logger?.warn('user access denied', { email: user.email, policy, path: truncate(c.req.path, 200) });
|
|
129
129
|
c.header('cache-control', 'no-store');
|
|
130
130
|
if (isApi(c)) {
|
|
131
131
|
return c.json({ error: 'not enabled for this account', code: ACCESS_DENIED_CODE, email: user.email }, 403);
|
package/dist/connector.js
CHANGED
|
@@ -21,7 +21,7 @@ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/
|
|
|
21
21
|
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
|
|
22
22
|
import { createMiddleware } from 'hono/factory';
|
|
23
23
|
import { ACCESS_DENIED_CODE, parseFullRole, userAllowed } from './auth.js';
|
|
24
|
-
import { serializeError } from './log-core.js';
|
|
24
|
+
import { serializeError, truncate } from './log-core.js';
|
|
25
25
|
import { cleanOutsideText, markOutsideText } from './outside-text.js';
|
|
26
26
|
import { insufficientScope } from './oauth-resource.js';
|
|
27
27
|
import { RateLimiter, resolveS2sCaller } from './s2s.js';
|
|
@@ -89,7 +89,7 @@ export function requireForwardedConnectorUser(opts) {
|
|
|
89
89
|
if (!user) {
|
|
90
90
|
opts.logger?.warn('connector identity headers missing or malformed', {
|
|
91
91
|
caller: resolved.caller.email,
|
|
92
|
-
path: c.req.path,
|
|
92
|
+
path: truncate(c.req.path, 200),
|
|
93
93
|
});
|
|
94
94
|
return c.json({ error: 'connector identity headers missing' }, 400);
|
|
95
95
|
}
|
|
@@ -97,7 +97,7 @@ export function requireForwardedConnectorUser(opts) {
|
|
|
97
97
|
opts.logger?.warn('connector user access denied', {
|
|
98
98
|
email: user.email,
|
|
99
99
|
policy: opts.userAccess,
|
|
100
|
-
path: c.req.path,
|
|
100
|
+
path: truncate(c.req.path, 200),
|
|
101
101
|
});
|
|
102
102
|
return c.json({ error: 'not enabled for this account', code: ACCESS_DENIED_CODE, email: user.email }, 403);
|
|
103
103
|
}
|
package/dist/http.js
CHANGED
|
@@ -21,7 +21,7 @@ export function requestLogging(logger, options = {}) {
|
|
|
21
21
|
await next();
|
|
22
22
|
const identity = c.var.identity;
|
|
23
23
|
const extra = Object.entries(options.meta?.(c) ?? {}).filter(([, value]) => value !== undefined);
|
|
24
|
-
logger.info(`${c.req.method} ${c.req.path} ${c.res.status}`, {
|
|
24
|
+
logger.info(`${c.req.method} ${truncate(c.req.path, 200)} ${c.res.status}`, {
|
|
25
25
|
method: c.req.method,
|
|
26
26
|
path: truncate(c.req.path, 200),
|
|
27
27
|
status: c.res.status,
|
|
@@ -63,10 +63,11 @@ export function addHealth(app) {
|
|
|
63
63
|
* has it). */
|
|
64
64
|
export function errorHandler(logger) {
|
|
65
65
|
return (err, c) => {
|
|
66
|
-
|
|
66
|
+
const path = truncate(c.req.path, 200);
|
|
67
|
+
logger.error(`unhandled error in ${c.req.method} ${path}`, {
|
|
67
68
|
error: err,
|
|
68
69
|
method: c.req.method,
|
|
69
|
-
path
|
|
70
|
+
path,
|
|
70
71
|
});
|
|
71
72
|
const wantsJson = c.req.path.startsWith('/api/') || (c.req.header('accept') ?? '').includes('application/json');
|
|
72
73
|
return wantsJson ? c.json({ error: 'internal error' }, 500) : c.text('Internal error', 500);
|
package/dist/oauth-resource.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Context } from 'hono';
|
|
2
2
|
import type { FullRole, UserAccessPolicy } from './auth.js';
|
|
3
|
-
import type
|
|
3
|
+
import { type Logger } from './log-core.js';
|
|
4
4
|
import { serviceFetch } from './s2s.js';
|
|
5
5
|
/** The exact shape of an access token: the prefix plus 32 random bytes in
|
|
6
6
|
* base64url. Anything else is refused before any cache or network work. */
|
package/dist/oauth-resource.js
CHANGED
|
@@ -31,6 +31,7 @@ import { createHash } from 'node:crypto';
|
|
|
31
31
|
import { createMiddleware } from 'hono/factory';
|
|
32
32
|
import { ACCESS_DENIED_CODE, DEFAULT_AUTH_URL, parseFullRole, userAllowed } from './auth.js';
|
|
33
33
|
import { authMode } from './config.js';
|
|
34
|
+
import { truncate } from './log-core.js';
|
|
34
35
|
import { forwardedClientIp } from './http.js';
|
|
35
36
|
import { RateLimiter, serviceFetch } from './s2s.js';
|
|
36
37
|
/** The exact shape of an access token: the prefix plus 32 random bytes in
|
|
@@ -150,13 +151,13 @@ export function requireConnectorUser(opts) {
|
|
|
150
151
|
opts.logger?.warn('connector user access denied', {
|
|
151
152
|
email: user.email,
|
|
152
153
|
policy,
|
|
153
|
-
path: c.req.path,
|
|
154
|
+
path: truncate(c.req.path, 200),
|
|
154
155
|
});
|
|
155
156
|
c.header('cache-control', 'no-store');
|
|
156
157
|
return c.json({ error: 'not enabled for this account', code: ACCESS_DENIED_CODE, email: user.email }, 403);
|
|
157
158
|
}
|
|
158
159
|
if (!limiter.allow(user.email)) {
|
|
159
|
-
opts.logger?.warn('connector rate limit exceeded', { email: user.email, path: c.req.path });
|
|
160
|
+
opts.logger?.warn('connector rate limit exceeded', { email: user.email, path: truncate(c.req.path, 200) });
|
|
160
161
|
return rateLimited(c);
|
|
161
162
|
}
|
|
162
163
|
// A copy per request: the cached identity is shared, and a Set typed
|
|
@@ -202,7 +203,7 @@ export function requireConnectorUser(opts) {
|
|
|
202
203
|
// concurrent junk cannot overshoot it. Over budget is a 429, never a 401:
|
|
203
204
|
// a client holding a good token must not be told to discard it.
|
|
204
205
|
if (!sourceBudget.allow(forwardedClientIp(c, resourceHost))) {
|
|
205
|
-
opts.logger?.warn('connector introspection budget exhausted', { path: c.req.path });
|
|
206
|
+
opts.logger?.warn('connector introspection budget exhausted', { path: truncate(c.req.path, 200) });
|
|
206
207
|
return rateLimited(c);
|
|
207
208
|
}
|
|
208
209
|
let res;
|
package/dist/s2s.js
CHANGED
|
@@ -116,6 +116,8 @@ export function redactToken(message, token) {
|
|
|
116
116
|
* Shared by s2sAuth() and auth.ts's requireIdentity(). */
|
|
117
117
|
export async function resolveS2sCaller(opts, headers, method, path) {
|
|
118
118
|
let email;
|
|
119
|
+
// The request's path is the caller's text: bounded in every log line.
|
|
120
|
+
const logPath = truncate(path, 200);
|
|
119
121
|
if (authMode() === 'test') {
|
|
120
122
|
email = headers.testCaller;
|
|
121
123
|
if (!email)
|
|
@@ -128,7 +130,7 @@ export async function resolveS2sCaller(opts, headers, method, path) {
|
|
|
128
130
|
if (CONNECTOR_TOKEN.test(m[1])) {
|
|
129
131
|
// A connector token on a route that takes service callers: refused here
|
|
130
132
|
// so it never reaches the verifier, whose errors quote the token.
|
|
131
|
-
opts.logger?.warn('connector token presented on a service route', { path });
|
|
133
|
+
opts.logger?.warn('connector token presented on a service route', { path: logPath });
|
|
132
134
|
return { failure: { status: 401, error: 'invalid token' } };
|
|
133
135
|
}
|
|
134
136
|
try {
|
|
@@ -147,7 +149,7 @@ export async function resolveS2sCaller(opts, headers, method, path) {
|
|
|
147
149
|
// redacted message only (PW LESSONS 2026-09-02 — log the shape, never
|
|
148
150
|
// the credential). Found by Mirror's device-key design review.
|
|
149
151
|
opts.logger?.warn('s2s token verification failed', {
|
|
150
|
-
path,
|
|
152
|
+
path: logPath,
|
|
151
153
|
reason: redactToken(err instanceof Error ? err.message : String(err), m[1]),
|
|
152
154
|
tokenLength: m[1].length,
|
|
153
155
|
tokenHashPrefix: createHash('sha256').update(m[1]).digest('hex').slice(0, 8),
|
|
@@ -156,13 +158,13 @@ export async function resolveS2sCaller(opts, headers, method, path) {
|
|
|
156
158
|
}
|
|
157
159
|
}
|
|
158
160
|
if (!opts.limiter.allow(email)) {
|
|
159
|
-
opts.logger?.warn('s2s rate limit exceeded', { caller: email, path });
|
|
161
|
+
opts.logger?.warn('s2s rate limit exceeded', { caller: email, path: logPath });
|
|
160
162
|
return { failure: { status: 429, error: 'rate limit exceeded' } };
|
|
161
163
|
}
|
|
162
164
|
if (!grantAllows(opts.grants, email, method, path)) {
|
|
163
165
|
// ERROR by policy: a denied identity is either an attack or a missing
|
|
164
166
|
// grant during development — both deserve attention.
|
|
165
|
-
opts.logger?.error('s2s call denied by grants', { caller: email, method, path });
|
|
167
|
+
opts.logger?.error('s2s call denied by grants', { caller: email, method, path: logPath });
|
|
166
168
|
return { failure: { status: 403, error: `${email} is not granted ${method} ${path}` } };
|
|
167
169
|
}
|
|
168
170
|
return { caller: { type: 's2s', email } };
|
package/dist/socket-age.js
CHANGED
|
@@ -57,7 +57,7 @@ function withAgeCap(events, path, maxAgeMs, graceMs, logger) {
|
|
|
57
57
|
if (cutAt === null || logged)
|
|
58
58
|
return;
|
|
59
59
|
logged = true;
|
|
60
|
-
logger.info('socket age cap', { path, ageMs: Math.round(cutAt - openedAt), terminated });
|
|
60
|
+
logger.info('socket age cap', { path: truncate(path, 200), ageMs: Math.round(cutAt - openedAt), terminated });
|
|
61
61
|
};
|
|
62
62
|
const cut = (ws) => {
|
|
63
63
|
ageTimer = null;
|
|
@@ -28,8 +28,10 @@ export interface SyncEventsRequestOptions<C extends EventBody, S extends EventBo
|
|
|
28
28
|
* (`applied` + committed events, `duplicate` + the stored event, `rejected`
|
|
29
29
|
* + error) and a contiguous catch-up from `lastSeq`, read after the commits:
|
|
30
30
|
* `events` + `currentSeq`, or `reload: true` when too far behind. Throws
|
|
31
|
-
* `SyncError('bad_request')` for a malformed batch (answer 400
|
|
32
|
-
* `
|
|
33
|
-
*
|
|
34
|
-
*
|
|
31
|
+
* `SyncError('bad_request')` for a malformed batch (answer 400; a
|
|
32
|
+
* `clientGuid` over 128 characters included). An event whose `eventGuid` is
|
|
33
|
+
* not a canonical UUID is `rejected` / `invalid_payload` without reaching
|
|
34
|
+
* the engine. Throws `SyncUnavailableError` when the database fails (answer
|
|
35
|
+
* 5xx; the client retries, and what committed before the failure comes
|
|
36
|
+
* back as duplicates). Events committed before such a failure still get `afterCommit`. */
|
|
35
37
|
export declare function handleSyncEventsRequest<C extends EventBody, S extends EventBody>(options: SyncEventsRequestOptions<C, S>): Promise<HttpEventsResponse<S>>;
|
package/dist/sync/http-events.js
CHANGED
|
@@ -8,6 +8,14 @@ function eventGuid(value) {
|
|
|
8
8
|
const candidate = record(value);
|
|
9
9
|
return typeof candidate?.eventGuid === 'string' ? candidate.eventGuid : '';
|
|
10
10
|
}
|
|
11
|
+
/** A canonical UUID of any version (the fleet's clients send v4 and v7),
|
|
12
|
+
* either case: the shape of the `uuid` column the default schema stores
|
|
13
|
+
* GUIDs in. Anything else would reach the engine's duplicate check and fail
|
|
14
|
+
* there as a Postgres data exception, logged with the client's text. */
|
|
15
|
+
const CANONICAL_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
16
|
+
/** The longest `clientGuid` a batch may carry (the fleet sends a 36-character
|
|
17
|
+
* UUID); it is stored with every event of the batch. */
|
|
18
|
+
const MAX_CLIENT_GUID_LENGTH = 128;
|
|
11
19
|
function parserError(error) {
|
|
12
20
|
return error instanceof SyncError
|
|
13
21
|
? { code: error.code, message: error.message }
|
|
@@ -21,10 +29,12 @@ function parserError(error) {
|
|
|
21
29
|
* (`applied` + committed events, `duplicate` + the stored event, `rejected`
|
|
22
30
|
* + error) and a contiguous catch-up from `lastSeq`, read after the commits:
|
|
23
31
|
* `events` + `currentSeq`, or `reload: true` when too far behind. Throws
|
|
24
|
-
* `SyncError('bad_request')` for a malformed batch (answer 400
|
|
25
|
-
* `
|
|
26
|
-
*
|
|
27
|
-
*
|
|
32
|
+
* `SyncError('bad_request')` for a malformed batch (answer 400; a
|
|
33
|
+
* `clientGuid` over 128 characters included). An event whose `eventGuid` is
|
|
34
|
+
* not a canonical UUID is `rejected` / `invalid_payload` without reaching
|
|
35
|
+
* the engine. Throws `SyncUnavailableError` when the database fails (answer
|
|
36
|
+
* 5xx; the client retries, and what committed before the failure comes
|
|
37
|
+
* back as duplicates). Events committed before such a failure still get `afterCommit`. */
|
|
28
38
|
export async function handleSyncEventsRequest(options) {
|
|
29
39
|
const batch = parseBatch(options.body);
|
|
30
40
|
const committed = { applied: [], duplicates: [] };
|
|
@@ -57,6 +67,7 @@ function parseBatch(request) {
|
|
|
57
67
|
if (!envelope ||
|
|
58
68
|
typeof envelope.clientGuid !== 'string' ||
|
|
59
69
|
envelope.clientGuid.length === 0 ||
|
|
70
|
+
envelope.clientGuid.length > MAX_CLIENT_GUID_LENGTH ||
|
|
60
71
|
!Array.isArray(envelope.events)) {
|
|
61
72
|
throw new SyncError('bad_request', 'invalid event batch');
|
|
62
73
|
}
|
|
@@ -83,6 +94,14 @@ async function applyBatch(engine, scopeKey, batch, parseClientEvent, committed)
|
|
|
83
94
|
outcomes.push({ eventGuid: guid, status: 'rejected', error: parserError(error) });
|
|
84
95
|
continue;
|
|
85
96
|
}
|
|
97
|
+
if (!CANONICAL_UUID.test(event.eventGuid)) {
|
|
98
|
+
outcomes.push({
|
|
99
|
+
eventGuid: event.eventGuid,
|
|
100
|
+
status: 'rejected',
|
|
101
|
+
error: { code: 'invalid_payload', message: 'eventGuid must be a UUID' },
|
|
102
|
+
});
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
86
105
|
const outcome = await engine.apply(scopeKey, batch.clientGuid, event);
|
|
87
106
|
if (outcome.kind === 'rejected') {
|
|
88
107
|
outcomes.push({ eventGuid: event.eventGuid, status: 'rejected', error: outcome.error });
|
package/dist/sync/types.d.ts
CHANGED
|
@@ -2,6 +2,10 @@ export interface EventBody<TType extends string = string, TPayload = unknown> {
|
|
|
2
2
|
type: TType;
|
|
3
3
|
payload: TPayload;
|
|
4
4
|
}
|
|
5
|
+
/** `eventGuid` is the event's idempotency key: a canonical UUID of any
|
|
6
|
+
* version (36 characters, 8-4-4-4-12 hex digits, either case), as the
|
|
7
|
+
* default schema's `uuid` column stores it. `handleSyncEventsRequest`
|
|
8
|
+
* rejects any other shape as `invalid_payload` before the engine runs. */
|
|
5
9
|
export type ClientEvent<TBody extends EventBody> = {
|
|
6
10
|
eventGuid: string;
|
|
7
11
|
} & TBody;
|
|
@@ -27,6 +31,9 @@ export declare class SyncUnavailableError extends Error {
|
|
|
27
31
|
constructor(cause: unknown);
|
|
28
32
|
}
|
|
29
33
|
export interface HttpEventsRequest<T extends EventBody> {
|
|
34
|
+
/** The sending tab's identity (the fleet sends a UUID), stored with every
|
|
35
|
+
* event it triggers: 1 to 128 characters, or the whole batch is refused
|
|
36
|
+
* as `bad_request`. */
|
|
30
37
|
clientGuid: string;
|
|
31
38
|
events: ClientEvent<T>[];
|
|
32
39
|
/** The tab's current sequence (every `SyncClient` POST). A request that
|
package/package.json
CHANGED