@bleedingdev/modern-js-server-runtime-extensions 3.5.0-ultramodern.36 → 3.5.0-ultramodern.38
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/cjs/contract-gate-snapshot-normalization.js +122 -0
- package/dist/cjs/contract-gate-snapshot-store/file-store.js +13 -13
- package/dist/cjs/contract-gate-snapshot-store/http-store.js +2 -11
- package/dist/cjs/contract-gate-snapshot-store/resolve.js +8 -6
- package/dist/cjs/contractGateAutopilot.js +8 -77
- package/dist/cjs/index.js +6 -25
- package/dist/cjs/module-federation-css/manifest.js +3 -18
- package/dist/cjs/runtime-fallback-signal/index.js +21 -27
- package/dist/cjs/runtime-fallback-signal/model.js +157 -0
- package/dist/cjs/runtime-fallback-signal/resolver.js +163 -0
- package/dist/cjs/runtime-fallback-signal/{contract-gate.js → store.js} +19 -0
- package/dist/cjs/telemetry/envelope.js +74 -0
- package/dist/cjs/telemetry/registry.js +20 -72
- package/dist/esm/contract-gate-snapshot-normalization.mjs +81 -0
- package/dist/esm/contract-gate-snapshot-store/file-store.mjs +7 -7
- package/dist/esm/contract-gate-snapshot-store/http-store.mjs +1 -1
- package/dist/esm/contract-gate-snapshot-store/resolve.mjs +7 -5
- package/dist/esm/contractGateAutopilot.mjs +7 -76
- package/dist/esm/index.mjs +0 -1
- package/dist/esm/module-federation-css/manifest.mjs +3 -3
- package/dist/esm/runtime-fallback-signal/index.mjs +3 -9
- package/dist/esm/runtime-fallback-signal/model.mjs +73 -0
- package/dist/esm/runtime-fallback-signal/resolver.mjs +107 -0
- package/dist/esm/runtime-fallback-signal/{contract-gate.mjs → store.mjs} +14 -1
- package/dist/esm/telemetry/envelope.mjs +63 -1
- package/dist/esm/telemetry/registry.mjs +21 -73
- package/dist/esm-node/contract-gate-snapshot-normalization.mjs +82 -0
- package/dist/esm-node/contract-gate-snapshot-store/file-store.mjs +7 -7
- package/dist/esm-node/contract-gate-snapshot-store/http-store.mjs +1 -1
- package/dist/esm-node/contract-gate-snapshot-store/resolve.mjs +8 -7
- package/dist/esm-node/contractGateAutopilot.mjs +7 -76
- package/dist/esm-node/index.mjs +0 -1
- package/dist/esm-node/module-federation-css/manifest.mjs +3 -3
- package/dist/esm-node/runtime-fallback-signal/index.mjs +3 -9
- package/dist/esm-node/runtime-fallback-signal/model.mjs +74 -0
- package/dist/esm-node/runtime-fallback-signal/resolver.mjs +108 -0
- package/dist/esm-node/runtime-fallback-signal/{contract-gate.mjs → store.mjs} +14 -1
- package/dist/esm-node/telemetry/envelope.mjs +63 -1
- package/dist/esm-node/telemetry/registry.mjs +21 -73
- package/dist/types/contract-gate-snapshot-normalization.d.ts +15 -0
- package/dist/types/contract-gate-snapshot-store/http-store.d.ts +0 -6
- package/dist/types/contractGateAutopilot.d.ts +1 -6
- package/dist/types/index.d.ts +0 -1
- package/dist/types/module-federation-css/manifest.d.ts +0 -5
- package/dist/types/runtime-fallback-signal/index.d.ts +4 -10
- package/dist/types/runtime-fallback-signal/model.d.ts +70 -0
- package/dist/types/runtime-fallback-signal/resolver.d.ts +29 -0
- package/dist/types/runtime-fallback-signal/store.d.ts +4 -0
- package/dist/types/telemetry/canaryEvaluation.d.ts +3 -2
- package/dist/types/telemetry/envelope.d.ts +10 -0
- package/dist/types/telemetry/registry.d.ts +1 -4
- package/package.json +10 -10
- package/src/contract-gate-snapshot-normalization.ts +155 -0
- package/src/contract-gate-snapshot-store/file-store.ts +2 -4
- package/src/contract-gate-snapshot-store/http-store.ts +3 -3
- package/src/contract-gate-snapshot-store/resolve.ts +8 -4
- package/src/contractGateAutopilot.ts +13 -141
- package/src/index.ts +0 -16
- package/src/module-federation-css/manifest.ts +6 -9
- package/src/runtime-fallback-signal/index.ts +23 -25
- package/src/runtime-fallback-signal/model.ts +230 -0
- package/src/runtime-fallback-signal/resolver.ts +304 -0
- package/src/runtime-fallback-signal/{contract-gate.ts → store.ts} +38 -3
- package/src/telemetry/canaryEvaluation.ts +2 -2
- package/src/telemetry/envelope.ts +84 -0
- package/src/telemetry/registry.ts +31 -74
- package/tests/telemetryAutopilot.test.ts +141 -0
- package/tests/telemetryRegistryQueueMatrix.test.ts +227 -0
- package/dist/cjs/runtime-fallback-signal/auth.js +0 -87
- package/dist/cjs/runtime-fallback-signal/constants.js +0 -72
- package/dist/cjs/runtime-fallback-signal/endpoint.js +0 -50
- package/dist/cjs/runtime-fallback-signal/errors.js +0 -44
- package/dist/cjs/runtime-fallback-signal/payload.js +0 -72
- package/dist/cjs/runtime-fallback-signal/source.js +0 -66
- package/dist/cjs/runtime-fallback-signal/state.js +0 -57
- package/dist/cjs/runtime-fallback-signal/status.js +0 -47
- package/dist/cjs/runtime-fallback-signal/trust-policy.js +0 -108
- package/dist/cjs/runtime-fallback-signal/types.js +0 -18
- package/dist/esm/runtime-fallback-signal/auth.mjs +0 -40
- package/dist/esm/runtime-fallback-signal/constants.mjs +0 -10
- package/dist/esm/runtime-fallback-signal/endpoint.mjs +0 -12
- package/dist/esm/runtime-fallback-signal/errors.mjs +0 -6
- package/dist/esm/runtime-fallback-signal/payload.mjs +0 -31
- package/dist/esm/runtime-fallback-signal/source.mjs +0 -22
- package/dist/esm/runtime-fallback-signal/state.mjs +0 -16
- package/dist/esm/runtime-fallback-signal/status.mjs +0 -9
- package/dist/esm/runtime-fallback-signal/trust-policy.mjs +0 -67
- package/dist/esm/runtime-fallback-signal/types.mjs +0 -0
- package/dist/esm-node/runtime-fallback-signal/auth.mjs +0 -41
- package/dist/esm-node/runtime-fallback-signal/constants.mjs +0 -11
- package/dist/esm-node/runtime-fallback-signal/endpoint.mjs +0 -13
- package/dist/esm-node/runtime-fallback-signal/errors.mjs +0 -7
- package/dist/esm-node/runtime-fallback-signal/payload.mjs +0 -32
- package/dist/esm-node/runtime-fallback-signal/source.mjs +0 -23
- package/dist/esm-node/runtime-fallback-signal/state.mjs +0 -17
- package/dist/esm-node/runtime-fallback-signal/status.mjs +0 -10
- package/dist/esm-node/runtime-fallback-signal/trust-policy.mjs +0 -68
- package/dist/esm-node/runtime-fallback-signal/types.mjs +0 -1
- package/dist/types/runtime-fallback-signal/auth.d.ts +0 -18
- package/dist/types/runtime-fallback-signal/constants.d.ts +0 -9
- package/dist/types/runtime-fallback-signal/contract-gate.d.ts +0 -2
- package/dist/types/runtime-fallback-signal/endpoint.d.ts +0 -1
- package/dist/types/runtime-fallback-signal/errors.d.ts +0 -2
- package/dist/types/runtime-fallback-signal/payload.d.ts +0 -6
- package/dist/types/runtime-fallback-signal/source.d.ts +0 -3
- package/dist/types/runtime-fallback-signal/state.d.ts +0 -3
- package/dist/types/runtime-fallback-signal/status.d.ts +0 -2
- package/dist/types/runtime-fallback-signal/trust-policy.d.ts +0 -13
- package/dist/types/runtime-fallback-signal/types.d.ts +0 -50
- package/src/runtime-fallback-signal/auth.ts +0 -117
- package/src/runtime-fallback-signal/constants.ts +0 -19
- package/src/runtime-fallback-signal/endpoint.ts +0 -22
- package/src/runtime-fallback-signal/errors.ts +0 -10
- package/src/runtime-fallback-signal/payload.ts +0 -79
- package/src/runtime-fallback-signal/source.ts +0 -49
- package/src/runtime-fallback-signal/state.ts +0 -34
- package/src/runtime-fallback-signal/status.ts +0 -22
- package/src/runtime-fallback-signal/trust-policy.ts +0 -179
- package/src/runtime-fallback-signal/types.ts +0 -66
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
function normalizeRuntimeSignalOrigin(value) {
|
|
2
|
-
if ('string' != typeof value || 0 === value.trim().length) return;
|
|
3
|
-
try {
|
|
4
|
-
return new URL(value).origin;
|
|
5
|
-
} catch (_error) {
|
|
6
|
-
return;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
function normalizeRuntimeSignalAppName(payload) {
|
|
10
|
-
if ('string' != typeof payload.appName) return 'unknown';
|
|
11
|
-
const normalized = payload.appName.trim();
|
|
12
|
-
return normalized.length > 0 ? normalized : 'unknown';
|
|
13
|
-
}
|
|
14
|
-
function normalizeRuntimeSignalRuntimeDigest(payload) {
|
|
15
|
-
if ('string' == typeof payload.runtimeDigest && payload.runtimeDigest.trim()) return payload.runtimeDigest.trim();
|
|
16
|
-
const metadata = payload.metadata;
|
|
17
|
-
if (metadata && 'object' == typeof metadata && !Array.isArray(metadata) && 'string' == typeof metadata.runtimeDigest) {
|
|
18
|
-
const digest = String(metadata.runtimeDigest).trim();
|
|
19
|
-
if (digest) return digest;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
export { normalizeRuntimeSignalAppName, normalizeRuntimeSignalOrigin, normalizeRuntimeSignalRuntimeDigest };
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
function createRuntimeFallbackSignalRuntimeState() {
|
|
2
|
-
return {
|
|
3
|
-
rateLimitBySource: new Map(),
|
|
4
|
-
dedupeByFingerprint: new Map()
|
|
5
|
-
};
|
|
6
|
-
}
|
|
7
|
-
function cleanupRuntimeFallbackSignalRuntimeState(now, runtimeState, trustPolicy) {
|
|
8
|
-
const dedupeExpiryMs = Math.max(trustPolicy.dedupeWindowMs, trustPolicy.windowMs, 1000);
|
|
9
|
-
runtimeState.dedupeByFingerprint.forEach((lastSeenAt, fingerprint)=>{
|
|
10
|
-
if (now - lastSeenAt > dedupeExpiryMs) runtimeState.dedupeByFingerprint.delete(fingerprint);
|
|
11
|
-
});
|
|
12
|
-
runtimeState.rateLimitBySource.forEach((state, source)=>{
|
|
13
|
-
if (now - state.windowStartedAt > 2 * trustPolicy.windowMs) runtimeState.rateLimitBySource.delete(source);
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
export { cleanupRuntimeFallbackSignalRuntimeState, createRuntimeFallbackSignalRuntimeState };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
function getRuntimeSignalErrorStatusCode(signalError) {
|
|
2
|
-
if ('PAYLOAD_TOO_LARGE' === signalError.code) return 413;
|
|
3
|
-
if ('INVALID_PAYLOAD' === signalError.code) return 400;
|
|
4
|
-
if ('UNAUTHORIZED' === signalError.code) return 401;
|
|
5
|
-
if ('RATE_LIMITED' === signalError.code) return 429;
|
|
6
|
-
if ('UNTRUSTED_SOURCE' === signalError.code) return 403;
|
|
7
|
-
return 500;
|
|
8
|
-
}
|
|
9
|
-
export { getRuntimeSignalErrorStatusCode };
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS, DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW, DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS } from "./constants.mjs";
|
|
2
|
-
import { createRuntimeSignalError } from "./errors.mjs";
|
|
3
|
-
import { normalizeRuntimeSignalAppName, normalizeRuntimeSignalOrigin, normalizeRuntimeSignalRuntimeDigest } from "./source.mjs";
|
|
4
|
-
import { cleanupRuntimeFallbackSignalRuntimeState } from "./state.mjs";
|
|
5
|
-
function normalizeRuntimeFallbackTrustPolicy(configured) {
|
|
6
|
-
const allowedApps = Array.isArray(configured?.allowedApps) ? configured.allowedApps.map((item)=>'string' == typeof item ? item.trim() : '').filter(Boolean) : [];
|
|
7
|
-
const allowedEntryOrigins = Array.isArray(configured?.allowedEntryOrigins) ? configured.allowedEntryOrigins.map((item)=>normalizeRuntimeSignalOrigin(item)).filter((item)=>Boolean(item)) : [];
|
|
8
|
-
const expectedRuntimeDigestsRaw = configured?.expectedRuntimeDigests || {};
|
|
9
|
-
const expectedRuntimeDigests = {};
|
|
10
|
-
Object.entries(expectedRuntimeDigestsRaw).forEach(([appName, digest])=>{
|
|
11
|
-
if ('string' == typeof appName && appName.trim().length > 0 && 'string' == typeof digest && digest.trim().length > 0) expectedRuntimeDigests[appName.trim()] = digest.trim();
|
|
12
|
-
});
|
|
13
|
-
return {
|
|
14
|
-
allowedApps,
|
|
15
|
-
allowedEntryOrigins,
|
|
16
|
-
expectedRuntimeDigests,
|
|
17
|
-
enforceRuntimeDigest: configured?.enforceRuntimeDigest === true,
|
|
18
|
-
maxSignalsPerWindow: Math.max(1, Math.floor(configured?.maxSignalsPerWindow ?? DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW)),
|
|
19
|
-
windowMs: Math.max(1000, Math.floor(configured?.windowMs ?? DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS)),
|
|
20
|
-
dedupeWindowMs: Math.max(0, Math.floor(configured?.dedupeWindowMs ?? DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS))
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
function enforceRuntimeFallbackSignalTrustPolicy(payload, runtimeSignalContext, source = {}) {
|
|
24
|
-
const { trustPolicy, runtimeState } = runtimeSignalContext;
|
|
25
|
-
const now = Date.now();
|
|
26
|
-
cleanupRuntimeFallbackSignalRuntimeState(now, runtimeState, trustPolicy);
|
|
27
|
-
const appName = normalizeRuntimeSignalAppName(payload);
|
|
28
|
-
const entryOrigin = normalizeRuntimeSignalOrigin(payload.entry);
|
|
29
|
-
const runtimeDigest = normalizeRuntimeSignalRuntimeDigest(payload);
|
|
30
|
-
if (trustPolicy.allowedApps.length > 0 && !trustPolicy.allowedApps.includes(appName)) throw createRuntimeSignalError(`runtime fallback signal app "${appName}" is not trusted`, 'UNTRUSTED_SOURCE');
|
|
31
|
-
if (trustPolicy.allowedEntryOrigins.length > 0) {
|
|
32
|
-
if (!entryOrigin || !trustPolicy.allowedEntryOrigins.includes(entryOrigin)) throw createRuntimeSignalError(`runtime fallback signal entry origin "${entryOrigin || 'unknown'}" is not trusted`, 'UNTRUSTED_SOURCE');
|
|
33
|
-
}
|
|
34
|
-
const expectedDigest = trustPolicy.expectedRuntimeDigests[appName];
|
|
35
|
-
if (expectedDigest && runtimeDigest !== expectedDigest) throw createRuntimeSignalError(`runtime fallback runtimeDigest mismatch for app "${appName}"`, 'UNTRUSTED_SOURCE');
|
|
36
|
-
if (trustPolicy.enforceRuntimeDigest && !runtimeDigest) throw createRuntimeSignalError(`runtime fallback signal for app "${appName}" is missing runtimeDigest`, 'UNTRUSTED_SOURCE');
|
|
37
|
-
const dedupeFingerprint = JSON.stringify({
|
|
38
|
-
appName,
|
|
39
|
-
entryOrigin: entryOrigin || 'unknown',
|
|
40
|
-
reason: payload.reason || 'runtime_fallback',
|
|
41
|
-
phase: payload.phase || 'unknown',
|
|
42
|
-
runtimeDigest: runtimeDigest || 'unknown'
|
|
43
|
-
});
|
|
44
|
-
const dedupeWindowMs = trustPolicy.dedupeWindowMs;
|
|
45
|
-
if (dedupeWindowMs > 0) {
|
|
46
|
-
const lastSeenAt = runtimeState.dedupeByFingerprint.get(dedupeFingerprint);
|
|
47
|
-
runtimeState.dedupeByFingerprint.set(dedupeFingerprint, now);
|
|
48
|
-
if ('number' == typeof lastSeenAt && now - lastSeenAt <= dedupeWindowMs) return {
|
|
49
|
-
deduped: true
|
|
50
|
-
};
|
|
51
|
-
} else runtimeState.dedupeByFingerprint.set(dedupeFingerprint, now);
|
|
52
|
-
const remoteAddress = source.remoteAddress?.trim();
|
|
53
|
-
const sourceKey = remoteAddress || 'unknown-remote';
|
|
54
|
-
const rateState = runtimeState.rateLimitBySource.get(sourceKey);
|
|
55
|
-
if (!rateState || now - rateState.windowStartedAt > trustPolicy.windowMs) runtimeState.rateLimitBySource.set(sourceKey, {
|
|
56
|
-
count: 1,
|
|
57
|
-
windowStartedAt: now
|
|
58
|
-
});
|
|
59
|
-
else {
|
|
60
|
-
if (rateState.count >= trustPolicy.maxSignalsPerWindow) throw createRuntimeSignalError(`runtime fallback signal rate-limited for source "${sourceKey}"`, 'RATE_LIMITED');
|
|
61
|
-
rateState.count += 1;
|
|
62
|
-
}
|
|
63
|
-
return {
|
|
64
|
-
deduped: false
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
export { enforceRuntimeFallbackSignalTrustPolicy, normalizeRuntimeFallbackTrustPolicy };
|
|
File without changes
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
import { createHash, timingSafeEqual } from "node:crypto";
|
|
3
|
-
import { DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER } from "./constants.mjs";
|
|
4
|
-
import { createRuntimeSignalError } from "./errors.mjs";
|
|
5
|
-
function normalizeRuntimeFallbackSignalAuthConfig(configured) {
|
|
6
|
-
const headerName = 'string' == typeof configured?.headerName && configured.headerName.trim() ? configured.headerName.trim().toLowerCase() : DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER;
|
|
7
|
-
const expectedFromEnv = 'string' == typeof configured?.expectedValueEnv && configured.expectedValueEnv.trim().length > 0 ? process.env[configured.expectedValueEnv.trim()] : void 0;
|
|
8
|
-
const expectedFromConfig = 'string' == typeof configured?.expectedValue && configured.expectedValue.trim().length > 0 ? configured.expectedValue.trim() : void 0;
|
|
9
|
-
const expectedValue = expectedFromConfig || expectedFromEnv;
|
|
10
|
-
const enabled = configured?.enabled === true;
|
|
11
|
-
if (enabled && !expectedValue) throw new Error('[telemetry.canary.autopilot.runtimeFallbackSignal] auth.enabled is true but no expected token is configured');
|
|
12
|
-
return {
|
|
13
|
-
enabled,
|
|
14
|
-
headerName,
|
|
15
|
-
expectedValue
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
function normalizeRequiredRuntimeFallbackSignalAuthConfig(configured) {
|
|
19
|
-
if (configured?.enabled === false) throw new Error('[telemetry.canary.autopilot.runtimeFallbackSignal] the endpoint cannot be enabled with auth disabled; configure auth.expectedValue or auth.expectedValueEnv');
|
|
20
|
-
try {
|
|
21
|
-
return normalizeRuntimeFallbackSignalAuthConfig({
|
|
22
|
-
...configured,
|
|
23
|
-
enabled: true
|
|
24
|
-
});
|
|
25
|
-
} catch (_error) {
|
|
26
|
-
throw new Error('[telemetry.canary.autopilot.runtimeFallbackSignal] enabling the endpoint requires an auth token; configure auth.expectedValue or auth.expectedValueEnv');
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
function safeTokenEquals(candidate, expected) {
|
|
30
|
-
const candidateDigest = createHash('sha256').update(candidate).digest();
|
|
31
|
-
const expectedDigest = createHash('sha256').update(expected).digest();
|
|
32
|
-
return timingSafeEqual(candidateDigest, expectedDigest);
|
|
33
|
-
}
|
|
34
|
-
function enforceRuntimeFallbackSignalAuthToken(token, authConfig) {
|
|
35
|
-
if (!authConfig.enabled) return;
|
|
36
|
-
if (!token || !authConfig.expectedValue || !safeTokenEquals(token, authConfig.expectedValue)) throw createRuntimeSignalError('runtime fallback signal auth failed', 'UNAUTHORIZED');
|
|
37
|
-
}
|
|
38
|
-
function enforceRuntimeFallbackSignalAuth(c, runtimeSignalConfig) {
|
|
39
|
-
enforceRuntimeFallbackSignalAuthToken(c.req.header(runtimeSignalConfig.auth.headerName), runtimeSignalConfig.auth);
|
|
40
|
-
}
|
|
41
|
-
export { enforceRuntimeFallbackSignalAuth, enforceRuntimeFallbackSignalAuthToken, normalizeRequiredRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackSignalAuthConfig };
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
const DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT = '/_modern/contract-gates/runtime-fallback';
|
|
3
|
-
const DEFAULT_RUNTIME_STATUS_ENDPOINT = '/_modern/runtime/status';
|
|
4
|
-
const DEFAULT_RUNTIME_FALLBACK_GATE_NAME = 'runtime-mf-fallback-health';
|
|
5
|
-
const DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS = 300000;
|
|
6
|
-
const DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES = 16384;
|
|
7
|
-
const DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER = 'x-modernjs-runtime-signal-token';
|
|
8
|
-
const DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW = 30;
|
|
9
|
-
const DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS = 60000;
|
|
10
|
-
const DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS = 10000;
|
|
11
|
-
export { DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER, DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS, DEFAULT_RUNTIME_FALLBACK_GATE_NAME, DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES, DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS, DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW, DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS, DEFAULT_RUNTIME_STATUS_ENDPOINT };
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
import { DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT } from "./constants.mjs";
|
|
3
|
-
function resolveRuntimeFallbackSignalEndpoint(configuredEndpoint) {
|
|
4
|
-
const rawEndpoint = configuredEndpoint?.trim();
|
|
5
|
-
if (!rawEndpoint) return DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT;
|
|
6
|
-
if (rawEndpoint.startsWith('/')) return rawEndpoint;
|
|
7
|
-
try {
|
|
8
|
-
return new URL(rawEndpoint).pathname || DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT;
|
|
9
|
-
} catch (_error) {
|
|
10
|
-
return `/${rawEndpoint.replace(/^\/+/, '')}`;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
export { resolveRuntimeFallbackSignalEndpoint };
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
import { createRuntimeSignalError } from "./errors.mjs";
|
|
3
|
-
function getUtf8ByteLength(input) {
|
|
4
|
-
if ("u" > typeof Buffer) return Buffer.byteLength(input);
|
|
5
|
-
return new TextEncoder().encode(input).length;
|
|
6
|
-
}
|
|
7
|
-
async function parseRuntimeFallbackSignalPayload(c, maxBodyBytes) {
|
|
8
|
-
const contentLengthHeader = c.req.header('content-length');
|
|
9
|
-
if (contentLengthHeader) {
|
|
10
|
-
const contentLength = Number.parseInt(contentLengthHeader, 10);
|
|
11
|
-
if (Number.isFinite(contentLength) && contentLength > maxBodyBytes) throw createRuntimeSignalError('runtime fallback signal payload too large', 'PAYLOAD_TOO_LARGE');
|
|
12
|
-
}
|
|
13
|
-
const rawBody = await c.req.raw.text();
|
|
14
|
-
const payload = parseRuntimeFallbackSignalPayloadFromRawBody(rawBody, maxBodyBytes);
|
|
15
|
-
return {
|
|
16
|
-
rawBody,
|
|
17
|
-
payload
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
function parseRuntimeFallbackSignalPayloadFromRawBody(rawBody, maxBodyBytes) {
|
|
21
|
-
if (!rawBody || 0 === rawBody.trim().length) throw createRuntimeSignalError('runtime fallback signal body is empty', 'INVALID_PAYLOAD');
|
|
22
|
-
if (getUtf8ByteLength(rawBody) > maxBodyBytes) throw createRuntimeSignalError('runtime fallback signal payload too large', 'PAYLOAD_TOO_LARGE');
|
|
23
|
-
let payload;
|
|
24
|
-
try {
|
|
25
|
-
payload = JSON.parse(rawBody);
|
|
26
|
-
} catch (_error) {
|
|
27
|
-
throw createRuntimeSignalError('runtime fallback signal body must be valid JSON', 'INVALID_PAYLOAD');
|
|
28
|
-
}
|
|
29
|
-
if (!payload || 'object' != typeof payload || Array.isArray(payload)) throw createRuntimeSignalError('runtime fallback signal body must be a JSON object', 'INVALID_PAYLOAD');
|
|
30
|
-
return payload;
|
|
31
|
-
}
|
|
32
|
-
export { parseRuntimeFallbackSignalPayload, parseRuntimeFallbackSignalPayloadFromRawBody };
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
function normalizeRuntimeSignalOrigin(value) {
|
|
3
|
-
if ('string' != typeof value || 0 === value.trim().length) return;
|
|
4
|
-
try {
|
|
5
|
-
return new URL(value).origin;
|
|
6
|
-
} catch (_error) {
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
function normalizeRuntimeSignalAppName(payload) {
|
|
11
|
-
if ('string' != typeof payload.appName) return 'unknown';
|
|
12
|
-
const normalized = payload.appName.trim();
|
|
13
|
-
return normalized.length > 0 ? normalized : 'unknown';
|
|
14
|
-
}
|
|
15
|
-
function normalizeRuntimeSignalRuntimeDigest(payload) {
|
|
16
|
-
if ('string' == typeof payload.runtimeDigest && payload.runtimeDigest.trim()) return payload.runtimeDigest.trim();
|
|
17
|
-
const metadata = payload.metadata;
|
|
18
|
-
if (metadata && 'object' == typeof metadata && !Array.isArray(metadata) && 'string' == typeof metadata.runtimeDigest) {
|
|
19
|
-
const digest = String(metadata.runtimeDigest).trim();
|
|
20
|
-
if (digest) return digest;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
export { normalizeRuntimeSignalAppName, normalizeRuntimeSignalOrigin, normalizeRuntimeSignalRuntimeDigest };
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
function createRuntimeFallbackSignalRuntimeState() {
|
|
3
|
-
return {
|
|
4
|
-
rateLimitBySource: new Map(),
|
|
5
|
-
dedupeByFingerprint: new Map()
|
|
6
|
-
};
|
|
7
|
-
}
|
|
8
|
-
function cleanupRuntimeFallbackSignalRuntimeState(now, runtimeState, trustPolicy) {
|
|
9
|
-
const dedupeExpiryMs = Math.max(trustPolicy.dedupeWindowMs, trustPolicy.windowMs, 1000);
|
|
10
|
-
runtimeState.dedupeByFingerprint.forEach((lastSeenAt, fingerprint)=>{
|
|
11
|
-
if (now - lastSeenAt > dedupeExpiryMs) runtimeState.dedupeByFingerprint.delete(fingerprint);
|
|
12
|
-
});
|
|
13
|
-
runtimeState.rateLimitBySource.forEach((state, source)=>{
|
|
14
|
-
if (now - state.windowStartedAt > 2 * trustPolicy.windowMs) runtimeState.rateLimitBySource.delete(source);
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
export { cleanupRuntimeFallbackSignalRuntimeState, createRuntimeFallbackSignalRuntimeState };
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
function getRuntimeSignalErrorStatusCode(signalError) {
|
|
3
|
-
if ('PAYLOAD_TOO_LARGE' === signalError.code) return 413;
|
|
4
|
-
if ('INVALID_PAYLOAD' === signalError.code) return 400;
|
|
5
|
-
if ('UNAUTHORIZED' === signalError.code) return 401;
|
|
6
|
-
if ('RATE_LIMITED' === signalError.code) return 429;
|
|
7
|
-
if ('UNTRUSTED_SOURCE' === signalError.code) return 403;
|
|
8
|
-
return 500;
|
|
9
|
-
}
|
|
10
|
-
export { getRuntimeSignalErrorStatusCode };
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
import { DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS, DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW, DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS } from "./constants.mjs";
|
|
3
|
-
import { createRuntimeSignalError } from "./errors.mjs";
|
|
4
|
-
import { normalizeRuntimeSignalAppName, normalizeRuntimeSignalOrigin, normalizeRuntimeSignalRuntimeDigest } from "./source.mjs";
|
|
5
|
-
import { cleanupRuntimeFallbackSignalRuntimeState } from "./state.mjs";
|
|
6
|
-
function normalizeRuntimeFallbackTrustPolicy(configured) {
|
|
7
|
-
const allowedApps = Array.isArray(configured?.allowedApps) ? configured.allowedApps.map((item)=>'string' == typeof item ? item.trim() : '').filter(Boolean) : [];
|
|
8
|
-
const allowedEntryOrigins = Array.isArray(configured?.allowedEntryOrigins) ? configured.allowedEntryOrigins.map((item)=>normalizeRuntimeSignalOrigin(item)).filter((item)=>Boolean(item)) : [];
|
|
9
|
-
const expectedRuntimeDigestsRaw = configured?.expectedRuntimeDigests || {};
|
|
10
|
-
const expectedRuntimeDigests = {};
|
|
11
|
-
Object.entries(expectedRuntimeDigestsRaw).forEach(([appName, digest])=>{
|
|
12
|
-
if ('string' == typeof appName && appName.trim().length > 0 && 'string' == typeof digest && digest.trim().length > 0) expectedRuntimeDigests[appName.trim()] = digest.trim();
|
|
13
|
-
});
|
|
14
|
-
return {
|
|
15
|
-
allowedApps,
|
|
16
|
-
allowedEntryOrigins,
|
|
17
|
-
expectedRuntimeDigests,
|
|
18
|
-
enforceRuntimeDigest: configured?.enforceRuntimeDigest === true,
|
|
19
|
-
maxSignalsPerWindow: Math.max(1, Math.floor(configured?.maxSignalsPerWindow ?? DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW)),
|
|
20
|
-
windowMs: Math.max(1000, Math.floor(configured?.windowMs ?? DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS)),
|
|
21
|
-
dedupeWindowMs: Math.max(0, Math.floor(configured?.dedupeWindowMs ?? DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS))
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
function enforceRuntimeFallbackSignalTrustPolicy(payload, runtimeSignalContext, source = {}) {
|
|
25
|
-
const { trustPolicy, runtimeState } = runtimeSignalContext;
|
|
26
|
-
const now = Date.now();
|
|
27
|
-
cleanupRuntimeFallbackSignalRuntimeState(now, runtimeState, trustPolicy);
|
|
28
|
-
const appName = normalizeRuntimeSignalAppName(payload);
|
|
29
|
-
const entryOrigin = normalizeRuntimeSignalOrigin(payload.entry);
|
|
30
|
-
const runtimeDigest = normalizeRuntimeSignalRuntimeDigest(payload);
|
|
31
|
-
if (trustPolicy.allowedApps.length > 0 && !trustPolicy.allowedApps.includes(appName)) throw createRuntimeSignalError(`runtime fallback signal app "${appName}" is not trusted`, 'UNTRUSTED_SOURCE');
|
|
32
|
-
if (trustPolicy.allowedEntryOrigins.length > 0) {
|
|
33
|
-
if (!entryOrigin || !trustPolicy.allowedEntryOrigins.includes(entryOrigin)) throw createRuntimeSignalError(`runtime fallback signal entry origin "${entryOrigin || 'unknown'}" is not trusted`, 'UNTRUSTED_SOURCE');
|
|
34
|
-
}
|
|
35
|
-
const expectedDigest = trustPolicy.expectedRuntimeDigests[appName];
|
|
36
|
-
if (expectedDigest && runtimeDigest !== expectedDigest) throw createRuntimeSignalError(`runtime fallback runtimeDigest mismatch for app "${appName}"`, 'UNTRUSTED_SOURCE');
|
|
37
|
-
if (trustPolicy.enforceRuntimeDigest && !runtimeDigest) throw createRuntimeSignalError(`runtime fallback signal for app "${appName}" is missing runtimeDigest`, 'UNTRUSTED_SOURCE');
|
|
38
|
-
const dedupeFingerprint = JSON.stringify({
|
|
39
|
-
appName,
|
|
40
|
-
entryOrigin: entryOrigin || 'unknown',
|
|
41
|
-
reason: payload.reason || 'runtime_fallback',
|
|
42
|
-
phase: payload.phase || 'unknown',
|
|
43
|
-
runtimeDigest: runtimeDigest || 'unknown'
|
|
44
|
-
});
|
|
45
|
-
const dedupeWindowMs = trustPolicy.dedupeWindowMs;
|
|
46
|
-
if (dedupeWindowMs > 0) {
|
|
47
|
-
const lastSeenAt = runtimeState.dedupeByFingerprint.get(dedupeFingerprint);
|
|
48
|
-
runtimeState.dedupeByFingerprint.set(dedupeFingerprint, now);
|
|
49
|
-
if ('number' == typeof lastSeenAt && now - lastSeenAt <= dedupeWindowMs) return {
|
|
50
|
-
deduped: true
|
|
51
|
-
};
|
|
52
|
-
} else runtimeState.dedupeByFingerprint.set(dedupeFingerprint, now);
|
|
53
|
-
const remoteAddress = source.remoteAddress?.trim();
|
|
54
|
-
const sourceKey = remoteAddress || 'unknown-remote';
|
|
55
|
-
const rateState = runtimeState.rateLimitBySource.get(sourceKey);
|
|
56
|
-
if (!rateState || now - rateState.windowStartedAt > trustPolicy.windowMs) runtimeState.rateLimitBySource.set(sourceKey, {
|
|
57
|
-
count: 1,
|
|
58
|
-
windowStartedAt: now
|
|
59
|
-
});
|
|
60
|
-
else {
|
|
61
|
-
if (rateState.count >= trustPolicy.maxSignalsPerWindow) throw createRuntimeSignalError(`runtime fallback signal rate-limited for source "${sourceKey}"`, 'RATE_LIMITED');
|
|
62
|
-
rateState.count += 1;
|
|
63
|
-
}
|
|
64
|
-
return {
|
|
65
|
-
deduped: false
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
export { enforceRuntimeFallbackSignalTrustPolicy, normalizeRuntimeFallbackTrustPolicy };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { Context, ServerEnv } from '@modern-js/server-core';
|
|
2
|
-
import type { RuntimeFallbackSignalAuthConfig, RuntimeFallbackSignalConfig } from './types';
|
|
3
|
-
export declare function normalizeRuntimeFallbackSignalAuthConfig(configured: {
|
|
4
|
-
enabled?: boolean;
|
|
5
|
-
headerName?: string;
|
|
6
|
-
expectedValue?: string;
|
|
7
|
-
expectedValueEnv?: string;
|
|
8
|
-
} | undefined): RuntimeFallbackSignalAuthConfig;
|
|
9
|
-
/**
|
|
10
|
-
* Normalizes the auth config for the runtime fallback signal endpoint when the
|
|
11
|
-
* endpoint itself is enabled. The endpoint can persist failing contract gates
|
|
12
|
-
* (a canary kill switch), so it always requires a token: auth cannot be
|
|
13
|
-
* disabled and a token must be configured via `auth.expectedValue` or
|
|
14
|
-
* `auth.expectedValueEnv`.
|
|
15
|
-
*/
|
|
16
|
-
export declare function normalizeRequiredRuntimeFallbackSignalAuthConfig(configured: Parameters<typeof normalizeRuntimeFallbackSignalAuthConfig>[0]): RuntimeFallbackSignalAuthConfig;
|
|
17
|
-
export declare function enforceRuntimeFallbackSignalAuthToken(token: string | undefined, authConfig: RuntimeFallbackSignalAuthConfig): void;
|
|
18
|
-
export declare function enforceRuntimeFallbackSignalAuth(c: Context<ServerEnv>, runtimeSignalConfig: RuntimeFallbackSignalConfig): void;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export declare const DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT = "/_modern/contract-gates/runtime-fallback";
|
|
2
|
-
export declare const DEFAULT_RUNTIME_STATUS_ENDPOINT = "/_modern/runtime/status";
|
|
3
|
-
export declare const DEFAULT_RUNTIME_FALLBACK_GATE_NAME = "runtime-mf-fallback-health";
|
|
4
|
-
export declare const DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS: number;
|
|
5
|
-
export declare const DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES: number;
|
|
6
|
-
export declare const DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER = "x-modernjs-runtime-signal-token";
|
|
7
|
-
export declare const DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW = 30;
|
|
8
|
-
export declare const DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS = 60000;
|
|
9
|
-
export declare const DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS = 10000;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function resolveRuntimeFallbackSignalEndpoint(configuredEndpoint?: string): string;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { Context, ServerEnv } from '@modern-js/server-core';
|
|
2
|
-
export declare function parseRuntimeFallbackSignalPayload(c: Context<ServerEnv>, maxBodyBytes: number): Promise<{
|
|
3
|
-
rawBody: string;
|
|
4
|
-
payload: Record<string, unknown>;
|
|
5
|
-
}>;
|
|
6
|
-
export declare function parseRuntimeFallbackSignalPayloadFromRawBody(rawBody: string, maxBodyBytes: number): Record<string, unknown>;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
export declare function normalizeRuntimeSignalOrigin(value: unknown): string | undefined;
|
|
2
|
-
export declare function normalizeRuntimeSignalAppName(payload: Record<string, unknown>): string;
|
|
3
|
-
export declare function normalizeRuntimeSignalRuntimeDigest(payload: Record<string, unknown>): string | undefined;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { RuntimeFallbackSignalRuntimeState, RuntimeFallbackSignalTrustPolicy } from './types';
|
|
2
|
-
export declare function createRuntimeFallbackSignalRuntimeState(): RuntimeFallbackSignalRuntimeState;
|
|
3
|
-
export declare function cleanupRuntimeFallbackSignalRuntimeState(now: number, runtimeState: RuntimeFallbackSignalRuntimeState, trustPolicy: RuntimeFallbackSignalTrustPolicy): void;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { RuntimeFallbackSignalSource, RuntimeFallbackSignalTrustContext, RuntimeFallbackSignalTrustPolicy } from './types';
|
|
2
|
-
export declare function normalizeRuntimeFallbackTrustPolicy(configured: {
|
|
3
|
-
allowedApps?: string[];
|
|
4
|
-
allowedEntryOrigins?: string[];
|
|
5
|
-
expectedRuntimeDigests?: Record<string, string>;
|
|
6
|
-
enforceRuntimeDigest?: boolean;
|
|
7
|
-
maxSignalsPerWindow?: number;
|
|
8
|
-
windowMs?: number;
|
|
9
|
-
dedupeWindowMs?: number;
|
|
10
|
-
} | undefined): RuntimeFallbackSignalTrustPolicy;
|
|
11
|
-
export declare function enforceRuntimeFallbackSignalTrustPolicy(payload: Record<string, unknown>, runtimeSignalContext: RuntimeFallbackSignalTrustContext, source?: RuntimeFallbackSignalSource): {
|
|
12
|
-
deduped: boolean;
|
|
13
|
-
};
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { ContractGateSnapshotStore } from '../contract-gate-snapshot-store';
|
|
2
|
-
export type RuntimeSignalErrorCode = 'PAYLOAD_TOO_LARGE' | 'INVALID_PAYLOAD' | 'RATE_LIMITED' | 'UNAUTHORIZED' | 'UNTRUSTED_SOURCE';
|
|
3
|
-
export type RuntimeSignalError = Error & {
|
|
4
|
-
code?: RuntimeSignalErrorCode;
|
|
5
|
-
};
|
|
6
|
-
export type RuntimeFallbackSignalTrustPolicy = {
|
|
7
|
-
allowedApps: string[];
|
|
8
|
-
allowedEntryOrigins: string[];
|
|
9
|
-
expectedRuntimeDigests: Record<string, string>;
|
|
10
|
-
enforceRuntimeDigest: boolean;
|
|
11
|
-
maxSignalsPerWindow: number;
|
|
12
|
-
windowMs: number;
|
|
13
|
-
dedupeWindowMs: number;
|
|
14
|
-
};
|
|
15
|
-
type RuntimeFallbackSignalRateLimitState = {
|
|
16
|
-
count: number;
|
|
17
|
-
windowStartedAt: number;
|
|
18
|
-
};
|
|
19
|
-
export type RuntimeFallbackSignalAuthConfig = {
|
|
20
|
-
enabled: boolean;
|
|
21
|
-
headerName: string;
|
|
22
|
-
expectedValue?: string;
|
|
23
|
-
};
|
|
24
|
-
export type RuntimeFallbackSignalRuntimeState = {
|
|
25
|
-
rateLimitBySource: Map<string, RuntimeFallbackSignalRateLimitState>;
|
|
26
|
-
dedupeByFingerprint: Map<string, number>;
|
|
27
|
-
};
|
|
28
|
-
export type RuntimeFallbackSignalTrustContext = {
|
|
29
|
-
trustPolicy: RuntimeFallbackSignalTrustPolicy;
|
|
30
|
-
runtimeState: RuntimeFallbackSignalRuntimeState;
|
|
31
|
-
};
|
|
32
|
-
export type RuntimeFallbackSignalConfig = {
|
|
33
|
-
endpoint: string;
|
|
34
|
-
gateName: string;
|
|
35
|
-
gateSnapshotStore: Promise<ContractGateSnapshotStore>;
|
|
36
|
-
failureHoldMs: number;
|
|
37
|
-
maxBodyBytes: number;
|
|
38
|
-
auth: RuntimeFallbackSignalAuthConfig;
|
|
39
|
-
trustPolicy: RuntimeFallbackSignalTrustPolicy;
|
|
40
|
-
runtimeState: RuntimeFallbackSignalRuntimeState;
|
|
41
|
-
};
|
|
42
|
-
export type RuntimeFallbackSignalSource = {
|
|
43
|
-
/**
|
|
44
|
-
* Server-trusted connection identity (socket remote address). Never derive
|
|
45
|
-
* this from request headers or the payload: both are attacker-controlled
|
|
46
|
-
* and would let callers reset their own rate-limit budget.
|
|
47
|
-
*/
|
|
48
|
-
remoteAddress?: string;
|
|
49
|
-
};
|
|
50
|
-
export {};
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { createHash, timingSafeEqual } from 'node:crypto';
|
|
2
|
-
import type { Context, ServerEnv } from '@modern-js/server-core';
|
|
3
|
-
import { DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER } from './constants';
|
|
4
|
-
import { createRuntimeSignalError } from './errors';
|
|
5
|
-
import type {
|
|
6
|
-
RuntimeFallbackSignalAuthConfig,
|
|
7
|
-
RuntimeFallbackSignalConfig,
|
|
8
|
-
} from './types';
|
|
9
|
-
|
|
10
|
-
export function normalizeRuntimeFallbackSignalAuthConfig(
|
|
11
|
-
configured:
|
|
12
|
-
| {
|
|
13
|
-
enabled?: boolean;
|
|
14
|
-
headerName?: string;
|
|
15
|
-
expectedValue?: string;
|
|
16
|
-
expectedValueEnv?: string;
|
|
17
|
-
}
|
|
18
|
-
| undefined,
|
|
19
|
-
): RuntimeFallbackSignalAuthConfig {
|
|
20
|
-
const headerName =
|
|
21
|
-
typeof configured?.headerName === 'string' && configured.headerName.trim()
|
|
22
|
-
? configured.headerName.trim().toLowerCase()
|
|
23
|
-
: DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER;
|
|
24
|
-
const expectedFromEnv =
|
|
25
|
-
typeof configured?.expectedValueEnv === 'string' &&
|
|
26
|
-
configured.expectedValueEnv.trim().length > 0
|
|
27
|
-
? process.env[configured.expectedValueEnv.trim()]
|
|
28
|
-
: undefined;
|
|
29
|
-
const expectedFromConfig =
|
|
30
|
-
typeof configured?.expectedValue === 'string' &&
|
|
31
|
-
configured.expectedValue.trim().length > 0
|
|
32
|
-
? configured.expectedValue.trim()
|
|
33
|
-
: undefined;
|
|
34
|
-
const expectedValue = expectedFromConfig || expectedFromEnv;
|
|
35
|
-
const enabled = configured?.enabled === true;
|
|
36
|
-
|
|
37
|
-
if (enabled && !expectedValue) {
|
|
38
|
-
throw new Error(
|
|
39
|
-
'[telemetry.canary.autopilot.runtimeFallbackSignal] auth.enabled is true but no expected token is configured',
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return {
|
|
44
|
-
enabled,
|
|
45
|
-
headerName,
|
|
46
|
-
expectedValue,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Normalizes the auth config for the runtime fallback signal endpoint when the
|
|
52
|
-
* endpoint itself is enabled. The endpoint can persist failing contract gates
|
|
53
|
-
* (a canary kill switch), so it always requires a token: auth cannot be
|
|
54
|
-
* disabled and a token must be configured via `auth.expectedValue` or
|
|
55
|
-
* `auth.expectedValueEnv`.
|
|
56
|
-
*/
|
|
57
|
-
export function normalizeRequiredRuntimeFallbackSignalAuthConfig(
|
|
58
|
-
configured: Parameters<typeof normalizeRuntimeFallbackSignalAuthConfig>[0],
|
|
59
|
-
): RuntimeFallbackSignalAuthConfig {
|
|
60
|
-
if (configured?.enabled === false) {
|
|
61
|
-
throw new Error(
|
|
62
|
-
'[telemetry.canary.autopilot.runtimeFallbackSignal] the endpoint cannot be enabled with auth disabled; configure auth.expectedValue or auth.expectedValueEnv',
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
try {
|
|
67
|
-
return normalizeRuntimeFallbackSignalAuthConfig({
|
|
68
|
-
...configured,
|
|
69
|
-
enabled: true,
|
|
70
|
-
});
|
|
71
|
-
} catch (_error) {
|
|
72
|
-
throw new Error(
|
|
73
|
-
'[telemetry.canary.autopilot.runtimeFallbackSignal] enabling the endpoint requires an auth token; configure auth.expectedValue or auth.expectedValueEnv',
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Constant-time token comparison. Both sides are hashed first so neither the
|
|
80
|
-
* comparison time nor the early length check leaks information about the
|
|
81
|
-
* expected secret.
|
|
82
|
-
*/
|
|
83
|
-
function safeTokenEquals(candidate: string, expected: string) {
|
|
84
|
-
const candidateDigest = createHash('sha256').update(candidate).digest();
|
|
85
|
-
const expectedDigest = createHash('sha256').update(expected).digest();
|
|
86
|
-
return timingSafeEqual(candidateDigest, expectedDigest);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function enforceRuntimeFallbackSignalAuthToken(
|
|
90
|
-
token: string | undefined,
|
|
91
|
-
authConfig: RuntimeFallbackSignalAuthConfig,
|
|
92
|
-
) {
|
|
93
|
-
if (!authConfig.enabled) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (
|
|
98
|
-
!token ||
|
|
99
|
-
!authConfig.expectedValue ||
|
|
100
|
-
!safeTokenEquals(token, authConfig.expectedValue)
|
|
101
|
-
) {
|
|
102
|
-
throw createRuntimeSignalError(
|
|
103
|
-
'runtime fallback signal auth failed',
|
|
104
|
-
'UNAUTHORIZED',
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function enforceRuntimeFallbackSignalAuth(
|
|
110
|
-
c: Context<ServerEnv>,
|
|
111
|
-
runtimeSignalConfig: RuntimeFallbackSignalConfig,
|
|
112
|
-
) {
|
|
113
|
-
enforceRuntimeFallbackSignalAuthToken(
|
|
114
|
-
c.req.header(runtimeSignalConfig.auth.headerName),
|
|
115
|
-
runtimeSignalConfig.auth,
|
|
116
|
-
);
|
|
117
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export const DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT =
|
|
2
|
-
'/_modern/contract-gates/runtime-fallback';
|
|
3
|
-
|
|
4
|
-
export const DEFAULT_RUNTIME_STATUS_ENDPOINT = '/_modern/runtime/status';
|
|
5
|
-
|
|
6
|
-
export const DEFAULT_RUNTIME_FALLBACK_GATE_NAME = 'runtime-mf-fallback-health';
|
|
7
|
-
|
|
8
|
-
export const DEFAULT_RUNTIME_FALLBACK_FAILURE_HOLD_MS = 5 * 60_000;
|
|
9
|
-
|
|
10
|
-
export const DEFAULT_RUNTIME_FALLBACK_MAX_BODY_BYTES = 16 * 1024;
|
|
11
|
-
|
|
12
|
-
export const DEFAULT_RUNTIME_FALLBACK_AUTH_HEADER =
|
|
13
|
-
'x-modernjs-runtime-signal-token';
|
|
14
|
-
|
|
15
|
-
export const DEFAULT_RUNTIME_FALLBACK_TRUST_MAX_SIGNALS_PER_WINDOW = 30;
|
|
16
|
-
|
|
17
|
-
export const DEFAULT_RUNTIME_FALLBACK_TRUST_WINDOW_MS = 60_000;
|
|
18
|
-
|
|
19
|
-
export const DEFAULT_RUNTIME_FALLBACK_TRUST_DEDUPE_WINDOW_MS = 10_000;
|