@daloyjs/core 1.0.0-rc.0 → 1.0.0-rc.2
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 +11 -9
- package/bin/daloy.mjs +38 -10
- package/dist/app.d.ts +21 -8
- package/dist/app.js +71 -12
- package/dist/cli.d.ts +5 -2
- package/dist/cli.js +9 -5
- package/dist/http-signatures.js +44 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/ip-reputation.d.ts +8 -2
- package/dist/ip-reputation.js +7 -1
- package/dist/jwk.js +6 -1
- package/dist/mcp.d.ts +80 -9
- package/dist/mcp.js +206 -4
- package/dist/middleware.d.ts +38 -0
- package/dist/middleware.js +45 -4
- package/dist/mtls.js +6 -1
- package/dist/sbom.cdx.json +9 -9
- package/dist/sbom.spdx.json +5 -5
- package/dist/types.d.ts +4 -1
- package/package.json +1 -1
package/dist/jwk.js
CHANGED
|
@@ -235,7 +235,7 @@ export function jwk(opts) {
|
|
|
235
235
|
});
|
|
236
236
|
return cachedVerifier;
|
|
237
237
|
}
|
|
238
|
-
|
|
238
|
+
const authHooks = {
|
|
239
239
|
async beforeHandle(ctx) {
|
|
240
240
|
const header = ctx.request.headers.get("authorization") ?? "";
|
|
241
241
|
const match = /^Bearer\s+(.+)$/i.exec(header);
|
|
@@ -268,6 +268,11 @@ export function jwk(opts) {
|
|
|
268
268
|
return undefined;
|
|
269
269
|
},
|
|
270
270
|
};
|
|
271
|
+
// Same global symbol as middleware's AUTH_HOOK_MARKER (stamped inline to keep
|
|
272
|
+
// the middleware module out of jwk's bundle): lets the route-auth boot guard
|
|
273
|
+
// recognize that a route declaring `auth:` is actually enforced here.
|
|
274
|
+
authHooks[Symbol.for("daloyjs.auth.hook")] = true;
|
|
275
|
+
return authHooks;
|
|
271
276
|
}
|
|
272
277
|
function extractScopes(payload) {
|
|
273
278
|
// RFC 8693 / OAuth2: `scope` is a space-delimited string; some IdPs emit
|
package/dist/mcp.d.ts
CHANGED
|
@@ -39,9 +39,16 @@ export type McpJsonObject = {
|
|
|
39
39
|
};
|
|
40
40
|
/**
|
|
41
41
|
* JSON Schema fragment advertised to MCP clients for a tool or prompt
|
|
42
|
-
* argument object.
|
|
43
|
-
*
|
|
44
|
-
*
|
|
42
|
+
* argument object.
|
|
43
|
+
*
|
|
44
|
+
* For a tool's `inputSchema`, DaloyJS enforces the commonly-used,
|
|
45
|
+
* security-relevant subset of JSON Schema server-side (see
|
|
46
|
+
* {@link validateMcpInput}) BEFORE the tool handler runs, rejecting a
|
|
47
|
+
* `tools/call` whose arguments violate it with JSON-RPC `-32602`. Keywords
|
|
48
|
+
* outside that subset (`pattern`, `format`, `$ref`,
|
|
49
|
+
* `anyOf`/`oneOf`/`allOf`, …) are advertised to clients but NOT enforced —
|
|
50
|
+
* validate any constraint expressed only through those keywords inside your
|
|
51
|
+
* handler before touching databases, files, or remote services.
|
|
45
52
|
*
|
|
46
53
|
* @since 1.0.0
|
|
47
54
|
*/
|
|
@@ -195,8 +202,11 @@ export interface McpToolAnnotations {
|
|
|
195
202
|
* Handler for a single MCP tool.
|
|
196
203
|
*
|
|
197
204
|
* @typeParam TArgs - Type expected in `params.arguments` for this tool.
|
|
198
|
-
* @param args - Tool arguments supplied by the MCP client. They
|
|
199
|
-
*
|
|
205
|
+
* @param args - Tool arguments supplied by the MCP client. They have already
|
|
206
|
+
* been validated against this tool's `inputSchema` (enforced subset — see
|
|
207
|
+
* {@link validateMcpInput}) and had prototype-pollution keys stripped, so the
|
|
208
|
+
* declared shape holds at runtime. Constraints expressed only through
|
|
209
|
+
* unsupported schema keywords (e.g. `pattern`) remain the handler's job.
|
|
200
210
|
* @param ctx - Request metadata and the original HTTP request.
|
|
201
211
|
* @returns Text shorthand or a full {@link McpToolResult}.
|
|
202
212
|
* @throws {McpToolError} for caller-correctable failures that should be
|
|
@@ -209,9 +219,11 @@ export type McpToolHandler<TArgs extends Record<string, unknown> = Record<string
|
|
|
209
219
|
* Definition of a callable MCP tool.
|
|
210
220
|
*
|
|
211
221
|
* Tools are model-controlled in MCP: clients may let the language model decide
|
|
212
|
-
* when to call them. Treat every tool as a public API operation
|
|
213
|
-
*
|
|
214
|
-
*
|
|
222
|
+
* when to call them. Treat every tool as a public API operation. DaloyJS
|
|
223
|
+
* enforces the tool's `inputSchema` (enforced subset — see
|
|
224
|
+
* {@link validateMcpInput}) before the handler runs; you remain responsible for
|
|
225
|
+
* authentication, authorization, rate limits, and any validation beyond that
|
|
226
|
+
* subset before side effects.
|
|
215
227
|
*
|
|
216
228
|
* @typeParam TArgs - Type expected by this tool's handler.
|
|
217
229
|
* @since 1.0.0
|
|
@@ -492,6 +504,31 @@ export interface McpHandlerOptions {
|
|
|
492
504
|
* @since 1.0.0
|
|
493
505
|
*/
|
|
494
506
|
export type McpHandler = (request: Request) => Promise<Response>;
|
|
507
|
+
/**
|
|
508
|
+
* Minimal, dependency-free JSON Schema validator for MCP tool arguments.
|
|
509
|
+
*
|
|
510
|
+
* DaloyJS core bundles no third-party schema library, so this implements the
|
|
511
|
+
* commonly-used, security-relevant subset of JSON Schema — enough to reject the
|
|
512
|
+
* untrusted `tools/call` argument shapes that matter before a tool handler
|
|
513
|
+
* runs: wrong `type` (including `integer`), missing `required` properties,
|
|
514
|
+
* unexpected keys under `additionalProperties: false`, `enum`/`const`
|
|
515
|
+
* violations, and basic string/number/array bounds (`minLength`/`maxLength`,
|
|
516
|
+
* `minimum`/`maximum`, `minItems`/`maxItems`). Nested `properties`, `items`,
|
|
517
|
+
* and object-form `additionalProperties` are validated recursively.
|
|
518
|
+
*
|
|
519
|
+
* Keywords outside this subset (`pattern`, `format`, `$ref`,
|
|
520
|
+
* `anyOf`/`oneOf`/`allOf`, etc.) are intentionally NOT enforced — notably
|
|
521
|
+
* `pattern` is skipped so a developer-authored regex can never become a ReDoS
|
|
522
|
+
* sink against attacker-controlled input. Handlers must still validate any
|
|
523
|
+
* constraint expressed only through those keywords.
|
|
524
|
+
*
|
|
525
|
+
* @param schema - The tool's advertised `inputSchema`.
|
|
526
|
+
* @param value - The untrusted `params.arguments` value from the client.
|
|
527
|
+
* @returns A list of human-readable validation errors; empty when the value
|
|
528
|
+
* satisfies the enforced subset of the schema.
|
|
529
|
+
* @since 1.0.0
|
|
530
|
+
*/
|
|
531
|
+
export declare function validateMcpInput(schema: McpJsonSchema, value: unknown): string[];
|
|
495
532
|
/**
|
|
496
533
|
* Create a dependency-free MCP Streamable HTTP endpoint handler.
|
|
497
534
|
*
|
|
@@ -544,6 +581,25 @@ export type McpHandler = (request: Request) => Promise<Response>;
|
|
|
544
581
|
* @since 1.0.0
|
|
545
582
|
*/
|
|
546
583
|
export declare function createMcpHandler(options: McpHandlerOptions): McpHandler;
|
|
584
|
+
/**
|
|
585
|
+
* Options for {@link mcpRoutes}.
|
|
586
|
+
*
|
|
587
|
+
* @since 1.0.0
|
|
588
|
+
*/
|
|
589
|
+
export interface McpRoutesOptions {
|
|
590
|
+
/**
|
|
591
|
+
* Set `true` to intentionally expose the MCP endpoint WITHOUT authentication,
|
|
592
|
+
* opting the `POST` transport out of the App's production route-auth boot
|
|
593
|
+
* guard. Only do this for a genuinely public MCP server — MCP tools are
|
|
594
|
+
* model-controlled and can trigger side effects, so an unauthenticated
|
|
595
|
+
* endpoint is a high-impact default. When left `false` (the default), a
|
|
596
|
+
* production `secureDefaults` App refuses to boot unless an authentication
|
|
597
|
+
* hook covers the MCP route.
|
|
598
|
+
*
|
|
599
|
+
* @defaultValue false
|
|
600
|
+
*/
|
|
601
|
+
public?: boolean;
|
|
602
|
+
}
|
|
547
603
|
/**
|
|
548
604
|
* Build the Daloy route definitions for a Streamable HTTP MCP endpoint.
|
|
549
605
|
*
|
|
@@ -552,8 +608,16 @@ export declare function createMcpHandler(options: McpHandlerOptions): McpHandler
|
|
|
552
608
|
* its public contract and auth policy, while the MCP server can use its own
|
|
553
609
|
* bearer token, rate limit, network allowlist, and tool set.
|
|
554
610
|
*
|
|
611
|
+
* By default the `POST` transport route is stamped so that a production
|
|
612
|
+
* `secureDefaults` App **refuses to boot** unless an authentication hook covers
|
|
613
|
+
* it — MCP tools are model-controlled and side-effecting. Cover the route with
|
|
614
|
+
* an auth middleware (e.g. `app.use(bearerAuth({ ... }))`), or pass
|
|
615
|
+
* `{ public: true }` to intentionally expose a public MCP server.
|
|
616
|
+
*
|
|
555
617
|
* @param path - Public MCP endpoint path, usually `"/mcp"`.
|
|
556
618
|
* @param handler - Handler returned by {@link createMcpHandler}.
|
|
619
|
+
* @param options - See {@link McpRoutesOptions}; pass `{ public: true }` to opt
|
|
620
|
+
* out of the auth boot guard.
|
|
557
621
|
* @returns Route definitions for `POST`, `GET`, and `OPTIONS` on the same
|
|
558
622
|
* path. `POST` is the actual MCP transport; `GET` gives a human-readable
|
|
559
623
|
* 405 hint because this helper does not open server-initiated SSE streams;
|
|
@@ -564,11 +628,18 @@ export declare function createMcpHandler(options: McpHandlerOptions): McpHandler
|
|
|
564
628
|
* const app = new App();
|
|
565
629
|
* const mcp = createMcpHandler({ serverInfo, tools });
|
|
566
630
|
*
|
|
631
|
+
* // Authenticated MCP server (satisfies the production boot guard):
|
|
632
|
+
* app.use(bearerAuth({ validate: (t) => timingSafeEqual(t, process.env.MCP_TOKEN!) }));
|
|
567
633
|
* for (const route of mcpRoutes("/mcp", mcp)) {
|
|
568
634
|
* app.route(route);
|
|
569
635
|
* }
|
|
636
|
+
*
|
|
637
|
+
* // ...or an intentionally public MCP server:
|
|
638
|
+
* for (const route of mcpRoutes("/mcp", mcp, { public: true })) {
|
|
639
|
+
* app.route(route);
|
|
640
|
+
* }
|
|
570
641
|
* ```
|
|
571
642
|
*
|
|
572
643
|
* @since 1.0.0
|
|
573
644
|
*/
|
|
574
|
-
export declare function mcpRoutes(path: PathString, handler: McpHandler): RouteDefinition<PathString, "GET" | "POST" | "OPTIONS">[];
|
|
645
|
+
export declare function mcpRoutes(path: PathString, handler: McpHandler, options?: McpRoutesOptions): RouteDefinition<PathString, "GET" | "POST" | "OPTIONS">[];
|
package/dist/mcp.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { safeJsonParse } from "./security.js";
|
|
1
2
|
/**
|
|
2
3
|
* Latest MCP protocol version DaloyJS negotiates by default.
|
|
3
4
|
*
|
|
@@ -125,6 +126,165 @@ function asRecord(value) {
|
|
|
125
126
|
? value
|
|
126
127
|
: {};
|
|
127
128
|
}
|
|
129
|
+
/** Hard cap on reported validation errors so a hostile payload can't inflate the response. */
|
|
130
|
+
const MAX_MCP_VALIDATION_ERRORS = 20;
|
|
131
|
+
/** Recursion-depth cap so a deeply-nested payload can't exhaust the stack. */
|
|
132
|
+
const MAX_MCP_SCHEMA_DEPTH = 64;
|
|
133
|
+
/** Narrow an arbitrary JSON value to a schema object (`{}`), excluding arrays/null. */
|
|
134
|
+
function isSchemaObject(v) {
|
|
135
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
136
|
+
}
|
|
137
|
+
/** Report the JSON type of a value using JSON Schema's type names. */
|
|
138
|
+
function jsonTypeOf(v) {
|
|
139
|
+
if (v === null)
|
|
140
|
+
return "null";
|
|
141
|
+
if (Array.isArray(v))
|
|
142
|
+
return "array";
|
|
143
|
+
return typeof v;
|
|
144
|
+
}
|
|
145
|
+
/** Test a value against a single JSON Schema `type` keyword. */
|
|
146
|
+
function matchesJsonType(type, value) {
|
|
147
|
+
switch (type) {
|
|
148
|
+
case "integer":
|
|
149
|
+
return typeof value === "number" && Number.isInteger(value);
|
|
150
|
+
case "number":
|
|
151
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
152
|
+
case "string":
|
|
153
|
+
return typeof value === "string";
|
|
154
|
+
case "boolean":
|
|
155
|
+
return typeof value === "boolean";
|
|
156
|
+
case "null":
|
|
157
|
+
return value === null;
|
|
158
|
+
case "object":
|
|
159
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
160
|
+
case "array":
|
|
161
|
+
return Array.isArray(value);
|
|
162
|
+
default:
|
|
163
|
+
// Unknown type keyword — do not reject; treat as unconstrained.
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/** Structural equality for `enum`/`const` comparison (sufficient for JSON scalars/objects). */
|
|
168
|
+
function deepEqualJson(a, b) {
|
|
169
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
170
|
+
}
|
|
171
|
+
/** Recursive worker for {@link validateMcpInput}. Pushes human-readable errors into `errors`. */
|
|
172
|
+
function validateSchemaNode(schema, value, path, errors, depth) {
|
|
173
|
+
if (errors.length >= MAX_MCP_VALIDATION_ERRORS)
|
|
174
|
+
return;
|
|
175
|
+
if (depth > MAX_MCP_SCHEMA_DEPTH) {
|
|
176
|
+
errors.push(`${path}: exceeds maximum validation depth`);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
// type (string or array-of-strings). A type mismatch stops deeper,
|
|
180
|
+
// type-dependent checks for this node to avoid a cascade of noise.
|
|
181
|
+
const typeKw = schema.type;
|
|
182
|
+
if (typeof typeKw === "string") {
|
|
183
|
+
if (!matchesJsonType(typeKw, value)) {
|
|
184
|
+
errors.push(`${path}: expected ${typeKw}, got ${jsonTypeOf(value)}`);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
else if (Array.isArray(typeKw)) {
|
|
189
|
+
const types = typeKw.filter((t) => typeof t === "string");
|
|
190
|
+
if (types.length > 0 && !types.some((t) => matchesJsonType(t, value))) {
|
|
191
|
+
errors.push(`${path}: expected one of [${types.join(", ")}], got ${jsonTypeOf(value)}`);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (Array.isArray(schema.enum) && !schema.enum.some((e) => deepEqualJson(e, value))) {
|
|
196
|
+
errors.push(`${path}: value is not one of the allowed enum values`);
|
|
197
|
+
}
|
|
198
|
+
if ("const" in schema && !deepEqualJson(schema.const, value)) {
|
|
199
|
+
errors.push(`${path}: value does not equal the required constant`);
|
|
200
|
+
}
|
|
201
|
+
if (typeof value === "string") {
|
|
202
|
+
if (typeof schema.minLength === "number" && value.length < schema.minLength) {
|
|
203
|
+
errors.push(`${path}: string shorter than minLength ${schema.minLength}`);
|
|
204
|
+
}
|
|
205
|
+
if (typeof schema.maxLength === "number" && value.length > schema.maxLength) {
|
|
206
|
+
errors.push(`${path}: string longer than maxLength ${schema.maxLength}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (typeof value === "number") {
|
|
210
|
+
if (typeof schema.minimum === "number" && value < schema.minimum) {
|
|
211
|
+
errors.push(`${path}: number below minimum ${schema.minimum}`);
|
|
212
|
+
}
|
|
213
|
+
if (typeof schema.maximum === "number" && value > schema.maximum) {
|
|
214
|
+
errors.push(`${path}: number above maximum ${schema.maximum}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (Array.isArray(value)) {
|
|
218
|
+
if (typeof schema.minItems === "number" && value.length < schema.minItems) {
|
|
219
|
+
errors.push(`${path}: array has fewer than minItems ${schema.minItems}`);
|
|
220
|
+
}
|
|
221
|
+
if (typeof schema.maxItems === "number" && value.length > schema.maxItems) {
|
|
222
|
+
errors.push(`${path}: array has more than maxItems ${schema.maxItems}`);
|
|
223
|
+
}
|
|
224
|
+
if (isSchemaObject(schema.items)) {
|
|
225
|
+
for (let i = 0; i < value.length; i++) {
|
|
226
|
+
validateSchemaNode(schema.items, value[i], `${path}[${i}]`, errors, depth + 1);
|
|
227
|
+
if (errors.length >= MAX_MCP_VALIDATION_ERRORS)
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
233
|
+
const obj = value;
|
|
234
|
+
const props = isSchemaObject(schema.properties) ? schema.properties : undefined;
|
|
235
|
+
if (Array.isArray(schema.required)) {
|
|
236
|
+
for (const req of schema.required) {
|
|
237
|
+
if (typeof req === "string" && !Object.prototype.hasOwnProperty.call(obj, req)) {
|
|
238
|
+
errors.push(`${path}.${req}: required property is missing`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const addl = schema.additionalProperties;
|
|
243
|
+
for (const key of Object.keys(obj)) {
|
|
244
|
+
const sub = props && isSchemaObject(props[key]) ? props[key] : undefined;
|
|
245
|
+
if (sub) {
|
|
246
|
+
validateSchemaNode(sub, obj[key], `${path}.${key}`, errors, depth + 1);
|
|
247
|
+
}
|
|
248
|
+
else if (addl === false) {
|
|
249
|
+
errors.push(`${path}.${key}: unexpected property (additionalProperties is false)`);
|
|
250
|
+
}
|
|
251
|
+
else if (isSchemaObject(addl)) {
|
|
252
|
+
validateSchemaNode(addl, obj[key], `${path}.${key}`, errors, depth + 1);
|
|
253
|
+
}
|
|
254
|
+
if (errors.length >= MAX_MCP_VALIDATION_ERRORS)
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Minimal, dependency-free JSON Schema validator for MCP tool arguments.
|
|
261
|
+
*
|
|
262
|
+
* DaloyJS core bundles no third-party schema library, so this implements the
|
|
263
|
+
* commonly-used, security-relevant subset of JSON Schema — enough to reject the
|
|
264
|
+
* untrusted `tools/call` argument shapes that matter before a tool handler
|
|
265
|
+
* runs: wrong `type` (including `integer`), missing `required` properties,
|
|
266
|
+
* unexpected keys under `additionalProperties: false`, `enum`/`const`
|
|
267
|
+
* violations, and basic string/number/array bounds (`minLength`/`maxLength`,
|
|
268
|
+
* `minimum`/`maximum`, `minItems`/`maxItems`). Nested `properties`, `items`,
|
|
269
|
+
* and object-form `additionalProperties` are validated recursively.
|
|
270
|
+
*
|
|
271
|
+
* Keywords outside this subset (`pattern`, `format`, `$ref`,
|
|
272
|
+
* `anyOf`/`oneOf`/`allOf`, etc.) are intentionally NOT enforced — notably
|
|
273
|
+
* `pattern` is skipped so a developer-authored regex can never become a ReDoS
|
|
274
|
+
* sink against attacker-controlled input. Handlers must still validate any
|
|
275
|
+
* constraint expressed only through those keywords.
|
|
276
|
+
*
|
|
277
|
+
* @param schema - The tool's advertised `inputSchema`.
|
|
278
|
+
* @param value - The untrusted `params.arguments` value from the client.
|
|
279
|
+
* @returns A list of human-readable validation errors; empty when the value
|
|
280
|
+
* satisfies the enforced subset of the schema.
|
|
281
|
+
* @since 1.0.0
|
|
282
|
+
*/
|
|
283
|
+
export function validateMcpInput(schema, value) {
|
|
284
|
+
const errors = [];
|
|
285
|
+
validateSchemaNode(schema, value, "arguments", errors, 0);
|
|
286
|
+
return errors;
|
|
287
|
+
}
|
|
128
288
|
function publicTool(tool) {
|
|
129
289
|
const { handler: _handler, ...rest } = tool;
|
|
130
290
|
return rest;
|
|
@@ -382,8 +542,18 @@ export function createMcpHandler(options) {
|
|
|
382
542
|
if (!tool) {
|
|
383
543
|
return rpcError(id, INVALID_PARAMS, `Unknown tool: ${name || "<missing>"}`, undefined, 200, headers);
|
|
384
544
|
}
|
|
545
|
+
// Enforce the tool's advertised inputSchema on the untrusted client
|
|
546
|
+
// arguments BEFORE the handler runs, so a handler is never handed a
|
|
547
|
+
// payload that violates its own contract (wrong types, missing required
|
|
548
|
+
// fields, unexpected keys). Protocol-level validation failures map to
|
|
549
|
+
// JSON-RPC -32602 (Invalid params).
|
|
550
|
+
const rawArgs = params.arguments === undefined ? {} : params.arguments;
|
|
551
|
+
const validationErrors = validateMcpInput(tool.inputSchema, rawArgs);
|
|
552
|
+
if (validationErrors.length > 0) {
|
|
553
|
+
return rpcError(id, INVALID_PARAMS, `Invalid arguments for tool "${name}": ${validationErrors[0]}`, { validationErrors }, 200, headers);
|
|
554
|
+
}
|
|
385
555
|
try {
|
|
386
|
-
const result = await tool.handler(asRecord(
|
|
556
|
+
const result = await tool.handler(asRecord(rawArgs), ctx);
|
|
387
557
|
return rpcResult(id, normalizeToolResult(result), headers);
|
|
388
558
|
}
|
|
389
559
|
catch (error) {
|
|
@@ -523,7 +693,11 @@ export function createMcpHandler(options) {
|
|
|
523
693
|
}
|
|
524
694
|
let message;
|
|
525
695
|
try {
|
|
526
|
-
|
|
696
|
+
// `safeJsonParse` strips `__proto__` / `constructor` / `prototype` keys so
|
|
697
|
+
// an untrusted MCP client cannot smuggle prototype-pollution-shaped keys
|
|
698
|
+
// into a tool handler's arguments — matching the REST body parsers'
|
|
699
|
+
// secure-by-default posture (see `safeJsonParse` in security.ts).
|
|
700
|
+
message = safeJsonParse(raw);
|
|
527
701
|
}
|
|
528
702
|
catch {
|
|
529
703
|
return rpcError(null, PARSE_ERROR, "Invalid JSON in request body.", undefined, 400, headers);
|
|
@@ -565,8 +739,16 @@ export function createMcpHandler(options) {
|
|
|
565
739
|
* its public contract and auth policy, while the MCP server can use its own
|
|
566
740
|
* bearer token, rate limit, network allowlist, and tool set.
|
|
567
741
|
*
|
|
742
|
+
* By default the `POST` transport route is stamped so that a production
|
|
743
|
+
* `secureDefaults` App **refuses to boot** unless an authentication hook covers
|
|
744
|
+
* it — MCP tools are model-controlled and side-effecting. Cover the route with
|
|
745
|
+
* an auth middleware (e.g. `app.use(bearerAuth({ ... }))`), or pass
|
|
746
|
+
* `{ public: true }` to intentionally expose a public MCP server.
|
|
747
|
+
*
|
|
568
748
|
* @param path - Public MCP endpoint path, usually `"/mcp"`.
|
|
569
749
|
* @param handler - Handler returned by {@link createMcpHandler}.
|
|
750
|
+
* @param options - See {@link McpRoutesOptions}; pass `{ public: true }` to opt
|
|
751
|
+
* out of the auth boot guard.
|
|
570
752
|
* @returns Route definitions for `POST`, `GET`, and `OPTIONS` on the same
|
|
571
753
|
* path. `POST` is the actual MCP transport; `GET` gives a human-readable
|
|
572
754
|
* 405 hint because this helper does not open server-initiated SSE streams;
|
|
@@ -577,14 +759,21 @@ export function createMcpHandler(options) {
|
|
|
577
759
|
* const app = new App();
|
|
578
760
|
* const mcp = createMcpHandler({ serverInfo, tools });
|
|
579
761
|
*
|
|
762
|
+
* // Authenticated MCP server (satisfies the production boot guard):
|
|
763
|
+
* app.use(bearerAuth({ validate: (t) => timingSafeEqual(t, process.env.MCP_TOKEN!) }));
|
|
580
764
|
* for (const route of mcpRoutes("/mcp", mcp)) {
|
|
581
765
|
* app.route(route);
|
|
582
766
|
* }
|
|
767
|
+
*
|
|
768
|
+
* // ...or an intentionally public MCP server:
|
|
769
|
+
* for (const route of mcpRoutes("/mcp", mcp, { public: true })) {
|
|
770
|
+
* app.route(route);
|
|
771
|
+
* }
|
|
583
772
|
* ```
|
|
584
773
|
*
|
|
585
774
|
* @since 1.0.0
|
|
586
775
|
*/
|
|
587
|
-
export function mcpRoutes(path, handler) {
|
|
776
|
+
export function mcpRoutes(path, handler, options = {}) {
|
|
588
777
|
const responses = {
|
|
589
778
|
200: { description: "MCP JSON-RPC response", body: MCP_JSON_RESPONSE_SCHEMA },
|
|
590
779
|
202: { description: "MCP notification accepted", body: MCP_JSON_RESPONSE_SCHEMA },
|
|
@@ -594,7 +783,7 @@ export function mcpRoutes(path, handler) {
|
|
|
594
783
|
405: { description: "Unsupported MCP transport method" },
|
|
595
784
|
413: { description: "MCP request body too large" },
|
|
596
785
|
};
|
|
597
|
-
|
|
786
|
+
const routes = [
|
|
598
787
|
{
|
|
599
788
|
method: "POST",
|
|
600
789
|
path,
|
|
@@ -620,4 +809,17 @@ export function mcpRoutes(path, handler) {
|
|
|
620
809
|
handler: ({ request }) => handler(request),
|
|
621
810
|
},
|
|
622
811
|
];
|
|
812
|
+
// Unless explicitly public, stamp the POST transport (the route that executes
|
|
813
|
+
// tools/call) with the global-registry marker the App boot guard reads. GET
|
|
814
|
+
// (405 hint) and OPTIONS (preflight) are not marked: preflight must stay
|
|
815
|
+
// credential-free. Uses the same string as app.ts's MCP_ROUTE_MARKER; kept as
|
|
816
|
+
// a bare Symbol.for so the App core never imports this module.
|
|
817
|
+
if (options.public !== true) {
|
|
818
|
+
for (const route of routes) {
|
|
819
|
+
if (route.method === "POST") {
|
|
820
|
+
route[Symbol.for("daloyjs.mcp.route")] = true;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
return routes;
|
|
623
825
|
}
|
package/dist/middleware.d.ts
CHANGED
|
@@ -314,6 +314,44 @@ export declare const CORS_WILDCARD_ORIGIN_MARKER: unique symbol;
|
|
|
314
314
|
* @since 0.17.0
|
|
315
315
|
*/
|
|
316
316
|
export declare const CSRF_HOOK_MARKER: unique symbol;
|
|
317
|
+
/**
|
|
318
|
+
* Marker stamped on a {@link Hooks} bundle that authenticates the request —
|
|
319
|
+
* i.e. rejects callers without valid credentials. Built-in auth middlewares
|
|
320
|
+
* (`bearerAuth`, `basicAuth`, `jwk`, `httpSignatureAuth`, `clientCertAuth`)
|
|
321
|
+
* stamp it so the framework's route-auth boot guard can confirm that any route
|
|
322
|
+
* declaring an `auth:` requirement is actually enforced by a hook rather than
|
|
323
|
+
* being silently public (a `security` entry in the OpenAPI doc with no runtime
|
|
324
|
+
* check). Wrap a custom authentication hook with {@link markAuthHook} to opt it
|
|
325
|
+
* into the same guard.
|
|
326
|
+
*
|
|
327
|
+
* @since 1.0.0
|
|
328
|
+
*/
|
|
329
|
+
export declare const AUTH_HOOK_MARKER: unique symbol;
|
|
330
|
+
/**
|
|
331
|
+
* Mark a custom {@link Hooks} bundle as performing request authentication.
|
|
332
|
+
*
|
|
333
|
+
* Use this when you authenticate with your own hook (not one of the built-in
|
|
334
|
+
* auth middlewares) but still declare `auth:` on the protected routes: it
|
|
335
|
+
* stamps {@link AUTH_HOOK_MARKER} so the production route-auth boot guard treats
|
|
336
|
+
* those routes as enforced. It is also the correct escape hatch when
|
|
337
|
+
* authentication is performed by an upstream gateway/mesh and the in-app hook
|
|
338
|
+
* is intentionally a pass-through.
|
|
339
|
+
*
|
|
340
|
+
* @param hooks - The hook bundle to mark (mutated in place and returned).
|
|
341
|
+
* @returns The same `hooks` object, now stamped as an auth hook.
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* ```ts
|
|
345
|
+
* app.use(markAuthHook({
|
|
346
|
+
* async beforeHandle(ctx) {
|
|
347
|
+
* if (!(await myVerify(ctx.request))) throw new UnauthorizedError();
|
|
348
|
+
* },
|
|
349
|
+
* }));
|
|
350
|
+
* ```
|
|
351
|
+
*
|
|
352
|
+
* @since 1.0.0
|
|
353
|
+
*/
|
|
354
|
+
export declare function markAuthHook(hooks: Hooks): Hooks;
|
|
317
355
|
/** Predicate stamped on a CORS `Hooks` object that returns `true` for allowed origins. */
|
|
318
356
|
export type CorsOriginAllow = (origin: string) => boolean;
|
|
319
357
|
/** Options for {@link cors}. */
|
package/dist/middleware.js
CHANGED
|
@@ -447,6 +447,47 @@ export const CORS_WILDCARD_ORIGIN_MARKER = Symbol.for("daloyjs.middleware.cors.w
|
|
|
447
447
|
* @since 0.17.0
|
|
448
448
|
*/
|
|
449
449
|
export const CSRF_HOOK_MARKER = Symbol.for("daloyjs.middleware.csrf");
|
|
450
|
+
/**
|
|
451
|
+
* Marker stamped on a {@link Hooks} bundle that authenticates the request —
|
|
452
|
+
* i.e. rejects callers without valid credentials. Built-in auth middlewares
|
|
453
|
+
* (`bearerAuth`, `basicAuth`, `jwk`, `httpSignatureAuth`, `clientCertAuth`)
|
|
454
|
+
* stamp it so the framework's route-auth boot guard can confirm that any route
|
|
455
|
+
* declaring an `auth:` requirement is actually enforced by a hook rather than
|
|
456
|
+
* being silently public (a `security` entry in the OpenAPI doc with no runtime
|
|
457
|
+
* check). Wrap a custom authentication hook with {@link markAuthHook} to opt it
|
|
458
|
+
* into the same guard.
|
|
459
|
+
*
|
|
460
|
+
* @since 1.0.0
|
|
461
|
+
*/
|
|
462
|
+
export const AUTH_HOOK_MARKER = Symbol.for("daloyjs.auth.hook");
|
|
463
|
+
/**
|
|
464
|
+
* Mark a custom {@link Hooks} bundle as performing request authentication.
|
|
465
|
+
*
|
|
466
|
+
* Use this when you authenticate with your own hook (not one of the built-in
|
|
467
|
+
* auth middlewares) but still declare `auth:` on the protected routes: it
|
|
468
|
+
* stamps {@link AUTH_HOOK_MARKER} so the production route-auth boot guard treats
|
|
469
|
+
* those routes as enforced. It is also the correct escape hatch when
|
|
470
|
+
* authentication is performed by an upstream gateway/mesh and the in-app hook
|
|
471
|
+
* is intentionally a pass-through.
|
|
472
|
+
*
|
|
473
|
+
* @param hooks - The hook bundle to mark (mutated in place and returned).
|
|
474
|
+
* @returns The same `hooks` object, now stamped as an auth hook.
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
477
|
+
* ```ts
|
|
478
|
+
* app.use(markAuthHook({
|
|
479
|
+
* async beforeHandle(ctx) {
|
|
480
|
+
* if (!(await myVerify(ctx.request))) throw new UnauthorizedError();
|
|
481
|
+
* },
|
|
482
|
+
* }));
|
|
483
|
+
* ```
|
|
484
|
+
*
|
|
485
|
+
* @since 1.0.0
|
|
486
|
+
*/
|
|
487
|
+
export function markAuthHook(hooks) {
|
|
488
|
+
hooks[AUTH_HOOK_MARKER] = true;
|
|
489
|
+
return hooks;
|
|
490
|
+
}
|
|
450
491
|
/**
|
|
451
492
|
* Cross-Origin Resource Sharing (CORS) middleware. Handles both preflight
|
|
452
493
|
* (`OPTIONS`) and actual requests, attaching the correct
|
|
@@ -838,7 +879,7 @@ export function bearerAuth(opts) {
|
|
|
838
879
|
if (/["\r\n\0]/.test(realm)) {
|
|
839
880
|
throw new Error("bearerAuth(): realm must not contain quotes, CR, LF, or NUL bytes.");
|
|
840
881
|
}
|
|
841
|
-
return {
|
|
882
|
+
return markAuthHook({
|
|
842
883
|
async beforeHandle(ctx) {
|
|
843
884
|
const h = ctx.request.headers.get("authorization") ?? "";
|
|
844
885
|
const m = /^Bearer\s+(.+)$/i.exec(h);
|
|
@@ -866,7 +907,7 @@ export function bearerAuth(opts) {
|
|
|
866
907
|
}
|
|
867
908
|
return undefined;
|
|
868
909
|
},
|
|
869
|
-
};
|
|
910
|
+
});
|
|
870
911
|
}
|
|
871
912
|
const CSRF_STATE_TOKEN = "csrfToken";
|
|
872
913
|
const CSRF_STATE_ISSUED = "__csrfIssued";
|
|
@@ -1115,7 +1156,7 @@ export function basicAuth(opts) {
|
|
|
1115
1156
|
if (!Number.isInteger(maxBytes) || maxBytes < 1) {
|
|
1116
1157
|
throw new Error("basicAuth(): maxCredentialBytes must be a positive integer.");
|
|
1117
1158
|
}
|
|
1118
|
-
return {
|
|
1159
|
+
return markAuthHook({
|
|
1119
1160
|
async beforeHandle(ctx) {
|
|
1120
1161
|
const header = ctx.request.headers.get("authorization") ?? "";
|
|
1121
1162
|
const match = BASIC_AUTH_TOKEN_RE.exec(header);
|
|
@@ -1134,7 +1175,7 @@ export function basicAuth(opts) {
|
|
|
1134
1175
|
}
|
|
1135
1176
|
return undefined;
|
|
1136
1177
|
},
|
|
1137
|
-
};
|
|
1178
|
+
});
|
|
1138
1179
|
}
|
|
1139
1180
|
// ---------- requireScopes ----------
|
|
1140
1181
|
/**
|
package/dist/mtls.js
CHANGED
|
@@ -347,7 +347,7 @@ export function clientCertAuth(opts = {}) {
|
|
|
347
347
|
return certFromHeaders(ctx.request, headerConfig);
|
|
348
348
|
return undefined;
|
|
349
349
|
});
|
|
350
|
-
|
|
350
|
+
const authHooks = {
|
|
351
351
|
async beforeHandle(ctx) {
|
|
352
352
|
const cert = resolve(ctx);
|
|
353
353
|
if (!cert) {
|
|
@@ -386,6 +386,11 @@ export function clientCertAuth(opts = {}) {
|
|
|
386
386
|
return undefined;
|
|
387
387
|
},
|
|
388
388
|
};
|
|
389
|
+
// Same global symbol as middleware's AUTH_HOOK_MARKER (stamped inline to keep
|
|
390
|
+
// the middleware module out of this bundle): lets the route-auth boot guard
|
|
391
|
+
// recognize that a route declaring `auth:` is actually enforced here.
|
|
392
|
+
authHooks[Symbol.for("daloyjs.auth.hook")] = true;
|
|
393
|
+
return authHooks;
|
|
389
394
|
}
|
|
390
395
|
function assertHeaderConfig(cfg) {
|
|
391
396
|
if (cfg.format === "xfcc")
|
package/dist/sbom.cdx.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"bomFormat": "CycloneDX",
|
|
3
3
|
"specVersion": "1.5",
|
|
4
|
-
"serialNumber": "urn:uuid:
|
|
4
|
+
"serialNumber": "urn:uuid:8448de36-3a72-553d-882f-b5b36bbd1426",
|
|
5
5
|
"version": 1,
|
|
6
6
|
"metadata": {
|
|
7
|
-
"timestamp": "2026-07-
|
|
7
|
+
"timestamp": "2026-07-07T09:24:36.816Z",
|
|
8
8
|
"tools": [
|
|
9
9
|
{
|
|
10
10
|
"vendor": "DaloyJS",
|
|
11
11
|
"name": "daloy-generate-sbom",
|
|
12
|
-
"version": "1.0.0-rc.
|
|
12
|
+
"version": "1.0.0-rc.2"
|
|
13
13
|
}
|
|
14
14
|
],
|
|
15
15
|
"authors": [
|
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
],
|
|
20
20
|
"component": {
|
|
21
21
|
"type": "library",
|
|
22
|
-
"bom-ref": "pkg:npm/@daloyjs/core@1.0.0-rc.
|
|
22
|
+
"bom-ref": "pkg:npm/@daloyjs/core@1.0.0-rc.2",
|
|
23
23
|
"name": "@daloyjs/core",
|
|
24
|
-
"version": "1.0.0-rc.
|
|
24
|
+
"version": "1.0.0-rc.2",
|
|
25
25
|
"description": "DaloyJS is a runtime-portable, contract-first TypeScript web framework with built-in OpenAPI (Hey API), typed client generation, large-scale maintainability, and security-first defaults. Hono-grade portability, Elysia-grade DX, FastAPI-grade docs, Fastify-grade ops — distributed via pnpm.",
|
|
26
|
-
"purl": "pkg:npm/@daloyjs/core@1.0.0-rc.
|
|
26
|
+
"purl": "pkg:npm/@daloyjs/core@1.0.0-rc.2",
|
|
27
27
|
"licenses": [
|
|
28
28
|
{
|
|
29
29
|
"license": {
|
|
@@ -46,9 +46,9 @@
|
|
|
46
46
|
}
|
|
47
47
|
],
|
|
48
48
|
"swid": {
|
|
49
|
-
"tagId": "swidtag--daloyjs-core-1.0.0-rc.
|
|
49
|
+
"tagId": "swidtag--daloyjs-core-1.0.0-rc.2",
|
|
50
50
|
"name": "@daloyjs/core",
|
|
51
|
-
"version": "1.0.0-rc.
|
|
51
|
+
"version": "1.0.0-rc.2",
|
|
52
52
|
"tagVersion": 0,
|
|
53
53
|
"patch": false
|
|
54
54
|
}
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"components": [],
|
|
58
58
|
"dependencies": [
|
|
59
59
|
{
|
|
60
|
-
"ref": "pkg:npm/@daloyjs/core@1.0.0-rc.
|
|
60
|
+
"ref": "pkg:npm/@daloyjs/core@1.0.0-rc.2",
|
|
61
61
|
"dependsOn": []
|
|
62
62
|
}
|
|
63
63
|
]
|
package/dist/sbom.spdx.json
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
"spdxVersion": "SPDX-2.3",
|
|
3
3
|
"dataLicense": "CC0-1.0",
|
|
4
4
|
"SPDXID": "SPDXRef-DOCUMENT",
|
|
5
|
-
"name": "@daloyjs/core-1.0.0-rc.
|
|
6
|
-
"documentNamespace": "https://github.com/daloyjs/daloy/sbom/@daloyjs/core-1.0.0-rc.
|
|
5
|
+
"name": "@daloyjs/core-1.0.0-rc.2",
|
|
6
|
+
"documentNamespace": "https://github.com/daloyjs/daloy/sbom/@daloyjs/core-1.0.0-rc.2-8448de36-3a72-553d-882f-b5b36bbd1426",
|
|
7
7
|
"creationInfo": {
|
|
8
|
-
"created": "2026-07-
|
|
8
|
+
"created": "2026-07-07T09:24:36.816Z",
|
|
9
9
|
"creators": [
|
|
10
10
|
"Tool: daloy-generate-sbom",
|
|
11
11
|
"Organization: DaloyJS"
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
{
|
|
17
17
|
"SPDXID": "SPDXRef-Package--daloyjs-core",
|
|
18
18
|
"name": "@daloyjs/core",
|
|
19
|
-
"versionInfo": "1.0.0-rc.
|
|
19
|
+
"versionInfo": "1.0.0-rc.2",
|
|
20
20
|
"downloadLocation": "https://github.com/daloyjs/daloy",
|
|
21
21
|
"filesAnalyzed": false,
|
|
22
22
|
"licenseConcluded": "MIT",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
{
|
|
28
28
|
"referenceCategory": "PACKAGE-MANAGER",
|
|
29
29
|
"referenceType": "purl",
|
|
30
|
-
"referenceLocator": "pkg:npm/@daloyjs/core@1.0.0-rc.
|
|
30
|
+
"referenceLocator": "pkg:npm/@daloyjs/core@1.0.0-rc.2"
|
|
31
31
|
}
|
|
32
32
|
]
|
|
33
33
|
}
|