@mudraid/adapter-node 1.1.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/LICENSE +201 -0
- package/README.md +34 -0
- package/dist/controlLoop.d.ts +57 -0
- package/dist/controlLoop.js +245 -0
- package/dist/controlLoop.js.map +1 -0
- package/dist/decideClient.d.ts +16 -0
- package/dist/decideClient.js +22 -0
- package/dist/decideClient.js.map +1 -0
- package/dist/executionBinding.d.ts +13 -0
- package/dist/executionBinding.js +40 -0
- package/dist/executionBinding.js.map +1 -0
- package/dist/httpAuthority.d.ts +32 -0
- package/dist/httpAuthority.js +196 -0
- package/dist/httpAuthority.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/signedBundle.d.ts +20 -0
- package/dist/signedBundle.js +129 -0
- package/dist/signedBundle.js.map +1 -0
- package/dist/types.d.ts +98 -0
- package/dist/types.js +21 -0
- package/dist/types.js.map +1 -0
- package/package.json +38 -0
- package/src/controlLoop.ts +387 -0
- package/src/decideClient.ts +25 -0
- package/src/executionBinding.ts +43 -0
- package/src/httpAuthority.ts +185 -0
- package/src/index.ts +38 -0
- package/src/signedBundle.ts +130 -0
- package/src/types.ts +128 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Support. While redistributing the Work or
|
|
166
|
+
Derivative Works thereof, You may choose to offer, and charge a
|
|
167
|
+
fee for, acceptance of support, warranty, indemnity, or other
|
|
168
|
+
liability obligations and/or rights consistent with this License.
|
|
169
|
+
However, in accepting such obligations, You may act only on Your
|
|
170
|
+
own behalf and on Your sole responsibility, not on behalf of any
|
|
171
|
+
other Contributor, and only if You agree to indemnify, defend,
|
|
172
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
173
|
+
or claims asserted against, such Contributor by reason of your
|
|
174
|
+
accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 MudraID
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# MudraID Node enforcement adapter
|
|
2
|
+
|
|
3
|
+
A framework-neutral decision core and authenticated authority client for MCP Streamable HTTP tool calls. It is also the decision core used by the MudraID sidecar.
|
|
4
|
+
|
|
5
|
+
## Scope
|
|
6
|
+
|
|
7
|
+
Protected tool calls require verified configuration and a signed live decision. MCP transport/session requests (`GET`, `HEAD`, `OPTIONS`, `DELETE`) and control/discovery messages (`initialize`, `ping`, `tools/list`) do not receive a tool authorization decision; the host must enforce its normal HTTP/OAuth authentication. For ordinary REST APIs, use route/scope middleware.
|
|
8
|
+
|
|
9
|
+
## Authority client
|
|
10
|
+
|
|
11
|
+
`HttpAuthority` accepts the MudraID HTTPS origin, a registered adapter credential and the exact platform/environment/resource binding. It verifies signed configuration, active version/digest, time windows and signed decisions. Adapter credentials remain separate from caller OAuth tokens. Requests and response sizes and timeouts are bounded; decisions are not retried.
|
|
12
|
+
|
|
13
|
+
Each `decide` call receives the exact body bytes, content type, HTTP method/path and caller authorization. Its signed execution digest binds those inputs and the action/mapping/scopes/configuration. Missing, expired, tampered or mismatched decisions fail closed. The host must own the request snapshot and forward only those authorized bytes; the decision client cannot control application code that ignores its result. The sidecar implements that snapshot and forwarding boundary.
|
|
14
|
+
|
|
15
|
+
The binding establishes the requested operation. It does not establish independent business facts, such as account ownership or an approved beneficiary, or prove a committed business execution. Trusted business-fact profiles/projection and live deployment qualification remain pending.
|
|
16
|
+
|
|
17
|
+
## Control loop and testing seams
|
|
18
|
+
|
|
19
|
+
The package exports `evaluateV2`, `shouldForward`, the request/decision types, reserved-header stripping helpers and the authenticated authority client. The existing function name is an API identifier, not a separate product choice. `staticDecideClient` and `throwingDecideClient` are test seams; they must not replace authenticated authority in a deployed enforcement path.
|
|
20
|
+
|
|
21
|
+
Missing or stale configuration, an unmapped tool, framing errors, denied authority and authority outages refuse protected calls. The host must prevent direct upstream access that would bypass enforcement.
|
|
22
|
+
|
|
23
|
+
## Development and distribution
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
npm ci
|
|
27
|
+
npm run typecheck
|
|
28
|
+
npm test
|
|
29
|
+
npm run build
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The package builds compiled JavaScript and TypeScript declarations. Release checks inspect the actual tarball and its installed entry points. Publication remains gated by the protected environment and the package's support record; a local test pass is not a publication receipt.
|
|
33
|
+
|
|
34
|
+
Framework-specific Express/Fastify hooks, durable execution receipts and broader deployment/chaos qualification remain separate work. See [SECURITY.md](./SECURITY.md) for private reporting and [LICENSE](./LICENSE) for Apache-2.0 terms.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V2 enforcement control loop — the adapter-decision semantics, natively in TS.
|
|
3
|
+
*
|
|
4
|
+
* This module encodes the portable adapter-decision contract
|
|
5
|
+
* (`mudraid.adapter.decision/1`) directly, so a Node platform reaches the SAME
|
|
6
|
+
* normalized outcome as the Kong `mudraid-enforce` Lua handler, the reference
|
|
7
|
+
* runner in `shared/mudraid_contracts`, and the Python middleware — for the same
|
|
8
|
+
* facts.
|
|
9
|
+
*
|
|
10
|
+
* The decision-tree order mirrors `handler.lua:access` (and the Python
|
|
11
|
+
* `_v2_control_loop.evaluate_v2`) EXACTLY:
|
|
12
|
+
*
|
|
13
|
+
* 1. reserved `x-mudraid-*` headers are stripped FIRST, before any evaluation,
|
|
14
|
+
* and even on requests that will be denied — trusted context is never
|
|
15
|
+
* accepted as an input fact;
|
|
16
|
+
* 2. fail CLOSED when no verified signed bundle is active → not_safely_decided;
|
|
17
|
+
* 3. method classification — control verbs pass, non-POST denies;
|
|
18
|
+
* 4. bounded JSON-RPC framing — oversized/unreadable bodies deny, a JSON
|
|
19
|
+
* *array* (batch) is rejected wholesale, never partially evaluated;
|
|
20
|
+
* 5. exact, case-sensitive canonical action resolution — never fuzzy/prefix;
|
|
21
|
+
* 6. a live `/decide` call, required for every protected tool invocation and
|
|
22
|
+
* **deny-closed** on timeout/error/unconfigured;
|
|
23
|
+
* 7. allow — and only then is trusted context injected downstream.
|
|
24
|
+
*
|
|
25
|
+
* "not safely decided" (no bundle, `/decide` timeout/error) is deny-closed,
|
|
26
|
+
* never optimistically treated as allow.
|
|
27
|
+
*/
|
|
28
|
+
import { type Decision, type DecideClient, type RequestFacts } from './types.js';
|
|
29
|
+
/**
|
|
30
|
+
* Reserved headers stripped from the request before evaluation (contract A03-04).
|
|
31
|
+
*
|
|
32
|
+
* Case-insensitive prefix match; applied on every protected request regardless
|
|
33
|
+
* of the eventual outcome (original case is preserved for auditability). On an
|
|
34
|
+
* unprotected surface the request passes through untouched, so nothing is
|
|
35
|
+
* stripped.
|
|
36
|
+
*/
|
|
37
|
+
export declare function normalizeStrippedHeaders(reservedHeadersPresented: readonly string[], { protectedSurface }: {
|
|
38
|
+
readonly protectedSurface: boolean;
|
|
39
|
+
}): readonly string[];
|
|
40
|
+
/** True when a header name is a reserved `x-mudraid-*` context header. */
|
|
41
|
+
export declare function isReservedHeader(name: string): boolean;
|
|
42
|
+
/** A usable canonical action name: non-empty, within the byte bound. */
|
|
43
|
+
export declare function validToolName(name: string | null | undefined): name is string;
|
|
44
|
+
/** A fresh correlation id for a bound allow, forwarded as trusted context. */
|
|
45
|
+
export declare function newDecisionId(): string;
|
|
46
|
+
/** Whether a decision means the request should forward to the wrapped handler. */
|
|
47
|
+
export declare function shouldForward(decision: Decision): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Reproduce the reference control loop's outcome for one request.
|
|
50
|
+
*
|
|
51
|
+
* `decide` is invoked EXACTLY at the `/decide` branch (a mapped `tools/call` on
|
|
52
|
+
* an active bundle) and nowhere else, so a control verb, an unmapped action, or
|
|
53
|
+
* a framing rejection never triggers a live call. Any transport error thrown by
|
|
54
|
+
* the injected client is treated as `"error"` and deny-closed. Any "not safely
|
|
55
|
+
* decided" state is deny-closed, never allow.
|
|
56
|
+
*/
|
|
57
|
+
export declare function evaluateV2(facts: RequestFacts, decide: DecideClient): Promise<Decision>;
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V2 enforcement control loop — the adapter-decision semantics, natively in TS.
|
|
3
|
+
*
|
|
4
|
+
* This module encodes the portable adapter-decision contract
|
|
5
|
+
* (`mudraid.adapter.decision/1`) directly, so a Node platform reaches the SAME
|
|
6
|
+
* normalized outcome as the Kong `mudraid-enforce` Lua handler, the reference
|
|
7
|
+
* runner in `shared/mudraid_contracts`, and the Python middleware — for the same
|
|
8
|
+
* facts.
|
|
9
|
+
*
|
|
10
|
+
* The decision-tree order mirrors `handler.lua:access` (and the Python
|
|
11
|
+
* `_v2_control_loop.evaluate_v2`) EXACTLY:
|
|
12
|
+
*
|
|
13
|
+
* 1. reserved `x-mudraid-*` headers are stripped FIRST, before any evaluation,
|
|
14
|
+
* and even on requests that will be denied — trusted context is never
|
|
15
|
+
* accepted as an input fact;
|
|
16
|
+
* 2. fail CLOSED when no verified signed bundle is active → not_safely_decided;
|
|
17
|
+
* 3. method classification — control verbs pass, non-POST denies;
|
|
18
|
+
* 4. bounded JSON-RPC framing — oversized/unreadable bodies deny, a JSON
|
|
19
|
+
* *array* (batch) is rejected wholesale, never partially evaluated;
|
|
20
|
+
* 5. exact, case-sensitive canonical action resolution — never fuzzy/prefix;
|
|
21
|
+
* 6. a live `/decide` call, required for every protected tool invocation and
|
|
22
|
+
* **deny-closed** on timeout/error/unconfigured;
|
|
23
|
+
* 7. allow — and only then is trusted context injected downstream.
|
|
24
|
+
*
|
|
25
|
+
* "not safely decided" (no bundle, `/decide` timeout/error) is deny-closed,
|
|
26
|
+
* never optimistically treated as allow.
|
|
27
|
+
*/
|
|
28
|
+
import { randomUUID } from 'node:crypto';
|
|
29
|
+
import { MAX_TOOL_NAME_LEN, RESERVED_HEADER_PREFIX, } from './types.js';
|
|
30
|
+
/** Streamable-HTTP control verbs — carry no JSON-RPC request, cannot invoke a tool. */
|
|
31
|
+
const CONTROL_VERBS = new Set(['GET', 'HEAD', 'OPTIONS', 'DELETE']);
|
|
32
|
+
/**
|
|
33
|
+
* Control/discovery JSON-RPC methods allowed to pass a protected surface without
|
|
34
|
+
* a tool invocation (mirrors the plugin `public_methods` default). Client
|
|
35
|
+
* `notifications/*` are handled by prefix, separately.
|
|
36
|
+
*/
|
|
37
|
+
// Pre-launch scan SSC-05: `resources/list` and `prompts/list` used to be here
|
|
38
|
+
// while the Kong plugin and the Python middleware denied them, so the same
|
|
39
|
+
// client got 403 from one deployed adapter and a pass-through from another.
|
|
40
|
+
// Enumeration on a protected surface is disclosure; the conservative three
|
|
41
|
+
// are the contract, pinned by two corpus fixtures every runner consumes.
|
|
42
|
+
const DEFAULT_PUBLIC_METHODS = new Set([
|
|
43
|
+
'initialize',
|
|
44
|
+
'ping',
|
|
45
|
+
'tools/list',
|
|
46
|
+
]);
|
|
47
|
+
/**
|
|
48
|
+
* `/decide` transport-failure mode → normalized not-safely-decided reason. Every
|
|
49
|
+
* one deny-closes; the reason differs by failure mode but the surfaced adapter
|
|
50
|
+
* code is uniformly `ENFORCE_DECIDE_UNAVAILABLE`.
|
|
51
|
+
*/
|
|
52
|
+
const DECIDE_UNAVAILABLE_REASONS = {
|
|
53
|
+
timeout: 'deadline_exceeded',
|
|
54
|
+
error: 'authority_source_unavailable',
|
|
55
|
+
unreachable: 'authority_source_unavailable',
|
|
56
|
+
unconfigured: 'adapter_config_stale',
|
|
57
|
+
credential_unconfigured: 'adapter_config_stale',
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Reasons that are NOT deny outcomes. A `/decide` response that tries to label a
|
|
61
|
+
* *deny* with one of these cannot leak an allow/soft outcome through the deny
|
|
62
|
+
* path; we fall back to the generic deny reason. This is a self-contained safety
|
|
63
|
+
* guard, deliberately NOT a mirror of the full governed reason-code registry
|
|
64
|
+
* (this SDK takes no dependency on the internal `mudraid_contracts` package),
|
|
65
|
+
* matching the Python middleware's guard. Extend only with reasons whose
|
|
66
|
+
* canonical outcome is provably not "deny".
|
|
67
|
+
*/
|
|
68
|
+
const NON_DENY_REASONS = new Set([
|
|
69
|
+
'authorized', // allow
|
|
70
|
+
'adapter_config_stale', // not_safely_decided
|
|
71
|
+
'deadline_exceeded', // not_safely_decided
|
|
72
|
+
'authority_source_unavailable', // not_safely_decided
|
|
73
|
+
]);
|
|
74
|
+
const DECIDE_DENY_DEFAULT_REASON = 'policy_rule_denied';
|
|
75
|
+
const NO_TRUSTED_CONTEXT = Object.freeze([]);
|
|
76
|
+
/**
|
|
77
|
+
* Reserved headers stripped from the request before evaluation (contract A03-04).
|
|
78
|
+
*
|
|
79
|
+
* Case-insensitive prefix match; applied on every protected request regardless
|
|
80
|
+
* of the eventual outcome (original case is preserved for auditability). On an
|
|
81
|
+
* unprotected surface the request passes through untouched, so nothing is
|
|
82
|
+
* stripped.
|
|
83
|
+
*/
|
|
84
|
+
export function normalizeStrippedHeaders(reservedHeadersPresented, { protectedSurface }) {
|
|
85
|
+
if (!protectedSurface) {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
return reservedHeadersPresented.filter((h) => h.toLowerCase().startsWith(RESERVED_HEADER_PREFIX));
|
|
89
|
+
}
|
|
90
|
+
/** True when a header name is a reserved `x-mudraid-*` context header. */
|
|
91
|
+
export function isReservedHeader(name) {
|
|
92
|
+
return name.toLowerCase().startsWith(RESERVED_HEADER_PREFIX);
|
|
93
|
+
}
|
|
94
|
+
/** A usable canonical action name: non-empty, within the byte bound. */
|
|
95
|
+
export function validToolName(name) {
|
|
96
|
+
if (typeof name !== 'string') {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
const byteLen = Buffer.byteLength(name, 'utf-8');
|
|
100
|
+
return byteLen > 0 && byteLen <= MAX_TOOL_NAME_LEN;
|
|
101
|
+
}
|
|
102
|
+
/** A fresh correlation id for a bound allow, forwarded as trusted context. */
|
|
103
|
+
export function newDecisionId() {
|
|
104
|
+
return randomUUID();
|
|
105
|
+
}
|
|
106
|
+
function deny(reasonCode, tier, httpStatus, adapterCode, message, stripped, outcome = 'deny') {
|
|
107
|
+
return {
|
|
108
|
+
outcome,
|
|
109
|
+
reasonCode,
|
|
110
|
+
reasonTier: tier,
|
|
111
|
+
httpStatus,
|
|
112
|
+
adapterCode,
|
|
113
|
+
message,
|
|
114
|
+
strippedReservedHeaders: stripped,
|
|
115
|
+
trustedContext: NO_TRUSTED_CONTEXT,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function passThrough(reasonCode, stripped) {
|
|
119
|
+
return {
|
|
120
|
+
outcome: 'allow',
|
|
121
|
+
reasonCode,
|
|
122
|
+
reasonTier: 'transport',
|
|
123
|
+
httpStatus: 200,
|
|
124
|
+
adapterCode: null,
|
|
125
|
+
message: '',
|
|
126
|
+
strippedReservedHeaders: stripped,
|
|
127
|
+
trustedContext: NO_TRUSTED_CONTEXT,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/** Whether a decision means the request should forward to the wrapped handler. */
|
|
131
|
+
export function shouldForward(decision) {
|
|
132
|
+
return decision.outcome === 'allow';
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Reproduce the reference control loop's outcome for one request.
|
|
136
|
+
*
|
|
137
|
+
* `decide` is invoked EXACTLY at the `/decide` branch (a mapped `tools/call` on
|
|
138
|
+
* an active bundle) and nowhere else, so a control verb, an unmapped action, or
|
|
139
|
+
* a framing rejection never triggers a live call. Any transport error thrown by
|
|
140
|
+
* the injected client is treated as `"error"` and deny-closed. Any "not safely
|
|
141
|
+
* decided" state is deny-closed, never allow.
|
|
142
|
+
*/
|
|
143
|
+
export async function evaluateV2(facts, decide) {
|
|
144
|
+
const stripped = normalizeStrippedHeaders(facts.reservedHeadersPresented ?? [], {
|
|
145
|
+
protectedSurface: facts.protected,
|
|
146
|
+
});
|
|
147
|
+
// 1. Unprotected surface: pass through untouched (not a bundled surface).
|
|
148
|
+
if (!facts.protected) {
|
|
149
|
+
return passThrough('surface_not_protected', stripped);
|
|
150
|
+
}
|
|
151
|
+
// 2. Fail CLOSED: no verified signed bundle active → not_safely_decided.
|
|
152
|
+
if (!facts.bundleActive) {
|
|
153
|
+
return deny('adapter_config_stale', 'authorization', 503, 'ENFORCE_NO_VALID_BUNDLE', 'no verified signed bundle is active; request cannot be safely decided', stripped, 'not_safely_decided');
|
|
154
|
+
}
|
|
155
|
+
// 3. Method classification. Control verbs pass; anything neither a control verb
|
|
156
|
+
// nor POST is denied.
|
|
157
|
+
const method = (facts.method ?? '').toUpperCase();
|
|
158
|
+
if (CONTROL_VERBS.has(method)) {
|
|
159
|
+
return passThrough('control_plane_passthrough', stripped);
|
|
160
|
+
}
|
|
161
|
+
if (method !== 'POST') {
|
|
162
|
+
return deny('method_not_allowed', 'transport', 405, 'ENFORCE_METHOD_NOT_ALLOWED', 'method not allowed on a protected MCP surface', stripped);
|
|
163
|
+
}
|
|
164
|
+
// 4. Bounded framing: oversized/unreadable bodies deny, never partial eval.
|
|
165
|
+
if (facts.bodyTooLarge === true) {
|
|
166
|
+
return deny('body_too_large', 'transport', 413, 'ENFORCE_BODY_TOO_LARGE', 'request body exceeds the bounded framing limit', stripped);
|
|
167
|
+
}
|
|
168
|
+
if (facts.bodyReadable === false) {
|
|
169
|
+
return deny('body_unreadable', 'transport', 400, 'ENFORCE_BODY_UNREADABLE', 'request body could not be read', stripped);
|
|
170
|
+
}
|
|
171
|
+
// 5. Parse exactly once. Non-object bodies (scalar / non-JSON) are malformed;
|
|
172
|
+
// a JSON *array* is a batch and rejected wholesale.
|
|
173
|
+
const jsonShape = facts.jsonShape ?? 'object';
|
|
174
|
+
if (jsonShape === 'array') {
|
|
175
|
+
return deny('batch_unsupported', 'transport', 400, 'ENFORCE_BATCH_UNSUPPORTED', 'JSON-RPC batch requests are not supported', stripped);
|
|
176
|
+
}
|
|
177
|
+
if (jsonShape !== 'object') {
|
|
178
|
+
return deny('malformed_request', 'transport', 400, 'ENFORCE_MALFORMED_REQUEST', 'request body is not a single JSON-RPC 2.0 object', stripped);
|
|
179
|
+
}
|
|
180
|
+
const rpcMethod = facts.rpcMethod;
|
|
181
|
+
if (facts.jsonrpc !== '2.0' || typeof rpcMethod !== 'string' || rpcMethod === '') {
|
|
182
|
+
return deny('malformed_request', 'transport', 400, 'ENFORCE_MALFORMED_REQUEST', 'request body is not a single JSON-RPC 2.0 object', stripped);
|
|
183
|
+
}
|
|
184
|
+
// 6. Non-tool protocol messages: allowlisted control/discovery + client
|
|
185
|
+
// notifications pass; everything else on a protected surface denies rather
|
|
186
|
+
// than slipping through because extraction found no action.
|
|
187
|
+
if (rpcMethod !== 'tools/call') {
|
|
188
|
+
if (rpcMethod.startsWith('notifications/')) {
|
|
189
|
+
return passThrough('notification_passthrough', stripped);
|
|
190
|
+
}
|
|
191
|
+
if (DEFAULT_PUBLIC_METHODS.has(rpcMethod)) {
|
|
192
|
+
return passThrough('control_plane_passthrough', stripped);
|
|
193
|
+
}
|
|
194
|
+
return deny('message_not_allowed', 'transport', 403, 'ENFORCE_MESSAGE_NOT_ALLOWED', 'JSON-RPC method is not permitted on a protected surface', stripped);
|
|
195
|
+
}
|
|
196
|
+
// 7. Exact canonical action resolution — never fuzzy.
|
|
197
|
+
if (!validToolName(facts.toolName)) {
|
|
198
|
+
return deny('malformed_request', 'transport', 400, 'ENFORCE_MALFORMED_REQUEST', 'tools/call params.name is missing or exceeds the action-name bound', stripped);
|
|
199
|
+
}
|
|
200
|
+
if (facts.actionMapped !== true) {
|
|
201
|
+
return deny('action_unmapped', 'authorization', 403, 'ENFORCE_ACTION_UNMAPPED', 'no exact canonical action is mapped for this tool', stripped);
|
|
202
|
+
}
|
|
203
|
+
// 8. Live /decide — required for every protected call; deny-closed on
|
|
204
|
+
// timeout/error/unconfigured. A thrown transport error is treated as
|
|
205
|
+
// "error" (deny-closed), never optimistically allowed.
|
|
206
|
+
const action = facts.action ?? facts.toolName;
|
|
207
|
+
let result;
|
|
208
|
+
try {
|
|
209
|
+
result = await decide(action);
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
// No error detail is surfaced: a transport exception may carry secrets.
|
|
213
|
+
return deny('authority_source_unavailable', 'authorization', 503, 'ENFORCE_DECIDE_UNAVAILABLE', 'the authority could not be reached; request cannot be safely decided', stripped, 'not_safely_decided');
|
|
214
|
+
}
|
|
215
|
+
if (result.status === 'allow') {
|
|
216
|
+
const trusted = [
|
|
217
|
+
['x-mudraid-action-key', action],
|
|
218
|
+
['x-mudraid-decision-id', result.decisionId ?? newDecisionId()],
|
|
219
|
+
];
|
|
220
|
+
return {
|
|
221
|
+
outcome: 'allow',
|
|
222
|
+
reasonCode: 'authorized',
|
|
223
|
+
reasonTier: 'authorization',
|
|
224
|
+
httpStatus: 200,
|
|
225
|
+
adapterCode: null,
|
|
226
|
+
message: '',
|
|
227
|
+
strippedReservedHeaders: stripped,
|
|
228
|
+
trustedContext: trusted,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
if (result.status === 'deny') {
|
|
232
|
+
let reason = result.reason ?? DECIDE_DENY_DEFAULT_REASON;
|
|
233
|
+
// A decide-supplied reason that is not provably a deny code cannot leak an
|
|
234
|
+
// allow/soft outcome through the deny path.
|
|
235
|
+
if (NON_DENY_REASONS.has(reason)) {
|
|
236
|
+
reason = DECIDE_DENY_DEFAULT_REASON;
|
|
237
|
+
}
|
|
238
|
+
return deny(reason, 'authorization', 403, 'ENFORCE_DECISION_DENY', 'the authority denied this action', stripped);
|
|
239
|
+
}
|
|
240
|
+
// timeout | error | unreachable | unconfigured | credential_unconfigured
|
|
241
|
+
// → deny-closed 503.
|
|
242
|
+
const reason = DECIDE_UNAVAILABLE_REASONS[result.status] ?? 'authority_source_unavailable';
|
|
243
|
+
return deny(reason, 'authorization', 503, 'ENFORCE_DECIDE_UNAVAILABLE', 'the authority could not be reached; request cannot be safely decided', stripped, 'not_safely_decided');
|
|
244
|
+
}
|
|
245
|
+
//# sourceMappingURL=controlLoop.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controlLoop.js","sourceRoot":"","sources":["../src/controlLoop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC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AA2E;QAC3E,4CAA4C;QAC5C,IAAI,gBAAgB,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;YACjC,MAAM,GAAG,0BAA0B,CAAC;QACtC,CAAC;QACD,OAAO,IAAI,CACT,MAAM,EACN,eAAe,EACf,GAAG,EACH,uBAAuB,EACvB,kCAAkC,EAClC,QAAQ,CACT,CAAC;IACJ,CAAC;IAED,yEAAyE;IACzE,qBAAqB;IACrB,MAAM,MAAM,GAAG,0BAA0B,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,8BAA8B,CAAC;IAC3F,OAAO,IAAI,CACT,MAAM,EACN,eAAe,EACf,GAAG,EACH,4BAA4B,EAC5B,sEAAsE,EACtE,QAAQ,EACR,oBAAoB,CACrB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/decide` seam helpers.
|
|
3
|
+
*
|
|
4
|
+
* The REAL HTTP `/decide` client (authenticated call to the MudraID authority,
|
|
5
|
+
* with timeout/retry) is a DEFERRED remainder of EP-120-US-05. This module
|
|
6
|
+
* provides only the injectable seam and in-memory fakes, so the control loop can
|
|
7
|
+
* be exercised — and proven deny-closed — with no network.
|
|
8
|
+
*/
|
|
9
|
+
import type { DecideClient, DecideResult } from './types.js';
|
|
10
|
+
/** A seam that always returns the same fixed `/decide` result. */
|
|
11
|
+
export declare function staticDecideClient(result: DecideResult): DecideClient;
|
|
12
|
+
/**
|
|
13
|
+
* A seam that rejects (simulating a transport exception). The control loop
|
|
14
|
+
* catches this and deny-closes as `ENFORCE_DECIDE_UNAVAILABLE`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function throwingDecideClient(error?: unknown): DecideClient;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/decide` seam helpers.
|
|
3
|
+
*
|
|
4
|
+
* The REAL HTTP `/decide` client (authenticated call to the MudraID authority,
|
|
5
|
+
* with timeout/retry) is a DEFERRED remainder of EP-120-US-05. This module
|
|
6
|
+
* provides only the injectable seam and in-memory fakes, so the control loop can
|
|
7
|
+
* be exercised — and proven deny-closed — with no network.
|
|
8
|
+
*/
|
|
9
|
+
/** A seam that always returns the same fixed `/decide` result. */
|
|
10
|
+
export function staticDecideClient(result) {
|
|
11
|
+
return async () => result;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A seam that rejects (simulating a transport exception). The control loop
|
|
15
|
+
* catches this and deny-closes as `ENFORCE_DECIDE_UNAVAILABLE`.
|
|
16
|
+
*/
|
|
17
|
+
export function throwingDecideClient(error) {
|
|
18
|
+
return async () => {
|
|
19
|
+
throw error ?? new Error('decide transport failure');
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=decideClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decideClient.js","sourceRoot":"","sources":["../src/decideClient.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,kEAAkE;AAClE,MAAM,UAAU,kBAAkB,CAAC,MAAoB;IACrD,OAAO,KAAK,IAAI,EAAE,CAAC,MAAM,CAAC;AAC5B,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAe;IAClD,OAAO,KAAK,IAAI,EAAE;QAChB,MAAM,KAAK,IAAI,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;IACvD,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type JsonObject, type VerifiedBundle } from './signedBundle.js';
|
|
2
|
+
export interface ExecutionContext {
|
|
3
|
+
readonly presentedAuthorization: string;
|
|
4
|
+
readonly httpMethod: string;
|
|
5
|
+
readonly path: string;
|
|
6
|
+
readonly contentType: string;
|
|
7
|
+
readonly body: Uint8Array;
|
|
8
|
+
}
|
|
9
|
+
export declare const sha256: (bytes: string | Uint8Array) => string;
|
|
10
|
+
export declare function bindExecution(snapshot: VerifiedBundle, action: Readonly<JsonObject>, context: ExecutionContext): {
|
|
11
|
+
digest: string;
|
|
12
|
+
execution: JsonObject;
|
|
13
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Exact request binding; supplied business values are not independent facts. */
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { canonicalJson } from './signedBundle.js';
|
|
4
|
+
export const sha256 = (bytes) => createHash('sha256').update(bytes).digest('hex');
|
|
5
|
+
export function bindExecution(snapshot, action, context) {
|
|
6
|
+
if (!(context.body instanceof Uint8Array) || context.body.byteLength > 8 * 1024 * 1024)
|
|
7
|
+
throw new Error('Invalid request body');
|
|
8
|
+
if (!context.contentType || context.contentType.length > 256 || /[\r\n]/.test(context.contentType))
|
|
9
|
+
throw new Error('Invalid content type');
|
|
10
|
+
let token = context.presentedAuthorization.trim();
|
|
11
|
+
if (token.toLowerCase().startsWith('bearer '))
|
|
12
|
+
token = token.slice(7).trim();
|
|
13
|
+
if (!token)
|
|
14
|
+
throw new Error('Missing caller');
|
|
15
|
+
const scopes = action['required_scopes'];
|
|
16
|
+
if (!Array.isArray(scopes) || scopes.some(scope => typeof scope !== 'string' || !/^[\x21\x23-\x5b\x5d-\x7e]+$/.test(scope)))
|
|
17
|
+
throw new Error('Invalid action scopes');
|
|
18
|
+
const material = {
|
|
19
|
+
profile: 'mudraid.execution.request/1', body_sha256: sha256(context.body),
|
|
20
|
+
content_type: context.contentType, http_method: context.httpMethod, path: context.path,
|
|
21
|
+
caller_token_sha256: sha256(token), platform_id: snapshot.surface['platform_id'],
|
|
22
|
+
environment: snapshot.surface['environment'], resource: snapshot.surface['canonical_resource_uri'],
|
|
23
|
+
action_key: action['action_key'], action_version: action['action_version'],
|
|
24
|
+
mapping_id: action['mapping_id'], mapping_version: action['mapping_revision'],
|
|
25
|
+
bundle_version: snapshot.version, bundle_payload_digest: snapshot.digest,
|
|
26
|
+
required_scopes: [...new Set(scopes)].sort(),
|
|
27
|
+
};
|
|
28
|
+
if (Object.values(material).some(value => value === undefined || value === null || value === ''))
|
|
29
|
+
throw new Error('Incomplete execution binding');
|
|
30
|
+
return { digest: sha256(canonicalJson(material)), execution: {
|
|
31
|
+
profile: material['profile'], body_sha256: material['body_sha256'], content_type: material['content_type'],
|
|
32
|
+
...(action['argument_profile'] == null ? {} : { body_base64: boundedArgumentBody(context.body) }),
|
|
33
|
+
} };
|
|
34
|
+
}
|
|
35
|
+
function boundedArgumentBody(body) {
|
|
36
|
+
if (body.byteLength === 0 || body.byteLength > 65536)
|
|
37
|
+
throw new Error('Argument body exceeds bounds');
|
|
38
|
+
return Buffer.from(body).toString('base64');
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=executionBinding.js.map
|