cap-mcp-guard 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +229 -163
- package/lib/adapters/cap.js +123 -79
- package/lib/core/interceptor.js +140 -15
- package/lib/index.js +1 -2
- package/lib/policy/config.js +191 -106
- package/lib/policy/evaluator.js +26 -11
- package/lib/policy/pseudonym.js +89 -0
- package/package.json +56 -54
- package/lib/adapters/odata.js +0 -366
- package/lib/core/edm.js +0 -241
- package/lib/core/odata-batch.js +0 -161
- package/lib/core/odata.js +0 -178
package/README.md
CHANGED
|
@@ -1,164 +1,230 @@
|
|
|
1
|
-
# cap-mcp-guard
|
|
2
|
-
|
|
3
|
-
CAP
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
│
|
|
33
|
-
│
|
|
34
|
-
│
|
|
35
|
-
│
|
|
36
|
-
│
|
|
37
|
-
│
|
|
38
|
-
│
|
|
39
|
-
│
|
|
40
|
-
│
|
|
41
|
-
│
|
|
42
|
-
│
|
|
43
|
-
│ └─
|
|
44
|
-
│ │
|
|
45
|
-
│ lib/
|
|
46
|
-
│ └─
|
|
47
|
-
│ │
|
|
48
|
-
│ lib/
|
|
49
|
-
│ └─
|
|
50
|
-
│ │
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
1
|
+
# cap-mcp-guard
|
|
2
|
+
|
|
3
|
+
**A CAP (CDS) plugin** — auto-discovered via `cds-plugin.js` the moment it's a dependency of your project, zero manual wiring required.
|
|
4
|
+
|
|
5
|
+
CAP MCP Guard is the trust layer for AI agents accessing SAP CAP business data — request interception, policy enforcement, field masking, and OpenTelemetry-native observability for MCP-enabled applications.
|
|
6
|
+
|
|
7
|
+
Today, organizations have only two options when exposing SAP CAP business data to AI agents: grant unrestricted access or deny access completely. CAP MCP Guard introduces a third option — controlled, observable, and policy-driven access, without requiring developers to hand-write authorization, masking, and audit logic for every entity.
|
|
8
|
+
|
|
9
|
+
We give CAP developers a standardized, reusable way to enforce AI-agent security, observability, and policy — without hand-writing that logic for every entity. Convention over implementation, in the same spirit as CAP itself.
|
|
10
|
+
|
|
11
|
+
## Why
|
|
12
|
+
|
|
13
|
+
- **Field exposure.** When AI agents connect to CAP entities over MCP, sensitive fields (`CreditCardNo`, `Salary`, national ID numbers, ...) are visible to the agent unless someone filters them by hand, entity by entity.
|
|
14
|
+
- **No visibility.** Which agent accessed which entity, when, how many rows, and how long it took isn't logged anywhere standard. There's no answer when someone asks for an audit trail.
|
|
15
|
+
- **Excessive trust.** Even a "read-only" agent can technically reach every action/function a service exposes through the MCP layer, unless something enforces otherwise. Relying on the agent to "behave" isn't a control.
|
|
16
|
+
- **Enterprise distrust.** Companies want AI agents connected to business data, but security teams block it because nobody can prove what the agent will actually do. CAP MCP Guard is the third option between "wide open" and "no access at all."
|
|
17
|
+
|
|
18
|
+
## How it works
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
AI Request → Intercept → Evaluate Policy → Mask → Execute → Audit → Trace
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
Claude / Joule / Copilot / Custom Agent
|
|
26
|
+
│
|
|
27
|
+
Any MCP Runtime
|
|
28
|
+
(gavdilabs/cap-mcp-plugin, a custom runtime, a future official SAP
|
|
29
|
+
solution — it doesn't matter which)
|
|
30
|
+
│
|
|
31
|
+
┌─────────────────────────┐
|
|
32
|
+
│ cap-mcp-guard │
|
|
33
|
+
│ │
|
|
34
|
+
│ lib/core/ │
|
|
35
|
+
│ ├─ interceptor.js │ → attaches to CAP's srv.before/srv.after hooks
|
|
36
|
+
│ └─ context.js │ → builds an OTel gen_ai.*-shaped, framework-
|
|
37
|
+
│ │ agnostic request context
|
|
38
|
+
│ │
|
|
39
|
+
│ lib/policy/ │ → knows nothing about CAP. Plain JS: Context in,
|
|
40
|
+
│ ├─ config.js │ Decision out.
|
|
41
|
+
│ ├─ evaluator.js │
|
|
42
|
+
│ ├─ masking.js │
|
|
43
|
+
│ └─ pseudonym.js │
|
|
44
|
+
│ │
|
|
45
|
+
│ lib/audit/ │
|
|
46
|
+
│ └─ log.js │ → Context + Decision → structured JSON log line
|
|
47
|
+
│ │
|
|
48
|
+
│ lib/otel/ │
|
|
49
|
+
│ └─ exporter.js │ → Context + Decision → a real OTel span
|
|
50
|
+
│ │
|
|
51
|
+
│ lib/adapters/ │
|
|
52
|
+
│ └─ cap.js │ → the ONE place that knows @sap/cds.
|
|
53
|
+
│ │ cds-plugin.js calls this.
|
|
54
|
+
└─────────────────────────┘
|
|
55
|
+
│
|
|
56
|
+
CAP Service
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`lib/policy/` and `lib/core/context.js` never import `@sap/cds` — they only ever see a plain `Context` object and a plain `PolicyDefinition` object, regardless of where either one came from. That's what lets the `"cap-mcp-guard"` package.json config be swapped out for a future CDS-annotation-based source later without touching the engine itself.
|
|
60
|
+
|
|
61
|
+
## Install
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
npm install --save cap-mcp-guard
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
This is a **CDS plugin**, not a library you wire up by hand: CAP auto-discovers `cds-plugin.js` the moment the package is a dependency of your project — no manual `require`, no server bootstrap changes. Add a `"cap-mcp-guard"` key to your project's `package.json` and it's picked up automatically the next time your CAP server starts.
|
|
68
|
+
|
|
69
|
+
## Configure
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
{
|
|
73
|
+
"cap-mcp-guard": {
|
|
74
|
+
"mode": "enforce",
|
|
75
|
+
"entities": {
|
|
76
|
+
"Orders": {
|
|
77
|
+
"mask": ["CreditCard", "Salary"],
|
|
78
|
+
"maxRows": 100,
|
|
79
|
+
"allowTools": ["ReadOrders"]
|
|
80
|
+
},
|
|
81
|
+
"Customers": {
|
|
82
|
+
"mask": ["Email", "Phone"]
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
- Entities not listed here are fully accessible — this is opt-in by design; you don't have to configure every entity up front.
|
|
90
|
+
- No `"cap-mcp-guard"` key at all? The guard runs in pass-through mode (a `console.warn` tells you so) rather than crashing your server.
|
|
91
|
+
- A config that exists but fails to parse *does* fail loudly — a broken config shouldn't fail silently.
|
|
92
|
+
|
|
93
|
+
### Scoping the guard to specific services
|
|
94
|
+
|
|
95
|
+
If the same entities are served both to a human-facing UI and to an AI/MCP agent, masking
|
|
96
|
+
everything is usually wrong — the UI would see masked fields too. Add a `"services"` array
|
|
97
|
+
to scope the guard to only the CAP services named there; every other served service is left
|
|
98
|
+
completely untouched (no masking, no audit, no interceptor at all):
|
|
99
|
+
|
|
100
|
+
```json
|
|
101
|
+
{
|
|
102
|
+
"cap-mcp-guard": {
|
|
103
|
+
"mode": "enforce",
|
|
104
|
+
"services": ["AgentCatalogService"],
|
|
105
|
+
"entities": {
|
|
106
|
+
"Customers": { "mask": ["IBAN"] }
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
The recommended pattern: expose the AI/MCP-facing traffic through its own CDS service (a
|
|
113
|
+
projection over the same entities your UI's service already serves), point `"services"` at
|
|
114
|
+
that one, and leave your UI's service out of the list entirely — it keeps seeing real,
|
|
115
|
+
unmasked data. Omitting `"services"` keeps the default: every served service is guarded, as
|
|
116
|
+
before.
|
|
117
|
+
|
|
118
|
+
### Scoping the guard to specific users
|
|
119
|
+
|
|
120
|
+
If splitting into a second CDS service isn't worth it, scope the guard by **identity**
|
|
121
|
+
instead, using CAP's own authenticated user (`req.user.id`, not a client-supplied header —
|
|
122
|
+
so it can't be spoofed the way a raw HTTP header could). Add a `"users"` array: masking (and
|
|
123
|
+
`allowTools`/`maxRows` enforcement) only applies to requests whose authenticated user is in
|
|
124
|
+
that list — everyone else's request is fully passed through, exactly as if the entity had no
|
|
125
|
+
policy at all:
|
|
126
|
+
|
|
127
|
+
```json
|
|
128
|
+
{
|
|
129
|
+
"cap-mcp-guard": {
|
|
130
|
+
"mode": "enforce",
|
|
131
|
+
"users": ["mcp-agent-technical-user"],
|
|
132
|
+
"entities": {
|
|
133
|
+
"Customers": { "mask": ["IBAN"] }
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Authenticate your MCP runtime as that technical user (via XSUAA/IAS, a service key bound to
|
|
140
|
+
the CAP app) so its requests carry that identity; your UI's human users authenticate
|
|
141
|
+
normally and are never in the list, so they always see real data through the same service and
|
|
142
|
+
the same endpoint. This is only as secure as your CAP app's auth strategy — it requires a
|
|
143
|
+
real, verified identity provider (XSUAA/IAS/JWT) in production. `mocked` auth (fine for local
|
|
144
|
+
dev, as `examples/bookshop` uses) lets `req.user.id` be set by an untrusted client-supplied
|
|
145
|
+
header, which defeats this entirely.
|
|
146
|
+
|
|
147
|
+
`"services"` and `"users"` compose — set both if you want a dedicated AI-facing service *and*
|
|
148
|
+
identity verification within it.
|
|
149
|
+
|
|
150
|
+
### Pseudonymizing instead of redacting
|
|
151
|
+
|
|
152
|
+
Plain `mask` replaces every real value with the same fixed string, `'***MASKED***'` —
|
|
153
|
+
which means an AI agent can no longer tell two different customers' masked fields apart at
|
|
154
|
+
all (no grouping, no counting distinct values, no relational reasoning). `pseudonymize`
|
|
155
|
+
replaces a real value with a **fake but deterministic** one instead: the same real value
|
|
156
|
+
always produces the same fake value, and different real values produce different fake
|
|
157
|
+
values — so the agent keeps that relational structure without ever seeing the real data.
|
|
158
|
+
|
|
159
|
+
```json
|
|
160
|
+
{
|
|
161
|
+
"cap-mcp-guard": {
|
|
162
|
+
"mode": "enforce",
|
|
163
|
+
"entities": {
|
|
164
|
+
"Customers": {
|
|
165
|
+
"mask": ["CreditCard"],
|
|
166
|
+
"pseudonymize": [
|
|
167
|
+
"Email",
|
|
168
|
+
{ "field": "IBAN", "type": "iban" }
|
|
169
|
+
]
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Each entry is either a bare field name (uses the generic `"opaque"` generator — a
|
|
177
|
+
deterministic token like `Email-7f3a9c21e1b4`, carrying no information about the real
|
|
178
|
+
value) or a `{ "field", "type" }` object naming a specific generator. The only built-in
|
|
179
|
+
typed generator today is `"iban"`: it keeps the real country code and total length, and
|
|
180
|
+
computes a real ISO 7064 MOD 97-10 check digit pair, so the fake IBAN passes standard IBAN
|
|
181
|
+
checksum validation — it isn't a real account, but nothing downstream sees it as malformed.
|
|
182
|
+
A field can't be listed in both `mask` and `pseudonymize` on the same entity (config fails
|
|
183
|
+
to load if it is). More typed generators (e.g. a Luhn-valid fake credit card number) can be
|
|
184
|
+
added later without changing this config shape — anything without a dedicated generator
|
|
185
|
+
just falls back to `"opaque"`.
|
|
186
|
+
|
|
187
|
+
**Requires a secret.** Set the `CAP_MCP_GUARD_PSEUDONYM_SECRET` environment variable (or
|
|
188
|
+
pass `pseudonymSecret` directly to `registerCapMcpGuard`) — every pseudonym is derived from
|
|
189
|
+
it via HMAC, so without it a fake value can't be reproduced or tied back to a real one.
|
|
190
|
+
**Never commit this value.** If any entity configures `pseudonymize` and no secret is set,
|
|
191
|
+
the server fails to start rather than silently producing unprotected data. Rotating the
|
|
192
|
+
secret invalidates every previously-issued pseudonym (the same real value will map to a new
|
|
193
|
+
fake one from then on) — this is expected, not a bug.
|
|
194
|
+
|
|
195
|
+
## What you get, per request
|
|
196
|
+
|
|
197
|
+
- **Masking** — in `enforce` mode, fields listed under `mask` are replaced with `'***MASKED***'`, and fields under `pseudonymize` with a deterministic fake value (see above), in the real response. In `observe` mode nothing is touched; the guard only computes what *would* happen.
|
|
198
|
+
- **Audit log** — every request produces a structured JSON line (Context + Decision), to stdout and/or a file you choose.
|
|
199
|
+
- **OpenTelemetry spans** — every request also becomes a real span via `@opentelemetry/api`. If your app already has an OTel SDK configured (any OTLP-compatible backend — Grafana, Jaeger, Datadog, SAP Cloud Logging), the guard's spans just show up there, correctly linked into the caller's trace via W3C Trace Context (`traceparent`/`tracestate`) when present — no extra mapping needed, because the context schema was built against OTel's GenAI semantic conventions (`gen_ai.*`) from the start.
|
|
200
|
+
|
|
201
|
+
All three run independently and can each be disabled per-call (`audit: false`, `otel: false`) if you're wiring `registerCapMcpGuard` yourself instead of relying on auto-discovery.
|
|
202
|
+
|
|
203
|
+
## Try it
|
|
204
|
+
|
|
205
|
+
A full working example lives in [`examples/bookshop`](examples/bookshop) — SAP's own CAP getting-started sample, with `cap-mcp-guard` wired in and a `"cap-mcp-guard"` package.json config masking real fields on `CatalogService.Books`.
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
cd examples/bookshop
|
|
209
|
+
npm install
|
|
210
|
+
npm test # runs enforce/observe/audit/OTel integration tests against a real CAP service
|
|
211
|
+
npm start # boots a real server at localhost:4004 — flip package.json's "cap-mcp-guard".mode to "enforce"
|
|
212
|
+
# and hit /odata/v4/browse/Books to see masking happen live
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Coming soon (not in v1)
|
|
216
|
+
|
|
217
|
+
- `@mcp.policy`-style CDS annotations as an alternative to the `"cap-mcp-guard"` package.json config
|
|
218
|
+
- Approval workflows (human-in-the-loop for sensitive operations)
|
|
219
|
+
- Rate limiting and a dashboard UI
|
|
220
|
+
- Actually blocking a request when `allowTools`/`maxRows` is violated (today those are computed and reported, not enforced)
|
|
221
|
+
|
|
222
|
+
## Development
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
npm test # unit tests for lib/core, lib/policy, lib/audit, lib/otel, lib/adapters
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## License
|
|
229
|
+
|
|
164
230
|
MIT
|
package/lib/adapters/cap.js
CHANGED
|
@@ -1,80 +1,124 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const {
|
|
7
|
-
const {
|
|
8
|
-
const {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* @
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
const { attachInterceptor } = require('../core/interceptor');
|
|
7
|
+
const { loadConfig, NOT_CONFIGURED_PREFIX } = require('../policy/config');
|
|
8
|
+
const { logAudit } = require('../audit/log');
|
|
9
|
+
const { exportSpan } = require('../otel/exporter');
|
|
10
|
+
|
|
11
|
+
// Versions before 0.3.0 read policy from this file instead of package.json. It is no
|
|
12
|
+
// longer read at all — see resolvePolicyDefinition()'s legacy-file check below, which
|
|
13
|
+
// exists specifically so an unmigrated project fails loudly instead of silently running
|
|
14
|
+
// with zero enforcement.
|
|
15
|
+
const LEGACY_YAML_FILENAME = 'cap-mcp-guard.yaml';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Loads the `"cap-mcp-guard"` key from the host CAP project's package.json.
|
|
19
|
+
* A missing package.json or a missing key is a valid "not configured yet"
|
|
20
|
+
* state — falls back to a pass-through PolicyDefinition (every entity is
|
|
21
|
+
* opt-in, so this is equivalent to no policy at all), UNLESS a legacy
|
|
22
|
+
* `cap-mcp-guard.yaml` is sitting right there (see LEGACY_YAML_FILENAME): that
|
|
23
|
+
* almost certainly means an unmigrated pre-0.3.0 project, and starting up
|
|
24
|
+
* silently unenforced would be worse than refusing to start. A config that
|
|
25
|
+
* exists but fails to parse is a user mistake and is NOT swallowed either —
|
|
26
|
+
* it propagates so the bad config gets noticed rather than silently ignored.
|
|
27
|
+
*
|
|
28
|
+
* @param {object} cds the @sap/cds module (used only for cds.root)
|
|
29
|
+
*/
|
|
30
|
+
function resolvePolicyDefinition(cds) {
|
|
31
|
+
const root = cds.root || process.cwd();
|
|
32
|
+
const configPath = path.join(root, 'package.json');
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
return loadConfig(configPath);
|
|
36
|
+
} catch (err) {
|
|
37
|
+
if (err.message.startsWith(NOT_CONFIGURED_PREFIX)) {
|
|
38
|
+
const legacyYamlPath = path.join(root, LEGACY_YAML_FILENAME);
|
|
39
|
+
if (fs.existsSync(legacyYamlPath)) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`[cap-mcp-guard] found "${LEGACY_YAML_FILENAME}" at ${legacyYamlPath}, but this file has not been read ` +
|
|
42
|
+
'since 0.3.0 — config now lives under the "cap-mcp-guard" key in package.json. Move its contents there ' +
|
|
43
|
+
'(or delete the file, if you no longer want a policy) before starting the server. Refusing to start ' +
|
|
44
|
+
'rather than run with zero enforcement silently.'
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
console.warn('[cap-mcp-guard] no config found, running in pass-through mode (no policies enforced)');
|
|
48
|
+
return { mode: 'observe', entities: {} };
|
|
49
|
+
}
|
|
50
|
+
throw err;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The single CAP-specific connection point in this package. Everything
|
|
56
|
+
* under lib/core and lib/policy is framework-agnostic; this file is where
|
|
57
|
+
* @sap/cds concepts (served services, cds.ApplicationService, cds.root)
|
|
58
|
+
* are known.
|
|
59
|
+
*
|
|
60
|
+
* cds-plugin.js is the only caller of this function in production; tests
|
|
61
|
+
* call it directly against a real or fake CAP service.
|
|
62
|
+
*
|
|
63
|
+
* @param {object} cds the @sap/cds module (or a compatible facade)
|
|
64
|
+
* @param {object} [options] forwarded to attachInterceptor (see interceptor.js)
|
|
65
|
+
* @param {object} [options.policyDefinition] when omitted, loaded from the
|
|
66
|
+
* `"cap-mcp-guard"` key in the project's package.json (see resolvePolicyDefinition)
|
|
67
|
+
* @param {object|false} [options.audit] forwarded to logAudit()'s write
|
|
68
|
+
* options (`{ stdout, filePath }`); pass `false` to disable audit
|
|
69
|
+
* logging entirely. When omitted, falls back to the `"audit"` key parsed
|
|
70
|
+
* from package.json's `"cap-mcp-guard"` config (see lib/policy/config.js) —
|
|
71
|
+
* this is what lets `audit.filePath` be set through config alone, without
|
|
72
|
+
* hand-writing a registerCapMcpGuard() call.
|
|
73
|
+
* @param {object|false} [options.otel] forwarded to exportSpan()'s options
|
|
74
|
+
* (`{ tracer }`); pass `false` to skip OTel span export entirely. With
|
|
75
|
+
* no host-configured OTel SDK this is already a harmless no-op, so the
|
|
76
|
+
* default is to always attempt it.
|
|
77
|
+
* @param {(decision: object, context: object, req: object) => void} [options.onDecision]
|
|
78
|
+
* called in addition to the built-in audit log and OTel export, not
|
|
79
|
+
* instead of them — all that apply run for every request.
|
|
80
|
+
*
|
|
81
|
+
* `policyDefinition.services` (see lib/policy/config.js), when set, scopes the guard to
|
|
82
|
+
* only the named served services — any other served ApplicationService is left completely
|
|
83
|
+
* untouched (no interceptor attached at all, not even for observation/audit). This is how
|
|
84
|
+
* you keep a human-facing UI service unmasked while guarding a separate AI/MCP-facing
|
|
85
|
+
* service that projects the same entities. Omitted (the default) attaches to every served
|
|
86
|
+
* ApplicationService, as before.
|
|
87
|
+
*
|
|
88
|
+
* @param {string} [options.pseudonymSecret] used to derive pseudonyms (see
|
|
89
|
+
* lib/policy/pseudonym.js) for any entity configuring `pseudonymize`; when omitted,
|
|
90
|
+
* falls back to the `CAP_MCP_GUARD_PSEUDONYM_SECRET` env var. If any entity uses
|
|
91
|
+
* `pseudonymize` and neither is set, this throws immediately (at server boot, before
|
|
92
|
+
* any request can be served) rather than silently producing unprotected data.
|
|
93
|
+
*/
|
|
94
|
+
function registerCapMcpGuard(cds, options = {}) {
|
|
95
|
+
const policyDefinition = options.policyDefinition || resolvePolicyDefinition(cds);
|
|
96
|
+
const { onDecision: userOnDecision, otel } = options;
|
|
97
|
+
const audit = options.audit !== undefined ? options.audit : policyDefinition.audit;
|
|
98
|
+
|
|
99
|
+
const pseudonymSecret = options.pseudonymSecret || process.env.CAP_MCP_GUARD_PSEUDONYM_SECRET;
|
|
100
|
+
const usesPseudonymize = Object.values(policyDefinition.entities).some(
|
|
101
|
+
(entityConfig) => entityConfig.pseudonymize && entityConfig.pseudonymize.length > 0
|
|
102
|
+
);
|
|
103
|
+
if (usesPseudonymize && !pseudonymSecret) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
'CAP_MCP_GUARD_PSEUDONYM_SECRET env var (or options.pseudonymSecret) is required when "pseudonymize" is configured'
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const onDecision = (decision, context, req) => {
|
|
110
|
+
if (audit !== false) logAudit(context, decision, audit);
|
|
111
|
+
if (otel !== false) exportSpan(context, decision, otel);
|
|
112
|
+
if (typeof userOnDecision === 'function') userOnDecision(decision, context, req);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
cds.on('served', () => {
|
|
116
|
+
for (const [name, srv] of Object.entries(cds.services)) {
|
|
117
|
+
if (!(srv instanceof cds.ApplicationService)) continue;
|
|
118
|
+
if (Array.isArray(policyDefinition.services) && !policyDefinition.services.includes(name)) continue;
|
|
119
|
+
attachInterceptor(srv, { ...options, policyDefinition, onDecision, pseudonymSecret });
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
80
124
|
module.exports = { registerCapMcpGuard };
|