@adaptic/backend 0.0.1036 → 0.0.1038
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/esm/middleware/__tests__/credential-field-guard.harness.d.ts +34 -0
- package/esm/middleware/__tests__/credential-field-guard.harness.d.ts.map +1 -0
- package/esm/middleware/__tests__/credential-field-guard.harness.js.map +1 -0
- package/esm/middleware/__tests__/credential-field-guard.harness.mjs +212 -0
- package/esm/middleware/credential-field-guard.d.ts +112 -0
- package/esm/middleware/credential-field-guard.d.ts.map +1 -0
- package/esm/middleware/credential-field-guard.js.map +1 -0
- package/esm/middleware/credential-field-guard.mjs +310 -0
- package/esm/resolvers/custom/BrokerageAccountCredentialStatusResolver.d.ts +22 -0
- package/esm/resolvers/custom/BrokerageAccountCredentialStatusResolver.d.ts.map +1 -0
- package/esm/resolvers/custom/BrokerageAccountCredentialStatusResolver.js.map +1 -0
- package/esm/resolvers/custom/BrokerageAccountCredentialStatusResolver.mjs +63 -0
- package/esm/resolvers/custom/index.d.ts +1 -0
- package/esm/resolvers/custom/index.d.ts.map +1 -1
- package/esm/resolvers/custom/index.js.map +1 -1
- package/esm/resolvers/custom/index.mjs +1 -0
- package/package.json +1 -1
- package/resolvers/custom/BrokerageAccountCredentialStatusResolver.cjs +100 -0
- package/resolvers/custom/BrokerageAccountCredentialStatusResolver.d.ts +22 -0
- package/resolvers/custom/BrokerageAccountCredentialStatusResolver.d.ts.map +1 -0
- package/resolvers/custom/BrokerageAccountCredentialStatusResolver.js.map +1 -0
- package/resolvers/custom/index.cjs +4 -1
- package/resolvers/custom/index.d.ts +1 -0
- package/resolvers/custom/index.d.ts.map +1 -1
- package/resolvers/custom/index.js.map +1 -1
- package/server.cjs +13 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Out-of-process harness for `credential-field-guard.test.ts`.
|
|
3
|
+
*
|
|
4
|
+
* The generated TypeGraphQL resolvers rely on `emitDecoratorMetadata`, which
|
|
5
|
+
* vitest's esbuild transform does not emit, so a schema built from them cannot
|
|
6
|
+
* be constructed inside the vitest worker. This harness runs under ts-node
|
|
7
|
+
* (which honours the repo tsconfig), builds a schema from the GENERATED
|
|
8
|
+
* AlpacaAccount and User resolvers with the guard installed exactly as
|
|
9
|
+
* `server.ts` installs it, executes every scenario, and prints one JSON
|
|
10
|
+
* document the test asserts on.
|
|
11
|
+
*
|
|
12
|
+
* Run directly: npx ts-node --transpile-only src/middleware/__tests__/credential-field-guard.harness.ts
|
|
13
|
+
*/
|
|
14
|
+
import 'reflect-metadata';
|
|
15
|
+
import type { BackendPrincipal } from '../../auth/token-verifier';
|
|
16
|
+
import { type CredentialFieldGuardMode } from '../credential-field-guard';
|
|
17
|
+
export declare const API_KEY = "PKGUARDTESTKEYVALUE0000000";
|
|
18
|
+
export declare const API_SECRET = "guard-test-secret-value-that-must-never-leak";
|
|
19
|
+
declare const PRINCIPALS: Record<string, BackendPrincipal | null>;
|
|
20
|
+
export interface Scenario {
|
|
21
|
+
name: string;
|
|
22
|
+
query: string;
|
|
23
|
+
principal: keyof typeof PRINCIPALS;
|
|
24
|
+
mode: CredentialFieldGuardMode;
|
|
25
|
+
}
|
|
26
|
+
export interface ScenarioResult {
|
|
27
|
+
codes: string[];
|
|
28
|
+
json: string;
|
|
29
|
+
resolverCalls: number;
|
|
30
|
+
counterDelta: Record<string, number>;
|
|
31
|
+
}
|
|
32
|
+
export declare const SCENARIOS: Scenario[];
|
|
33
|
+
export {};
|
|
34
|
+
//# sourceMappingURL=credential-field-guard.harness.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-field-guard.harness.d.ts","sourceRoot":"","sources":["../../../../src/middleware/__tests__/credential-field-guard.harness.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,kBAAkB,CAAC;AAQ1B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAClE,OAAO,EAGL,KAAK,wBAAwB,EAC9B,MAAM,2BAA2B,CAAC;AAEnC,eAAO,MAAM,OAAO,+BAA+B,CAAC;AACpD,eAAO,MAAM,UAAU,iDAAiD,CAAC;AAIzE,QAAA,MAAM,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,GAAG,IAAI,CAKvD,CAAC;AAEF,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,OAAO,UAAU,CAAC;IACnC,IAAI,EAAE,wBAAwB,CAAC;CAChC;AAED,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACtC;AAID,eAAO,MAAM,SAAS,EAAE,QAAQ,EAwG/B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-field-guard.harness.js","sourceRoot":"","sources":["../../../../src/middleware/__tests__/credential-field-guard.harness.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,OAAO,EAAE,yBAAyB,EAAE,MAAM,2FAA2F,CAAC;AACtI,OAAO,EAAE,gBAAgB,EAAE,MAAM,yEAAyE,CAAC;AAC3G,OAAO,EAAE,4BAA4B,EAAE,MAAM,iGAAiG,CAAC;AAC/I,OAAO,EAAE,wCAAwC,EAAE,MAAM,iEAAiE,CAAC;AAE3H,OAAO,EACL,oCAAoC,EACpC,0BAA0B,GAE3B,MAAM,2BAA2B,CAAC;AAEnC,MAAM,CAAC,MAAM,OAAO,GAAG,4BAA4B,CAA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AE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC;YAC5B,aAAa;YACb,YAAY;SACb,CAAC;IACJ,CAAC;IACD,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,gBAAgB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;AAC7E,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;IAC9B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,mBAAmB,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC3D,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Out-of-process harness for `credential-field-guard.test.ts`.
|
|
3
|
+
*
|
|
4
|
+
* The generated TypeGraphQL resolvers rely on `emitDecoratorMetadata`, which
|
|
5
|
+
* vitest's esbuild transform does not emit, so a schema built from them cannot
|
|
6
|
+
* be constructed inside the vitest worker. This harness runs under ts-node
|
|
7
|
+
* (which honours the repo tsconfig), builds a schema from the GENERATED
|
|
8
|
+
* AlpacaAccount and User resolvers with the guard installed exactly as
|
|
9
|
+
* `server.ts` installs it, executes every scenario, and prints one JSON
|
|
10
|
+
* document the test asserts on.
|
|
11
|
+
*
|
|
12
|
+
* Run directly: npx ts-node --transpile-only src/middleware/__tests__/credential-field-guard.harness.ts
|
|
13
|
+
*/
|
|
14
|
+
import 'reflect-metadata';
|
|
15
|
+
import { buildSchema } from 'type-graphql';
|
|
16
|
+
import { graphql } from 'graphql';
|
|
17
|
+
import { AlpacaAccountCrudResolver } from '../../generated/typegraphql-prisma/resolvers/crud/AlpacaAccount/AlpacaAccountCrudResolver.mjs';
|
|
18
|
+
import { UserCrudResolver } from '../../generated/typegraphql-prisma/resolvers/crud/User/UserCrudResolver.mjs';
|
|
19
|
+
import { BrokerageAccountCrudResolver } from '../../generated/typegraphql-prisma/resolvers/crud/BrokerageAccount/BrokerageAccountCrudResolver.mjs';
|
|
20
|
+
import { BrokerageAccountCredentialStatusResolver } from '../../resolvers/custom/BrokerageAccountCredentialStatusResolver.mjs';
|
|
21
|
+
import { createCredentialFieldGuardMiddleware, credentialFieldAccessTotal, } from '../credential-field-guard.mjs';
|
|
22
|
+
export const API_KEY = 'PKGUARDTESTKEYVALUE0000000';
|
|
23
|
+
export const API_SECRET = 'guard-test-secret-value-that-must-never-leak';
|
|
24
|
+
const ROW = { id: 'acct-1', type: 'LIVE', APIKey: API_KEY, APISecret: API_SECRET };
|
|
25
|
+
const PRINCIPALS = {
|
|
26
|
+
none: null,
|
|
27
|
+
server: { kind: 'server' },
|
|
28
|
+
user: { kind: 'user', sub: 'u-1', roles: ['user'] },
|
|
29
|
+
admin: { kind: 'admin', sub: 'a-1', roles: ['admin'] },
|
|
30
|
+
};
|
|
31
|
+
const READ_KEYS = '{ alpacaAccounts { id APIKey APISecret } }';
|
|
32
|
+
export const SCENARIOS = [
|
|
33
|
+
{ name: 'output/server', query: READ_KEYS, principal: 'server', mode: 'enforce' },
|
|
34
|
+
{ name: 'output/none', query: READ_KEYS, principal: 'none', mode: 'enforce' },
|
|
35
|
+
{ name: 'output/user', query: READ_KEYS, principal: 'user', mode: 'enforce' },
|
|
36
|
+
{ name: 'output/admin', query: READ_KEYS, principal: 'admin', mode: 'enforce' },
|
|
37
|
+
{
|
|
38
|
+
name: 'output/non-credential-none',
|
|
39
|
+
query: '{ alpacaAccounts { id type } }',
|
|
40
|
+
principal: 'none',
|
|
41
|
+
mode: 'enforce',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: 'output/single-row-none',
|
|
45
|
+
query: '{ alpacaAccount(where: { id: "acct-1" }) { id APISecret } }',
|
|
46
|
+
principal: 'none',
|
|
47
|
+
mode: 'enforce',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: 'aggregate-max/server',
|
|
51
|
+
query: '{ aggregateAlpacaAccount { _max { APISecret } } }',
|
|
52
|
+
principal: 'server',
|
|
53
|
+
mode: 'enforce',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: 'aggregate-max/none',
|
|
57
|
+
query: '{ aggregateAlpacaAccount { _max { APISecret } } }',
|
|
58
|
+
principal: 'none',
|
|
59
|
+
mode: 'enforce',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: 'aggregate-count/none',
|
|
63
|
+
query: '{ aggregateAlpacaAccount { _count { APISecret } } }',
|
|
64
|
+
principal: 'none',
|
|
65
|
+
mode: 'enforce',
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: 'arg/where-none',
|
|
69
|
+
query: '{ alpacaAccounts(where: { APISecret: { startsWith: "g" } }) { id } }',
|
|
70
|
+
principal: 'none',
|
|
71
|
+
mode: 'enforce',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: 'arg/where-server',
|
|
75
|
+
query: '{ alpacaAccounts(where: { APISecret: { startsWith: "g" } }) { id } }',
|
|
76
|
+
principal: 'server',
|
|
77
|
+
mode: 'enforce',
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
name: 'arg/not-nested-none',
|
|
81
|
+
query: '{ alpacaAccounts(where: { NOT: [{ APIKey: { equals: "x" } }] }) { id } }',
|
|
82
|
+
principal: 'none',
|
|
83
|
+
mode: 'enforce',
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
name: 'arg/orderBy-none',
|
|
87
|
+
query: '{ alpacaAccounts(orderBy: [{ APISecret: asc }]) { id } }',
|
|
88
|
+
principal: 'none',
|
|
89
|
+
mode: 'enforce',
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: 'arg/distinct-none',
|
|
93
|
+
query: '{ alpacaAccounts(distinct: [APIKey]) { id } }',
|
|
94
|
+
principal: 'none',
|
|
95
|
+
mode: 'enforce',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'arg/relation-filter-none',
|
|
99
|
+
query: '{ users(where: { alpacaAccounts: { some: { APISecret: { startsWith: "g" } } } }) { id } }',
|
|
100
|
+
principal: 'none',
|
|
101
|
+
mode: 'enforce',
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: 'arg/groupBy-none',
|
|
105
|
+
query: '{ groupByAlpacaAccount(by: [APIKey]) { APIKey } }',
|
|
106
|
+
principal: 'none',
|
|
107
|
+
mode: 'enforce',
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: 'arg/non-credential-filter-none',
|
|
111
|
+
query: '{ alpacaAccounts(where: { id: { equals: "acct-1" } }) { id } }',
|
|
112
|
+
principal: 'none',
|
|
113
|
+
mode: 'enforce',
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: 'brokerage/status-none',
|
|
117
|
+
query: '{ brokerageAccounts { id hasApiCredentials } }',
|
|
118
|
+
principal: 'none',
|
|
119
|
+
mode: 'enforce',
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
name: 'brokerage/key-none',
|
|
123
|
+
query: '{ brokerageAccounts { id apiKey } }',
|
|
124
|
+
principal: 'none',
|
|
125
|
+
mode: 'enforce',
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
name: 'brokerage/key-server',
|
|
129
|
+
query: '{ brokerageAccounts { id apiKey } }',
|
|
130
|
+
principal: 'server',
|
|
131
|
+
mode: 'enforce',
|
|
132
|
+
},
|
|
133
|
+
{ name: 'mode/shadow-none', query: READ_KEYS, principal: 'none', mode: 'shadow' },
|
|
134
|
+
{ name: 'mode/off-none', query: READ_KEYS, principal: 'none', mode: 'off' },
|
|
135
|
+
];
|
|
136
|
+
async function counterSnapshot() {
|
|
137
|
+
const metric = await credentialFieldAccessTotal.get();
|
|
138
|
+
const out = {};
|
|
139
|
+
for (const v of metric.values) {
|
|
140
|
+
const key = `${v.labels.surface}|${v.labels.decision}|${v.labels.principal_kind}`;
|
|
141
|
+
out[key] = (out[key] ?? 0) + v.value;
|
|
142
|
+
}
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
145
|
+
async function main() {
|
|
146
|
+
let mode = 'enforce';
|
|
147
|
+
let resolverCalls = 0;
|
|
148
|
+
const count = (value) => {
|
|
149
|
+
resolverCalls += 1;
|
|
150
|
+
return Promise.resolve(value);
|
|
151
|
+
};
|
|
152
|
+
const prisma = {
|
|
153
|
+
alpacaAccount: {
|
|
154
|
+
findMany: () => count([ROW]),
|
|
155
|
+
findUnique: () => count(ROW),
|
|
156
|
+
groupBy: () => count([{ id: ROW.id, APIKey: API_KEY, _max: { APISecret: API_SECRET } }]),
|
|
157
|
+
aggregate: () => count({ _max: { APISecret: API_SECRET }, _count: { APISecret: 1 } }),
|
|
158
|
+
},
|
|
159
|
+
user: { findMany: () => count([{ id: 'u-1' }]) },
|
|
160
|
+
brokerageAccount: {
|
|
161
|
+
findMany: () => count([
|
|
162
|
+
{ id: 'ba-1', apiKey: API_KEY, apiSecret: API_SECRET },
|
|
163
|
+
{ id: 'ba-2', apiKey: null, apiSecret: null },
|
|
164
|
+
{ id: 'ba-3', apiKey: API_KEY, apiSecret: ' ' },
|
|
165
|
+
]),
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
const schema = await buildSchema({
|
|
169
|
+
resolvers: [
|
|
170
|
+
AlpacaAccountCrudResolver,
|
|
171
|
+
UserCrudResolver,
|
|
172
|
+
BrokerageAccountCrudResolver,
|
|
173
|
+
BrokerageAccountCredentialStatusResolver,
|
|
174
|
+
],
|
|
175
|
+
validate: false,
|
|
176
|
+
globalMiddlewares: [createCredentialFieldGuardMiddleware({ modeProvider: () => mode })],
|
|
177
|
+
});
|
|
178
|
+
const results = {};
|
|
179
|
+
for (const scenario of SCENARIOS) {
|
|
180
|
+
mode = scenario.mode;
|
|
181
|
+
resolverCalls = 0;
|
|
182
|
+
const before = await counterSnapshot();
|
|
183
|
+
const result = await graphql({
|
|
184
|
+
schema,
|
|
185
|
+
source: scenario.query,
|
|
186
|
+
contextValue: {
|
|
187
|
+
prisma,
|
|
188
|
+
principal: PRINCIPALS[scenario.principal],
|
|
189
|
+
req: { ip: '203.0.113.9', headers: { 'user-agent': 'guard-harness' } },
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
const after = await counterSnapshot();
|
|
193
|
+
const counterDelta = {};
|
|
194
|
+
for (const [key, value] of Object.entries(after)) {
|
|
195
|
+
const delta = value - (before[key] ?? 0);
|
|
196
|
+
if (delta !== 0)
|
|
197
|
+
counterDelta[key] = delta;
|
|
198
|
+
}
|
|
199
|
+
results[scenario.name] = {
|
|
200
|
+
codes: (result.errors ?? []).map((e) => String(e.extensions?.code)),
|
|
201
|
+
json: JSON.stringify(result),
|
|
202
|
+
resolverCalls,
|
|
203
|
+
counterDelta,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
process.stdout.write(`<<<RESULTS>>>${JSON.stringify(results)}<<<END>>>\n`);
|
|
207
|
+
}
|
|
208
|
+
main().catch((error) => {
|
|
209
|
+
process.stderr.write(`harness failed: ${String(error)}\n`);
|
|
210
|
+
process.exit(1);
|
|
211
|
+
});
|
|
212
|
+
//# sourceMappingURL=credential-field-guard.harness.js.map
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credential-field guard: stored credentials resolve, and can be filtered or
|
|
3
|
+
* sorted on, only for a verified service principal.
|
|
4
|
+
*
|
|
5
|
+
* ## Why this exists
|
|
6
|
+
*
|
|
7
|
+
* The `/graphql` context admits a request that presents no bearer token as a
|
|
8
|
+
* null principal — context-level enforcement is staged per
|
|
9
|
+
* `docs/security/2026-08-23-graphql-auth-enforcement-runbook.md` because live
|
|
10
|
+
* callers still depend on that path. The generated Prisma types expose every
|
|
11
|
+
* scalar column as a plain field, including the broker API key and secret on
|
|
12
|
+
* `AlpacaAccount` and the OAuth, session and invite tokens on the auth models.
|
|
13
|
+
* With a null principal admitted, those columns are readable by anyone who can
|
|
14
|
+
* reach the endpoint.
|
|
15
|
+
*
|
|
16
|
+
* Hiding the field alone is not enough. A filter, sort or cursor over a
|
|
17
|
+
* credential column is an oracle: `where: { APISecret: { startsWith: "a" } }`
|
|
18
|
+
* answers "does any row match" without ever selecting the field, and a caller
|
|
19
|
+
* can walk the whole secret one character at a time. `groupBy` and the
|
|
20
|
+
* `_min` / `_max` aggregates return the column's value directly. So the guard
|
|
21
|
+
* covers every way a query can observe a credential, not just the field.
|
|
22
|
+
*
|
|
23
|
+
* A stored credential is only ever needed by a service acting for the
|
|
24
|
+
* platform — the engine placing an order with an account's key. No end user,
|
|
25
|
+
* admin session or anonymous caller needs the raw value, so the read surface
|
|
26
|
+
* admits `kind: "server"` only. Writes of a credential are governed by the
|
|
27
|
+
* mutation authentication guard, not here.
|
|
28
|
+
*
|
|
29
|
+
* ## What is guarded
|
|
30
|
+
*
|
|
31
|
+
* 1. **Output** — a credential field on its model type and on that model's
|
|
32
|
+
* `GroupBy` / `MinAggregate` / `MaxAggregate` output types.
|
|
33
|
+
* 2. **Filter and sort input** — any argument that names a credential field:
|
|
34
|
+
* a key in the model's own `…Where…`, `…OrderBy…` or `…Having…` input types
|
|
35
|
+
* at any nesting depth (AND / OR / NOT, relation filters reached from other
|
|
36
|
+
* models), and a `by` / `distinct` value of the model's `ScalarFieldEnum`.
|
|
37
|
+
*
|
|
38
|
+
* ## Modes (`CREDENTIAL_FIELD_GUARD_MODE`, read on every request)
|
|
39
|
+
*
|
|
40
|
+
* - `enforce` — deny with `FORBIDDEN`. This is the default, and the value any
|
|
41
|
+
* unset or unrecognised setting resolves to: a guard whose configuration
|
|
42
|
+
* cannot be read must refuse, never admit.
|
|
43
|
+
* - `shadow` — resolve unchanged, but count and log every access `enforce`
|
|
44
|
+
* would deny. Exists only to confirm, before enforcing, that no legitimate
|
|
45
|
+
* caller reads a credential without a service principal.
|
|
46
|
+
* - `off` — no-op.
|
|
47
|
+
*
|
|
48
|
+
* @module middleware/credential-field-guard
|
|
49
|
+
*/
|
|
50
|
+
import type { MiddlewareFn } from 'type-graphql';
|
|
51
|
+
import { type GraphQLResolveInfo } from 'graphql';
|
|
52
|
+
import { Counter } from 'prom-client';
|
|
53
|
+
import type { BackendPrincipal } from '../auth/token-verifier';
|
|
54
|
+
/** The guard's operating mode. See the module doc. */
|
|
55
|
+
export type CredentialFieldGuardMode = 'enforce' | 'shadow' | 'off';
|
|
56
|
+
/**
|
|
57
|
+
* Every stored credential column, by Prisma model. A column belongs here when
|
|
58
|
+
* its value lets the holder act as someone: a broker or vendor API secret, an
|
|
59
|
+
* OAuth access / refresh / id token, a session token, a one-time verification
|
|
60
|
+
* or invite token.
|
|
61
|
+
*/
|
|
62
|
+
export declare const CREDENTIAL_FIELDS: ReadonlyMap<string, ReadonlySet<string>>;
|
|
63
|
+
/**
|
|
64
|
+
* Every credential reference in a field's arguments, as `TypeName.field`
|
|
65
|
+
* strings. Empty when the field takes no arguments or names no credential.
|
|
66
|
+
*/
|
|
67
|
+
export declare function findCredentialArgumentReferences(info: Pick<GraphQLResolveInfo, 'parentType' | 'fieldName'>, args: Record<string, unknown>): string[];
|
|
68
|
+
/** The credential field this resolver outputs, as `TypeName.field`, if any. */
|
|
69
|
+
export declare function findCredentialOutputField(info: Pick<GraphQLResolveInfo, 'parentType' | 'fieldName'>): string | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* Resolve the guard mode from `CREDENTIAL_FIELD_GUARD_MODE`. Anything other
|
|
72
|
+
* than an explicit `shadow` or `off` enforces.
|
|
73
|
+
*/
|
|
74
|
+
export declare function getCredentialFieldGuardMode(): CredentialFieldGuardMode;
|
|
75
|
+
/**
|
|
76
|
+
* Credential-surface accesses by surface, decision and principal kind. The
|
|
77
|
+
* `would_deny` series in `shadow` is the list of callers that enforcement
|
|
78
|
+
* would break; it must be empty of legitimate callers before enforcing.
|
|
79
|
+
*/
|
|
80
|
+
export declare const credentialFieldAccessTotal: Counter<"decision" | "surface" | "principal_kind">;
|
|
81
|
+
/** The slice of the Express request the guard reads for its log line. */
|
|
82
|
+
interface GuardRequest {
|
|
83
|
+
ip?: string;
|
|
84
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
85
|
+
}
|
|
86
|
+
/** GraphQL context shape the guard reads. */
|
|
87
|
+
export interface CredentialFieldGuardContext {
|
|
88
|
+
principal?: BackendPrincipal | null;
|
|
89
|
+
req?: GuardRequest;
|
|
90
|
+
}
|
|
91
|
+
/** Options for {@link createCredentialFieldGuardMiddleware}, for tests. */
|
|
92
|
+
export interface CredentialFieldGuardOptions {
|
|
93
|
+
/** Override the mode resolver (defaults to the environment). */
|
|
94
|
+
modeProvider?: () => CredentialFieldGuardMode;
|
|
95
|
+
/** Clock for the log throttle. */
|
|
96
|
+
now?: () => number;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Create the TypeGraphQL global middleware that guards credential columns.
|
|
100
|
+
*
|
|
101
|
+
* Runs on every field. The hot path — a non-credential field with no
|
|
102
|
+
* arguments, or any field for a service principal — returns after a map
|
|
103
|
+
* lookup.
|
|
104
|
+
*
|
|
105
|
+
* @param options - Optional overrides for tests.
|
|
106
|
+
* @returns A {@link MiddlewareFn} for `buildSchema({ globalMiddlewares })`.
|
|
107
|
+
*/
|
|
108
|
+
export declare function createCredentialFieldGuardMiddleware(options?: CredentialFieldGuardOptions): MiddlewareFn<CredentialFieldGuardContext>;
|
|
109
|
+
/** @internal Test hook: forget the log throttle state. */
|
|
110
|
+
export declare function resetCredentialFieldGuardLogThrottle(): void;
|
|
111
|
+
export {};
|
|
112
|
+
//# sourceMappingURL=credential-field-guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-field-guard.d.ts","sourceRoot":"","sources":["../../../src/middleware/credential-field-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAO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M,MAAM,CAAC;CACpB;AAeD;;;;;;;;;GASG;AACH,wBAAgB,oCAAoC,CAClD,OAAO,GAAE,2BAAgC,GACxC,YAAY,CAAC,2BAA2B,CAAC,CA4C3C;AAED,0DAA0D;AAC1D,wBAAgB,oCAAoC,IAAI,IAAI,CAE3D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-field-guard.js","sourceRoot":"","sources":["../../../src/middleware/credential-field-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AAGH,OAAO,EACL,eAAe,EACf,YAAY,EACZ,sBAAsB,EACtB,WAAW,EACX,eAAe,GAGhB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAGtC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAKzC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAA6C,IAAI,GAAG,CAGhF;IACA,CAAC,eAAe,EAAE,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC;IACnD,CAAC,kBAAkB,EAAE,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC;IACtD;QACE,kBAAkB;QAClB,IAAI,GAAG,CAAC;YACN,cAAc;YACd,iBAAiB;YACjB,gBAAgB;YAChB,YAAY;YACZ,YAAY;YACZ,WAAW;YACX,cAAc;YACd,iBAAiB;SAClB,CAAC;KACH;IACD,CAAC,MAAM,EAAE,IAAI,GAAG,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC;IACnC,CAAC,SAAS,EAAE,IAAI,GAAG,CAAC,CAAC,eAAe,EAAE,cAAc,EAAE,UAAU,CAAC,CAAC,CAAC;IACnE,CAAC,gBAAgB,EAAE,IAAI,GAAG,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M,IAAI,GAAG,WAAW,EAAE,CAAC;QAC3B,IAAI,IAAI,KAAK,KAAK;YAAE,OAAO,OAAO,EAAE,CAAC;QAErC,MAAM,OAAO,GAAiB,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC;QAC7D,MAAM,UAAU,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC;QACrE,MAAM,cAAc,GAAmB,SAAS,EAAE,IAAI,IAAI,MAAM,CAAC;QACjE,MAAM,QAAQ,GAAkB,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC;QAE7E,0BAA0B,CAAC,GAAG,CAAC;YAC7B,OAAO;YACP,QAAQ;YACR,cAAc,EAAE,cAAc;SAC/B,CAAC,CAAC;QACH,WAAW,CAAC,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,IAAI,EAAE,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;QAErF,IAAI,IAAI,KAAK,SAAS;YAAE,MAAM,SAAS,CAAC,UAAU,CAAC,CAAC;QACpD,OAAO,OAAO,EAAE,CAAC;IACnB,CAAC,CAAC;AACJ,CAAC;AAED,0DAA0D;AAC1D,MAAM,UAAU,oCAAoC;IAClD,YAAY,CAAC,KAAK,EAAE,CAAC;AACvB,CAAC"}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credential-field guard: stored credentials resolve, and can be filtered or
|
|
3
|
+
* sorted on, only for a verified service principal.
|
|
4
|
+
*
|
|
5
|
+
* ## Why this exists
|
|
6
|
+
*
|
|
7
|
+
* The `/graphql` context admits a request that presents no bearer token as a
|
|
8
|
+
* null principal — context-level enforcement is staged per
|
|
9
|
+
* `docs/security/2026-08-23-graphql-auth-enforcement-runbook.md` because live
|
|
10
|
+
* callers still depend on that path. The generated Prisma types expose every
|
|
11
|
+
* scalar column as a plain field, including the broker API key and secret on
|
|
12
|
+
* `AlpacaAccount` and the OAuth, session and invite tokens on the auth models.
|
|
13
|
+
* With a null principal admitted, those columns are readable by anyone who can
|
|
14
|
+
* reach the endpoint.
|
|
15
|
+
*
|
|
16
|
+
* Hiding the field alone is not enough. A filter, sort or cursor over a
|
|
17
|
+
* credential column is an oracle: `where: { APISecret: { startsWith: "a" } }`
|
|
18
|
+
* answers "does any row match" without ever selecting the field, and a caller
|
|
19
|
+
* can walk the whole secret one character at a time. `groupBy` and the
|
|
20
|
+
* `_min` / `_max` aggregates return the column's value directly. So the guard
|
|
21
|
+
* covers every way a query can observe a credential, not just the field.
|
|
22
|
+
*
|
|
23
|
+
* A stored credential is only ever needed by a service acting for the
|
|
24
|
+
* platform — the engine placing an order with an account's key. No end user,
|
|
25
|
+
* admin session or anonymous caller needs the raw value, so the read surface
|
|
26
|
+
* admits `kind: "server"` only. Writes of a credential are governed by the
|
|
27
|
+
* mutation authentication guard, not here.
|
|
28
|
+
*
|
|
29
|
+
* ## What is guarded
|
|
30
|
+
*
|
|
31
|
+
* 1. **Output** — a credential field on its model type and on that model's
|
|
32
|
+
* `GroupBy` / `MinAggregate` / `MaxAggregate` output types.
|
|
33
|
+
* 2. **Filter and sort input** — any argument that names a credential field:
|
|
34
|
+
* a key in the model's own `…Where…`, `…OrderBy…` or `…Having…` input types
|
|
35
|
+
* at any nesting depth (AND / OR / NOT, relation filters reached from other
|
|
36
|
+
* models), and a `by` / `distinct` value of the model's `ScalarFieldEnum`.
|
|
37
|
+
*
|
|
38
|
+
* ## Modes (`CREDENTIAL_FIELD_GUARD_MODE`, read on every request)
|
|
39
|
+
*
|
|
40
|
+
* - `enforce` — deny with `FORBIDDEN`. This is the default, and the value any
|
|
41
|
+
* unset or unrecognised setting resolves to: a guard whose configuration
|
|
42
|
+
* cannot be read must refuse, never admit.
|
|
43
|
+
* - `shadow` — resolve unchanged, but count and log every access `enforce`
|
|
44
|
+
* would deny. Exists only to confirm, before enforcing, that no legitimate
|
|
45
|
+
* caller reads a credential without a service principal.
|
|
46
|
+
* - `off` — no-op.
|
|
47
|
+
*
|
|
48
|
+
* @module middleware/credential-field-guard
|
|
49
|
+
*/
|
|
50
|
+
import { GraphQLEnumType, GraphQLError, GraphQLInputObjectType, GraphQLList, getNullableType, } from 'graphql';
|
|
51
|
+
import { Counter } from 'prom-client';
|
|
52
|
+
import { metricsRegistry } from '../config/metrics.mjs';
|
|
53
|
+
import { logger } from '../utils/logger.mjs';
|
|
54
|
+
/**
|
|
55
|
+
* Every stored credential column, by Prisma model. A column belongs here when
|
|
56
|
+
* its value lets the holder act as someone: a broker or vendor API secret, an
|
|
57
|
+
* OAuth access / refresh / id token, a session token, a one-time verification
|
|
58
|
+
* or invite token.
|
|
59
|
+
*/
|
|
60
|
+
export const CREDENTIAL_FIELDS = new Map([
|
|
61
|
+
['AlpacaAccount', new Set(['APIKey', 'APISecret'])],
|
|
62
|
+
['BrokerageAccount', new Set(['apiKey', 'apiSecret'])],
|
|
63
|
+
[
|
|
64
|
+
'LlmConfiguration',
|
|
65
|
+
new Set([
|
|
66
|
+
'openaiApiKey',
|
|
67
|
+
'anthropicApiKey',
|
|
68
|
+
'deepseekApiKey',
|
|
69
|
+
'kimiApiKey',
|
|
70
|
+
'qwenApiKey',
|
|
71
|
+
'xaiApiKey',
|
|
72
|
+
'geminiApiKey',
|
|
73
|
+
'deepinfraApiKey',
|
|
74
|
+
]),
|
|
75
|
+
],
|
|
76
|
+
['User', new Set(['openaiAPIKey'])],
|
|
77
|
+
['Account', new Set(['refresh_token', 'access_token', 'id_token'])],
|
|
78
|
+
['LinkedProvider', new Set(['accessToken', 'refreshToken'])],
|
|
79
|
+
['Session', new Set(['sessionToken'])],
|
|
80
|
+
['VerificationToken', new Set(['token'])],
|
|
81
|
+
['AccountLinkingRequest', new Set(['verificationToken'])],
|
|
82
|
+
['InviteToken', new Set(['token'])],
|
|
83
|
+
]);
|
|
84
|
+
/**
|
|
85
|
+
* Output types that carry a model's column VALUES. The `Count` aggregate is
|
|
86
|
+
* absent on purpose: it returns how many rows are non-null, never a value.
|
|
87
|
+
*/
|
|
88
|
+
const VALUE_OUTPUT_SUFFIXES = ['', 'GroupBy', 'MinAggregate', 'MaxAggregate'];
|
|
89
|
+
/** Output type name → the credential fields it exposes. */
|
|
90
|
+
const OUTPUT_TYPE_FIELDS = new Map([...CREDENTIAL_FIELDS].flatMap(([model, fields]) => VALUE_OUTPUT_SUFFIXES.map((suffix) => [`${model}${suffix}`, fields])));
|
|
91
|
+
/**
|
|
92
|
+
* Model names longest-first, so the prefix match below attributes
|
|
93
|
+
* `AccountLinkingRequestWhereInput` to `AccountLinkingRequest`, not `Account`.
|
|
94
|
+
*/
|
|
95
|
+
const MODELS_LONGEST_FIRST = [...CREDENTIAL_FIELDS.keys()].sort((a, b) => b.length - a.length);
|
|
96
|
+
/**
|
|
97
|
+
* Input-type name fragments that make a type a read predicate (filter, sort,
|
|
98
|
+
* cursor, aggregate filter) rather than a write payload. A `…WhereUniqueInput`
|
|
99
|
+
* is also how a mutation selects its row, so a credential key there is an
|
|
100
|
+
* oracle on a write path too.
|
|
101
|
+
*/
|
|
102
|
+
const PREDICATE_TYPE_FRAGMENTS = ['Where', 'OrderBy', 'Having'];
|
|
103
|
+
/**
|
|
104
|
+
* The credential fields a named input type can reference, when the type is
|
|
105
|
+
* one of a credential model's own predicate types; otherwise `undefined`.
|
|
106
|
+
*
|
|
107
|
+
* Generated names start with the model name followed by an upper-case letter
|
|
108
|
+
* (`AlpacaAccountWhereInput`, `AlpacaAccountOrderByWithRelationInput`), so a
|
|
109
|
+
* model named `Account` does not claim `AccountLinkingRequestWhereInput`.
|
|
110
|
+
*/
|
|
111
|
+
function predicateFieldsFor(typeName) {
|
|
112
|
+
for (const model of MODELS_LONGEST_FIRST) {
|
|
113
|
+
if (!typeName.startsWith(model))
|
|
114
|
+
continue;
|
|
115
|
+
const rest = typeName.slice(model.length);
|
|
116
|
+
if (!/^[A-Z]/.test(rest))
|
|
117
|
+
continue;
|
|
118
|
+
if (!PREDICATE_TYPE_FRAGMENTS.some((f) => rest.includes(f)))
|
|
119
|
+
return undefined;
|
|
120
|
+
return CREDENTIAL_FIELDS.get(model);
|
|
121
|
+
}
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
/** The credential fields a `<Model>ScalarFieldEnum` can name, if it is one. */
|
|
125
|
+
function enumFieldsFor(typeName) {
|
|
126
|
+
const suffix = 'ScalarFieldEnum';
|
|
127
|
+
if (!typeName.endsWith(suffix))
|
|
128
|
+
return undefined;
|
|
129
|
+
return CREDENTIAL_FIELDS.get(typeName.slice(0, -suffix.length));
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Walk one argument value alongside its GraphQL input type and collect every
|
|
133
|
+
* place it names a credential field, as `TypeName.field` strings.
|
|
134
|
+
*
|
|
135
|
+
* Type-directed rather than key-directed: a key called `token` is a
|
|
136
|
+
* credential only inside a credential model's predicate type, so an unrelated
|
|
137
|
+
* input that happens to use the same key name is never flagged.
|
|
138
|
+
*/
|
|
139
|
+
function collectInputReferences(type, value, found) {
|
|
140
|
+
if (value === null || value === undefined)
|
|
141
|
+
return;
|
|
142
|
+
const nullable = getNullableType(type);
|
|
143
|
+
if (nullable instanceof GraphQLList) {
|
|
144
|
+
const inner = nullable.ofType;
|
|
145
|
+
const items = Array.isArray(value) ? value : [value];
|
|
146
|
+
for (const item of items)
|
|
147
|
+
collectInputReferences(inner, item, found);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (nullable instanceof GraphQLEnumType) {
|
|
151
|
+
const fields = enumFieldsFor(nullable.name);
|
|
152
|
+
if (fields && typeof value === 'string' && fields.has(value)) {
|
|
153
|
+
found.push(`${nullable.name}.${value}`);
|
|
154
|
+
}
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (nullable instanceof GraphQLInputObjectType && typeof value === 'object') {
|
|
158
|
+
const guarded = predicateFieldsFor(nullable.name);
|
|
159
|
+
const fieldDefs = nullable.getFields();
|
|
160
|
+
for (const [key, child] of Object.entries(value)) {
|
|
161
|
+
if (child === undefined)
|
|
162
|
+
continue;
|
|
163
|
+
if (guarded?.has(key))
|
|
164
|
+
found.push(`${nullable.name}.${key}`);
|
|
165
|
+
const def = fieldDefs[key];
|
|
166
|
+
if (def)
|
|
167
|
+
collectInputReferences(def.type, child, found);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Every credential reference in a field's arguments, as `TypeName.field`
|
|
173
|
+
* strings. Empty when the field takes no arguments or names no credential.
|
|
174
|
+
*/
|
|
175
|
+
export function findCredentialArgumentReferences(info, args) {
|
|
176
|
+
const found = [];
|
|
177
|
+
if (!args || Object.keys(args).length === 0)
|
|
178
|
+
return found;
|
|
179
|
+
const fieldDef = info.parentType.getFields()[info.fieldName];
|
|
180
|
+
if (!fieldDef)
|
|
181
|
+
return found;
|
|
182
|
+
for (const argDef of fieldDef.args) {
|
|
183
|
+
collectInputReferences(argDef.type, args[argDef.name], found);
|
|
184
|
+
}
|
|
185
|
+
return found;
|
|
186
|
+
}
|
|
187
|
+
/** The credential field this resolver outputs, as `TypeName.field`, if any. */
|
|
188
|
+
export function findCredentialOutputField(info) {
|
|
189
|
+
const fields = OUTPUT_TYPE_FIELDS.get(info.parentType.name);
|
|
190
|
+
return fields?.has(info.fieldName)
|
|
191
|
+
? `${info.parentType.name}.${info.fieldName}`
|
|
192
|
+
: undefined;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Resolve the guard mode from `CREDENTIAL_FIELD_GUARD_MODE`. Anything other
|
|
196
|
+
* than an explicit `shadow` or `off` enforces.
|
|
197
|
+
*/
|
|
198
|
+
export function getCredentialFieldGuardMode() {
|
|
199
|
+
const raw = (process.env.CREDENTIAL_FIELD_GUARD_MODE ?? '').trim().toLowerCase();
|
|
200
|
+
if (raw === 'shadow' || raw === 'off')
|
|
201
|
+
return raw;
|
|
202
|
+
return 'enforce';
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Credential-surface accesses by surface, decision and principal kind. The
|
|
206
|
+
* `would_deny` series in `shadow` is the list of callers that enforcement
|
|
207
|
+
* would break; it must be empty of legitimate callers before enforcing.
|
|
208
|
+
*/
|
|
209
|
+
export const credentialFieldAccessTotal = new Counter({
|
|
210
|
+
name: 'graphql_credential_field_access_total',
|
|
211
|
+
help: 'Reads of, or predicates over, stored credential columns on /graphql, by ' +
|
|
212
|
+
'surface (output | argument), decision (allowed | would_deny | denied) and ' +
|
|
213
|
+
'principal kind (server | admin | user | none). Only kind=server is allowed.',
|
|
214
|
+
labelNames: ['surface', 'decision', 'principal_kind'],
|
|
215
|
+
registers: [metricsRegistry],
|
|
216
|
+
});
|
|
217
|
+
/** Throttle window for the identity log line, per distinct caller key. */
|
|
218
|
+
const LOG_THROTTLE_MS = 10 * 60 * 1000;
|
|
219
|
+
/** Most distinct caller keys the throttle remembers before it resets. */
|
|
220
|
+
const LOG_THROTTLE_MAX_KEYS = 5_000;
|
|
221
|
+
const lastLoggedAt = new Map();
|
|
222
|
+
/** Log a denied or would-deny access at most once per caller key per window. */
|
|
223
|
+
function logDecision(decision, surface, references, principal, info, request, nowMs) {
|
|
224
|
+
const operationName = info.operation?.name?.value ?? '<unnamed>';
|
|
225
|
+
const userAgent = headerValue(request?.headers?.['user-agent']);
|
|
226
|
+
const key = `${decision}|${surface}|${principal}|${references.join(',')}|${operationName}|${userAgent}`;
|
|
227
|
+
const previous = lastLoggedAt.get(key);
|
|
228
|
+
if (previous !== undefined && nowMs - previous < LOG_THROTTLE_MS)
|
|
229
|
+
return;
|
|
230
|
+
if (lastLoggedAt.size >= LOG_THROTTLE_MAX_KEYS)
|
|
231
|
+
lastLoggedAt.clear();
|
|
232
|
+
lastLoggedAt.set(key, nowMs);
|
|
233
|
+
logger.warn('[credential-field-guard] credential access by a non-service principal', {
|
|
234
|
+
decision,
|
|
235
|
+
surface,
|
|
236
|
+
references,
|
|
237
|
+
principalKind: principal,
|
|
238
|
+
operationName,
|
|
239
|
+
ip: request?.ip,
|
|
240
|
+
userAgent,
|
|
241
|
+
dedupWindowMs: LOG_THROTTLE_MS,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
function headerValue(value) {
|
|
245
|
+
if (Array.isArray(value))
|
|
246
|
+
return value.join(',');
|
|
247
|
+
return value ?? '<none>';
|
|
248
|
+
}
|
|
249
|
+
function forbidden(references) {
|
|
250
|
+
return new GraphQLError('Forbidden: stored credentials are readable only by a service principal', {
|
|
251
|
+
extensions: {
|
|
252
|
+
code: 'FORBIDDEN',
|
|
253
|
+
references,
|
|
254
|
+
http: { status: 403 },
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Create the TypeGraphQL global middleware that guards credential columns.
|
|
260
|
+
*
|
|
261
|
+
* Runs on every field. The hot path — a non-credential field with no
|
|
262
|
+
* arguments, or any field for a service principal — returns after a map
|
|
263
|
+
* lookup.
|
|
264
|
+
*
|
|
265
|
+
* @param options - Optional overrides for tests.
|
|
266
|
+
* @returns A {@link MiddlewareFn} for `buildSchema({ globalMiddlewares })`.
|
|
267
|
+
*/
|
|
268
|
+
export function createCredentialFieldGuardMiddleware(options = {}) {
|
|
269
|
+
const resolveMode = options.modeProvider ?? getCredentialFieldGuardMode;
|
|
270
|
+
const now = options.now ?? Date.now;
|
|
271
|
+
return async ({ context, args, info }, next) => {
|
|
272
|
+
const proceed = () => next();
|
|
273
|
+
const output = findCredentialOutputField(info);
|
|
274
|
+
const principal = context.principal ?? null;
|
|
275
|
+
if (principal?.kind === 'server') {
|
|
276
|
+
if (output && resolveMode() !== 'off') {
|
|
277
|
+
credentialFieldAccessTotal.inc({
|
|
278
|
+
surface: 'output',
|
|
279
|
+
decision: 'allowed',
|
|
280
|
+
principal_kind: 'server',
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
return proceed();
|
|
284
|
+
}
|
|
285
|
+
const argumentRefs = findCredentialArgumentReferences(info, args);
|
|
286
|
+
if (!output && argumentRefs.length === 0)
|
|
287
|
+
return proceed();
|
|
288
|
+
const mode = resolveMode();
|
|
289
|
+
if (mode === 'off')
|
|
290
|
+
return proceed();
|
|
291
|
+
const surface = output ? 'output' : 'argument';
|
|
292
|
+
const references = output ? [output, ...argumentRefs] : argumentRefs;
|
|
293
|
+
const principalLabel = principal?.kind ?? 'none';
|
|
294
|
+
const decision = mode === 'enforce' ? 'denied' : 'would_deny';
|
|
295
|
+
credentialFieldAccessTotal.inc({
|
|
296
|
+
surface,
|
|
297
|
+
decision,
|
|
298
|
+
principal_kind: principalLabel,
|
|
299
|
+
});
|
|
300
|
+
logDecision(decision, surface, references, principalLabel, info, context.req, now());
|
|
301
|
+
if (mode === 'enforce')
|
|
302
|
+
throw forbidden(references);
|
|
303
|
+
return proceed();
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
/** @internal Test hook: forget the log throttle state. */
|
|
307
|
+
export function resetCredentialFieldGuardLogThrottle() {
|
|
308
|
+
lastLoggedAt.clear();
|
|
309
|
+
}
|
|
310
|
+
//# sourceMappingURL=credential-field-guard.js.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { BrokerageAccount } from '../../generated/typegraphql-prisma/models/BrokerageAccount';
|
|
2
|
+
/** The two credential columns this resolver inspects on the parent row. */
|
|
3
|
+
type CredentialColumns = Pick<BrokerageAccount, 'apiKey' | 'apiSecret'>;
|
|
4
|
+
/**
|
|
5
|
+
* True only when both credential columns hold a non-blank value.
|
|
6
|
+
*
|
|
7
|
+
* @param account - The parent row as the generated resolvers loaded it.
|
|
8
|
+
* @returns Whether the account can authenticate to its broker.
|
|
9
|
+
*/
|
|
10
|
+
export declare function hasStoredApiCredentials(account: CredentialColumns): boolean;
|
|
11
|
+
/** Adds the computed `hasApiCredentials` field to `BrokerageAccount`. */
|
|
12
|
+
export declare class BrokerageAccountCredentialStatusResolver {
|
|
13
|
+
/**
|
|
14
|
+
* Whether both the API key and secret are stored for this account.
|
|
15
|
+
*
|
|
16
|
+
* @param account - The parent `BrokerageAccount` row.
|
|
17
|
+
* @returns `true` when both credential columns are non-blank.
|
|
18
|
+
*/
|
|
19
|
+
hasApiCredentials(account: BrokerageAccount): boolean;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=BrokerageAccountCredentialStatusResolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BrokerageAccountCredentialStatusResolver.d.ts","sourceRoot":"","sources":["../../../../src/resolvers/custom/BrokerageAccountCredentialStatusResolver.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,gBAAgB,EAAE,MAAM,4DAA4D,CAAC;AAE9F,2EAA2E;AAC3E,KAAK,iBAAiB,GAAG,IAAI,CAAC,gBAAgB,EAAE,QAAQ,GAAG,WAAW,CAAC,CAAC;AAExE;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAI3E;AAED,yEAAyE;AACzE,qBACa,wCAAwC;IACnD;;;;;OAKG;IAKH,iBAAiB,CAAqB,OAAO,EAAE,gBAAgB,GAAG,OAAO;CAG1E"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BrokerageAccountCredentialStatusResolver.js","sourceRoot":"","sources":["../../../../src/resolvers/custom/BrokerageAccountCredentialStatusResolver.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,KAAK,WAAW,MAAM,cAAc,CAAC;AAE5C,OAAO,EAAE,gBAAgB,EAAE,MAAM,4DAA4D,CAAC;AAK9F;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CAAC,OAA0B;IAChE,MAAM,OAAO,GAAG,CAAC,KAAgC,EAAW,EAAE,CAC5D,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC;IACvD,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;AAC/D,CAAC;AAED,yEAAyE;AAElE,IAAM,wCAAwC,GAA9C,MAAM,wCAAwC;IACnD;;;;;OAKG;IAKH,iBAAiB,CAAqB,OAAyB;QAC7D,OAAO,uBAAuB,CAAC,OAAO,CAAC,CAAC;IAC1C,CAAC;CACF,CAAA;AAHC;IAJC,WAAW,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE;QACxC,WAAW,EACT,8IAA8I;KACjJ,CAAC;IACiB,WAAA,WAAW,CAAC,IAAI,EAAE,CAAA;;qCAAU,gBAAgB;;iFAE9D;AAbU,wCAAwC;IADpD,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,gBAAgB,CAAC;GAChC,wCAAwC,CAcpD"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* `BrokerageAccount.hasApiCredentials` — whether an account's broker API key
|
|
15
|
+
* and secret are both stored, without reading either one.
|
|
16
|
+
*
|
|
17
|
+
* The credential columns are readable only by a service principal (see
|
|
18
|
+
* `src/middleware/credential-field-guard.ts`), yet a client legitimately needs
|
|
19
|
+
* to show whether an account is configured. Selecting the secret to test it
|
|
20
|
+
* for emptiness would put the secret on the wire for a yes/no answer, so the
|
|
21
|
+
* answer is computed here, where the row is already in memory, and only the
|
|
22
|
+
* boolean leaves the server.
|
|
23
|
+
*
|
|
24
|
+
* @module resolvers/custom/BrokerageAccountCredentialStatusResolver
|
|
25
|
+
*/
|
|
26
|
+
import * as TypeGraphQL from 'type-graphql';
|
|
27
|
+
import { BrokerageAccount } from '../../generated/typegraphql-prisma/models/BrokerageAccount.mjs';
|
|
28
|
+
/**
|
|
29
|
+
* True only when both credential columns hold a non-blank value.
|
|
30
|
+
*
|
|
31
|
+
* @param account - The parent row as the generated resolvers loaded it.
|
|
32
|
+
* @returns Whether the account can authenticate to its broker.
|
|
33
|
+
*/
|
|
34
|
+
export function hasStoredApiCredentials(account) {
|
|
35
|
+
const present = (value) => typeof value === 'string' && value.trim().length > 0;
|
|
36
|
+
return present(account.apiKey) && present(account.apiSecret);
|
|
37
|
+
}
|
|
38
|
+
/** Adds the computed `hasApiCredentials` field to `BrokerageAccount`. */
|
|
39
|
+
let BrokerageAccountCredentialStatusResolver = class BrokerageAccountCredentialStatusResolver {
|
|
40
|
+
/**
|
|
41
|
+
* Whether both the API key and secret are stored for this account.
|
|
42
|
+
*
|
|
43
|
+
* @param account - The parent `BrokerageAccount` row.
|
|
44
|
+
* @returns `true` when both credential columns are non-blank.
|
|
45
|
+
*/
|
|
46
|
+
hasApiCredentials(account) {
|
|
47
|
+
return hasStoredApiCredentials(account);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
__decorate([
|
|
51
|
+
TypeGraphQL.FieldResolver(() => Boolean, {
|
|
52
|
+
description: 'Whether both broker API credential columns are stored. Readable by any caller that can read the account; the credentials themselves are not.',
|
|
53
|
+
}),
|
|
54
|
+
__param(0, TypeGraphQL.Root()),
|
|
55
|
+
__metadata("design:type", Function),
|
|
56
|
+
__metadata("design:paramtypes", [BrokerageAccount]),
|
|
57
|
+
__metadata("design:returntype", Boolean)
|
|
58
|
+
], BrokerageAccountCredentialStatusResolver.prototype, "hasApiCredentials", null);
|
|
59
|
+
BrokerageAccountCredentialStatusResolver = __decorate([
|
|
60
|
+
TypeGraphQL.Resolver(() => BrokerageAccount)
|
|
61
|
+
], BrokerageAccountCredentialStatusResolver);
|
|
62
|
+
export { BrokerageAccountCredentialStatusResolver };
|
|
63
|
+
//# sourceMappingURL=BrokerageAccountCredentialStatusResolver.js.map
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* with custom aggregation queries and business logic
|
|
5
5
|
*/
|
|
6
6
|
export { OptionsGreeksHistoryCustomResolver } from './OptionsGreeksHistoryCustomResolver';
|
|
7
|
+
export { BrokerageAccountCredentialStatusResolver, hasStoredApiCredentials, } from './BrokerageAccountCredentialStatusResolver';
|
|
7
8
|
export { OptionsGreeksHistorySystemSummary } from './OptionsGreeksHistorySystemSummary';
|
|
8
9
|
export { TradingSettingsResolver, TradingSettings, TradingSettingsInput, OrgTradingDefaultsPayload, FundTradingOverridesPayload, } from './TradingSettingsResolver';
|
|
9
10
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/resolvers/custom/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,kCAAkC,EAAE,MAAM,sCAAsC,CAAC;AAC1F,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,oBAAoB,EACpB,yBAAyB,EACzB,2BAA2B,GAC5B,MAAM,2BAA2B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/resolvers/custom/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,kCAAkC,EAAE,MAAM,sCAAsC,CAAC;AAC1F,OAAO,EACL,wCAAwC,EACxC,uBAAuB,GACxB,MAAM,4CAA4C,CAAC;AACpD,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,oBAAoB,EACpB,yBAAyB,EACzB,2BAA2B,GAC5B,MAAM,2BAA2B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/resolvers/custom/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,kCAAkC,EAAE,MAAM,sCAAsC,CAAC;AAC1F,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,oBAAoB,EACpB,yBAAyB,EACzB,2BAA2B,GAC5B,MAAM,2BAA2B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/resolvers/custom/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,kCAAkC,EAAE,MAAM,sCAAsC,CAAC;AAC1F,OAAO,EACL,wCAAwC,EACxC,uBAAuB,GACxB,MAAM,4CAA4C,CAAC;AACpD,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,oBAAoB,EACpB,yBAAyB,EACzB,2BAA2B,GAC5B,MAAM,2BAA2B,CAAC"}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* with custom aggregation queries and business logic
|
|
5
5
|
*/
|
|
6
6
|
export { OptionsGreeksHistoryCustomResolver } from './OptionsGreeksHistoryCustomResolver.mjs';
|
|
7
|
+
export { BrokerageAccountCredentialStatusResolver, hasStoredApiCredentials, } from './BrokerageAccountCredentialStatusResolver.mjs';
|
|
7
8
|
export { OptionsGreeksHistorySystemSummary } from './OptionsGreeksHistorySystemSummary.mjs';
|
|
8
9
|
export { TradingSettingsResolver, TradingSettings, TradingSettingsInput, OrgTradingDefaultsPayload, FundTradingOverridesPayload, } from './TradingSettingsResolver.mjs';
|
|
9
10
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
42
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
43
|
+
};
|
|
44
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
45
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.BrokerageAccountCredentialStatusResolver = void 0;
|
|
49
|
+
exports.hasStoredApiCredentials = hasStoredApiCredentials;
|
|
50
|
+
/**
|
|
51
|
+
* `BrokerageAccount.hasApiCredentials` — whether an account's broker API key
|
|
52
|
+
* and secret are both stored, without reading either one.
|
|
53
|
+
*
|
|
54
|
+
* The credential columns are readable only by a service principal (see
|
|
55
|
+
* `src/middleware/credential-field-guard.ts`), yet a client legitimately needs
|
|
56
|
+
* to show whether an account is configured. Selecting the secret to test it
|
|
57
|
+
* for emptiness would put the secret on the wire for a yes/no answer, so the
|
|
58
|
+
* answer is computed here, where the row is already in memory, and only the
|
|
59
|
+
* boolean leaves the server.
|
|
60
|
+
*
|
|
61
|
+
* @module resolvers/custom/BrokerageAccountCredentialStatusResolver
|
|
62
|
+
*/
|
|
63
|
+
const TypeGraphQL = __importStar(require("type-graphql"));
|
|
64
|
+
const BrokerageAccount_1 = require("../../generated/typegraphql-prisma/models/BrokerageAccount.cjs");
|
|
65
|
+
/**
|
|
66
|
+
* True only when both credential columns hold a non-blank value.
|
|
67
|
+
*
|
|
68
|
+
* @param account - The parent row as the generated resolvers loaded it.
|
|
69
|
+
* @returns Whether the account can authenticate to its broker.
|
|
70
|
+
*/
|
|
71
|
+
function hasStoredApiCredentials(account) {
|
|
72
|
+
const present = (value) => typeof value === 'string' && value.trim().length > 0;
|
|
73
|
+
return present(account.apiKey) && present(account.apiSecret);
|
|
74
|
+
}
|
|
75
|
+
/** Adds the computed `hasApiCredentials` field to `BrokerageAccount`. */
|
|
76
|
+
let BrokerageAccountCredentialStatusResolver = class BrokerageAccountCredentialStatusResolver {
|
|
77
|
+
/**
|
|
78
|
+
* Whether both the API key and secret are stored for this account.
|
|
79
|
+
*
|
|
80
|
+
* @param account - The parent `BrokerageAccount` row.
|
|
81
|
+
* @returns `true` when both credential columns are non-blank.
|
|
82
|
+
*/
|
|
83
|
+
hasApiCredentials(account) {
|
|
84
|
+
return hasStoredApiCredentials(account);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
exports.BrokerageAccountCredentialStatusResolver = BrokerageAccountCredentialStatusResolver;
|
|
88
|
+
__decorate([
|
|
89
|
+
TypeGraphQL.FieldResolver(() => Boolean, {
|
|
90
|
+
description: 'Whether both broker API credential columns are stored. Readable by any caller that can read the account; the credentials themselves are not.',
|
|
91
|
+
}),
|
|
92
|
+
__param(0, TypeGraphQL.Root()),
|
|
93
|
+
__metadata("design:type", Function),
|
|
94
|
+
__metadata("design:paramtypes", [BrokerageAccount_1.BrokerageAccount]),
|
|
95
|
+
__metadata("design:returntype", Boolean)
|
|
96
|
+
], BrokerageAccountCredentialStatusResolver.prototype, "hasApiCredentials", null);
|
|
97
|
+
exports.BrokerageAccountCredentialStatusResolver = BrokerageAccountCredentialStatusResolver = __decorate([
|
|
98
|
+
TypeGraphQL.Resolver(() => BrokerageAccount_1.BrokerageAccount)
|
|
99
|
+
], BrokerageAccountCredentialStatusResolver);
|
|
100
|
+
//# sourceMappingURL=BrokerageAccountCredentialStatusResolver.js.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { BrokerageAccount } from '../../generated/typegraphql-prisma/models/BrokerageAccount';
|
|
2
|
+
/** The two credential columns this resolver inspects on the parent row. */
|
|
3
|
+
type CredentialColumns = Pick<BrokerageAccount, 'apiKey' | 'apiSecret'>;
|
|
4
|
+
/**
|
|
5
|
+
* True only when both credential columns hold a non-blank value.
|
|
6
|
+
*
|
|
7
|
+
* @param account - The parent row as the generated resolvers loaded it.
|
|
8
|
+
* @returns Whether the account can authenticate to its broker.
|
|
9
|
+
*/
|
|
10
|
+
export declare function hasStoredApiCredentials(account: CredentialColumns): boolean;
|
|
11
|
+
/** Adds the computed `hasApiCredentials` field to `BrokerageAccount`. */
|
|
12
|
+
export declare class BrokerageAccountCredentialStatusResolver {
|
|
13
|
+
/**
|
|
14
|
+
* Whether both the API key and secret are stored for this account.
|
|
15
|
+
*
|
|
16
|
+
* @param account - The parent `BrokerageAccount` row.
|
|
17
|
+
* @returns `true` when both credential columns are non-blank.
|
|
18
|
+
*/
|
|
19
|
+
hasApiCredentials(account: BrokerageAccount): boolean;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=BrokerageAccountCredentialStatusResolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BrokerageAccountCredentialStatusResolver.d.ts","sourceRoot":"","sources":["../../../src/resolvers/custom/BrokerageAccountCredentialStatusResolver.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,gBAAgB,EAAE,MAAM,4DAA4D,CAAC;AAE9F,2EAA2E;AAC3E,KAAK,iBAAiB,GAAG,IAAI,CAAC,gBAAgB,EAAE,QAAQ,GAAG,WAAW,CAAC,CAAC;AAExE;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAI3E;AAED,yEAAyE;AACzE,qBACa,wCAAwC;IACnD;;;;;OAKG;IAKH,iBAAiB,CAAqB,OAAO,EAAE,gBAAgB,GAAG,OAAO;CAG1E"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BrokerageAccountCredentialStatusResolver.js","sourceRoot":"","sources":["../../../src/resolvers/custom/BrokerageAccountCredentialStatusResolver.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,0DAIC;AA9BD;;;;;;;;;;;;GAYG;AACH,0DAA4C;AAE5C,iGAA8F;AAK9F;;;;;GAKG;AACH,SAAgB,uBAAuB,CAAC,OAA0B;IAChE,MAAM,OAAO,GAAG,CAAC,KAAgC,EAAW,EAAE,CAC5D,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC;IACvD,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;AAC/D,CAAC;AAED,yEAAyE;AAElE,IAAM,wCAAwC,GAA9C,MAAM,wCAAwC;IACnD;;;;;OAKG;IAKH,iBAAiB,CAAqB,OAAyB;QAC7D,OAAO,uBAAuB,CAAC,OAAO,CAAC,CAAC;IAC1C,CAAC;CACF,CAAA;AAdY,4FAAwC;AAWnD;IAJC,WAAW,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE;QACxC,WAAW,EACT,8IAA8I;KACjJ,CAAC;IACiB,WAAA,WAAW,CAAC,IAAI,EAAE,CAAA;;qCAAU,mCAAgB;;iFAE9D;mDAbU,wCAAwC;IADpD,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,mCAAgB,CAAC;GAChC,wCAAwC,CAcpD"}
|
|
@@ -5,9 +5,12 @@
|
|
|
5
5
|
* with custom aggregation queries and business logic
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.FundTradingOverridesPayload = exports.OrgTradingDefaultsPayload = exports.TradingSettingsInput = exports.TradingSettings = exports.TradingSettingsResolver = exports.OptionsGreeksHistorySystemSummary = exports.OptionsGreeksHistoryCustomResolver = void 0;
|
|
8
|
+
exports.FundTradingOverridesPayload = exports.OrgTradingDefaultsPayload = exports.TradingSettingsInput = exports.TradingSettings = exports.TradingSettingsResolver = exports.OptionsGreeksHistorySystemSummary = exports.hasStoredApiCredentials = exports.BrokerageAccountCredentialStatusResolver = exports.OptionsGreeksHistoryCustomResolver = void 0;
|
|
9
9
|
var OptionsGreeksHistoryCustomResolver_1 = require("./OptionsGreeksHistoryCustomResolver.cjs");
|
|
10
10
|
Object.defineProperty(exports, "OptionsGreeksHistoryCustomResolver", { enumerable: true, get: function () { return OptionsGreeksHistoryCustomResolver_1.OptionsGreeksHistoryCustomResolver; } });
|
|
11
|
+
var BrokerageAccountCredentialStatusResolver_1 = require("./BrokerageAccountCredentialStatusResolver.cjs");
|
|
12
|
+
Object.defineProperty(exports, "BrokerageAccountCredentialStatusResolver", { enumerable: true, get: function () { return BrokerageAccountCredentialStatusResolver_1.BrokerageAccountCredentialStatusResolver; } });
|
|
13
|
+
Object.defineProperty(exports, "hasStoredApiCredentials", { enumerable: true, get: function () { return BrokerageAccountCredentialStatusResolver_1.hasStoredApiCredentials; } });
|
|
11
14
|
var OptionsGreeksHistorySystemSummary_1 = require("./OptionsGreeksHistorySystemSummary.cjs");
|
|
12
15
|
Object.defineProperty(exports, "OptionsGreeksHistorySystemSummary", { enumerable: true, get: function () { return OptionsGreeksHistorySystemSummary_1.OptionsGreeksHistorySystemSummary; } });
|
|
13
16
|
var TradingSettingsResolver_1 = require("./TradingSettingsResolver.cjs");
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* with custom aggregation queries and business logic
|
|
5
5
|
*/
|
|
6
6
|
export { OptionsGreeksHistoryCustomResolver } from './OptionsGreeksHistoryCustomResolver';
|
|
7
|
+
export { BrokerageAccountCredentialStatusResolver, hasStoredApiCredentials, } from './BrokerageAccountCredentialStatusResolver';
|
|
7
8
|
export { OptionsGreeksHistorySystemSummary } from './OptionsGreeksHistorySystemSummary';
|
|
8
9
|
export { TradingSettingsResolver, TradingSettings, TradingSettingsInput, OrgTradingDefaultsPayload, FundTradingOverridesPayload, } from './TradingSettingsResolver';
|
|
9
10
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/resolvers/custom/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,kCAAkC,EAAE,MAAM,sCAAsC,CAAC;AAC1F,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,oBAAoB,EACpB,yBAAyB,EACzB,2BAA2B,GAC5B,MAAM,2BAA2B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/resolvers/custom/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,kCAAkC,EAAE,MAAM,sCAAsC,CAAC;AAC1F,OAAO,EACL,wCAAwC,EACxC,uBAAuB,GACxB,MAAM,4CAA4C,CAAC;AACpD,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,oBAAoB,EACpB,yBAAyB,EACzB,2BAA2B,GAC5B,MAAM,2BAA2B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/resolvers/custom/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,2FAA0F;AAAjF,wJAAA,kCAAkC,OAAA;AAC3C,yFAAwF;AAA/E,sJAAA,iCAAiC,OAAA;AAC1C,qEAMmC;AALjC,kIAAA,uBAAuB,OAAA;AACvB,0HAAA,eAAe,OAAA;AACf,+HAAA,oBAAoB,OAAA;AACpB,oIAAA,yBAAyB,OAAA;AACzB,sIAAA,2BAA2B,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/resolvers/custom/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,2FAA0F;AAAjF,wJAAA,kCAAkC,OAAA;AAC3C,uGAGoD;AAFlD,oKAAA,wCAAwC,OAAA;AACxC,mJAAA,uBAAuB,OAAA;AAEzB,yFAAwF;AAA/E,sJAAA,iCAAiC,OAAA;AAC1C,qEAMmC;AALjC,kIAAA,uBAAuB,OAAA;AACvB,0HAAA,eAAe,OAAA;AACf,+HAAA,oBAAoB,OAAA;AACpB,oIAAA,yBAAyB,OAAA;AACzB,sIAAA,2BAA2B,OAAA"}
|
package/server.cjs
CHANGED
|
@@ -62,6 +62,7 @@ const auth_1 = require("./middleware/auth.cjs");
|
|
|
62
62
|
const rate_limiter_1 = require("./middleware/rate-limiter.cjs");
|
|
63
63
|
const audit_logger_1 = require("./middleware/audit-logger.cjs");
|
|
64
64
|
const tenancy_scoping_1 = require("./middleware/tenancy-scoping.cjs");
|
|
65
|
+
const credential_field_guard_1 = require("./middleware/credential-field-guard.cjs");
|
|
65
66
|
const cortex_auth_checker_1 = require("./auth/cortex-auth-checker.cjs");
|
|
66
67
|
const authorization_map_1 = require("./auth/authorization-map.cjs");
|
|
67
68
|
const http_status_mapper_1 = require("./plugins/http-status-mapper.cjs");
|
|
@@ -143,13 +144,24 @@ const startServer = async () => {
|
|
|
143
144
|
...typegraphql_prisma_1.resolvers,
|
|
144
145
|
custom_1.OptionsGreeksHistoryCustomResolver,
|
|
145
146
|
custom_1.TradingSettingsResolver,
|
|
147
|
+
custom_1.BrokerageAccountCredentialStatusResolver,
|
|
146
148
|
],
|
|
147
149
|
validate: false,
|
|
148
150
|
// Row-level tenancy scoping (SP2-G7 / SOC2). Applies ONLY to user-scoped
|
|
149
151
|
// principals on the tenancy + notification models; service/admin principals
|
|
150
152
|
// and unauthenticated callers are bypassed in every mode. Gated by
|
|
151
153
|
// `TENANCY_SCOPING_MODE` (default `shadow`).
|
|
152
|
-
|
|
154
|
+
//
|
|
155
|
+
// Credential-field guard: stored credentials (broker API keys, OAuth,
|
|
156
|
+
// session and invite tokens) resolve — and can be filtered, sorted or
|
|
157
|
+
// grouped on — only for a verified service principal. It runs FIRST so a
|
|
158
|
+
// credential predicate is refused before any other middleware or resolver
|
|
159
|
+
// touches the database. Gated by `CREDENTIAL_FIELD_GUARD_MODE` (default
|
|
160
|
+
// `enforce`); see src/middleware/credential-field-guard.ts.
|
|
161
|
+
globalMiddlewares: [
|
|
162
|
+
(0, credential_field_guard_1.createCredentialFieldGuardMiddleware)(),
|
|
163
|
+
(0, tenancy_scoping_1.createTenancyScopingMiddleware)(),
|
|
164
|
+
],
|
|
153
165
|
// Resolver-level authorization (CORTEX-P0-001). Invoked for the
|
|
154
166
|
// `@Authorized()`-decorated fields applied by applyCortexAuthorizationMap
|
|
155
167
|
// above. SHADOW-FIRST: while `CORTEX_AUTHCHECKER_ENFORCE` is OFF (default),
|