@contember/engine-http 2.2.0-alpha.17 → 2.2.0-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/index.cjs +3 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +3 -0
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/MasterContainer.cjs +5 -4
- package/dist/development/src/MasterContainer.cjs.map +1 -1
- package/dist/development/src/MasterContainer.js +5 -4
- package/dist/development/src/MasterContainer.js.map +1 -1
- package/dist/development/src/application/PanelApiMount.cjs.map +1 -1
- package/dist/development/src/application/PanelApiMount.js.map +1 -1
- package/dist/development/src/application/application.cjs +19 -12
- package/dist/development/src/application/application.cjs.map +1 -1
- package/dist/development/src/application/application.js +19 -12
- package/dist/development/src/application/application.js.map +1 -1
- package/dist/development/src/application/requestBodyMasking.cjs +97 -0
- package/dist/development/src/application/requestBodyMasking.cjs.map +1 -0
- package/dist/development/src/application/requestBodyMasking.js +97 -0
- package/dist/development/src/application/requestBodyMasking.js.map +1 -0
- package/dist/development/src/config/configSchema.cjs +28 -0
- package/dist/development/src/config/configSchema.cjs.map +1 -1
- package/dist/development/src/config/configSchema.js +28 -0
- package/dist/development/src/config/configSchema.js.map +1 -1
- package/dist/development/src/config/configTemplate.cjs +5 -0
- package/dist/development/src/config/configTemplate.cjs.map +1 -1
- package/dist/development/src/config/configTemplate.js +5 -0
- package/dist/development/src/config/configTemplate.js.map +1 -1
- package/dist/development/src/content/ProjectMembershipResolver.cjs +10 -3
- package/dist/development/src/content/ProjectMembershipResolver.cjs.map +1 -1
- package/dist/development/src/content/ProjectMembershipResolver.js +10 -3
- package/dist/development/src/content/ProjectMembershipResolver.js.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.cjs +2 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.js +2 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.js.map +1 -1
- package/dist/development/src/transfer/ImportExecutor.cjs +15 -2
- package/dist/development/src/transfer/ImportExecutor.cjs.map +1 -1
- package/dist/development/src/transfer/ImportExecutor.js +15 -2
- package/dist/development/src/transfer/ImportExecutor.js.map +1 -1
- package/dist/development/src/utils/CryptoWrapper.cjs +11 -0
- package/dist/development/src/utils/CryptoWrapper.cjs.map +1 -1
- package/dist/development/src/utils/CryptoWrapper.js +11 -0
- package/dist/development/src/utils/CryptoWrapper.js.map +1 -1
- package/dist/development/src/utils/sentry.cjs +1 -0
- package/dist/development/src/utils/sentry.cjs.map +1 -1
- package/dist/development/src/utils/sentry.js +1 -0
- package/dist/development/src/utils/sentry.js.map +1 -1
- package/dist/production/index.cjs +3 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +3 -0
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/MasterContainer.cjs +5 -4
- package/dist/production/src/MasterContainer.cjs.map +1 -1
- package/dist/production/src/MasterContainer.js +5 -4
- package/dist/production/src/MasterContainer.js.map +1 -1
- package/dist/production/src/application/PanelApiMount.cjs.map +1 -1
- package/dist/production/src/application/PanelApiMount.js.map +1 -1
- package/dist/production/src/application/application.cjs +19 -12
- package/dist/production/src/application/application.cjs.map +1 -1
- package/dist/production/src/application/application.js +19 -12
- package/dist/production/src/application/application.js.map +1 -1
- package/dist/production/src/application/requestBodyMasking.cjs +97 -0
- package/dist/production/src/application/requestBodyMasking.cjs.map +1 -0
- package/dist/production/src/application/requestBodyMasking.js +97 -0
- package/dist/production/src/application/requestBodyMasking.js.map +1 -0
- package/dist/production/src/config/configSchema.cjs +28 -0
- package/dist/production/src/config/configSchema.cjs.map +1 -1
- package/dist/production/src/config/configSchema.js +28 -0
- package/dist/production/src/config/configSchema.js.map +1 -1
- package/dist/production/src/config/configTemplate.cjs +5 -0
- package/dist/production/src/config/configTemplate.cjs.map +1 -1
- package/dist/production/src/config/configTemplate.js +5 -0
- package/dist/production/src/config/configTemplate.js.map +1 -1
- package/dist/production/src/content/ProjectMembershipResolver.cjs +10 -3
- package/dist/production/src/content/ProjectMembershipResolver.cjs.map +1 -1
- package/dist/production/src/content/ProjectMembershipResolver.js +10 -3
- package/dist/production/src/content/ProjectMembershipResolver.js.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.cjs +2 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.js +2 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.js.map +1 -1
- package/dist/production/src/transfer/ImportExecutor.cjs +15 -2
- package/dist/production/src/transfer/ImportExecutor.cjs.map +1 -1
- package/dist/production/src/transfer/ImportExecutor.js +15 -2
- package/dist/production/src/transfer/ImportExecutor.js.map +1 -1
- package/dist/production/src/utils/CryptoWrapper.cjs +11 -0
- package/dist/production/src/utils/CryptoWrapper.cjs.map +1 -1
- package/dist/production/src/utils/CryptoWrapper.js +11 -0
- package/dist/production/src/utils/CryptoWrapper.js.map +1 -1
- package/dist/production/src/utils/sentry.cjs +1 -0
- package/dist/production/src/utils/sentry.cjs.map +1 -1
- package/dist/production/src/utils/sentry.js +1 -0
- package/dist/production/src/utils/sentry.js.map +1 -1
- package/dist/tests/cases/unit/requestBodyLogging.test.d.ts +2 -0
- package/dist/tests/cases/unit/requestBodyLogging.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/requestBodyMasking.test.d.ts +2 -0
- package/dist/tests/cases/unit/requestBodyMasking.test.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/MasterContainer.d.ts +7 -0
- package/dist/types/MasterContainer.d.ts.map +1 -1
- package/dist/types/application/PanelApiMount.d.ts +2 -0
- package/dist/types/application/PanelApiMount.d.ts.map +1 -1
- package/dist/types/application/application.d.ts +7 -1
- package/dist/types/application/application.d.ts.map +1 -1
- package/dist/types/application/index.d.ts +1 -0
- package/dist/types/application/index.d.ts.map +1 -1
- package/dist/types/application/requestBodyMasking.d.ts +10 -0
- package/dist/types/application/requestBodyMasking.d.ts.map +1 -0
- package/dist/types/config/configSchema.d.ts +10 -0
- package/dist/types/config/configSchema.d.ts.map +1 -1
- package/dist/types/config/configTemplate.d.ts.map +1 -1
- package/dist/types/content/ProjectMembershipResolver.d.ts.map +1 -1
- package/dist/types/projectGroup/ProjectGroupContainer.d.ts.map +1 -1
- package/dist/types/transfer/ImportExecutor.d.ts +2 -0
- package/dist/types/transfer/ImportExecutor.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/utils/CryptoWrapper.d.ts +5 -0
- package/dist/types/utils/CryptoWrapper.d.ts.map +1 -1
- package/package.json +16 -16
- package/src/MasterContainer.ts +5 -3
- package/src/application/PanelApiMount.ts +2 -0
- package/src/application/application.ts +30 -11
- package/src/application/index.ts +1 -0
- package/src/application/requestBodyMasking.ts +117 -0
- package/src/config/configSchema.ts +30 -0
- package/src/config/configTemplate.ts +5 -0
- package/src/content/ProjectMembershipResolver.ts +13 -5
- package/src/projectGroup/ProjectGroupContainer.ts +1 -0
- package/src/transfer/ImportExecutor.ts +16 -2
- package/src/utils/CryptoWrapper.ts +12 -0
- package/tests/cases/unit/crypto.test.ts +16 -0
- package/tests/cases/unit/projectMembershipResolver.test.ts +50 -0
- package/tests/cases/unit/requestBodyLogging.test.ts +89 -0
- package/tests/cases/unit/requestBodyMasking.test.ts +102 -0
- package/tests/cases/unit/serverConfig.test.ts +23 -0
|
@@ -10,6 +10,11 @@ export declare class CryptoWrapper {
|
|
|
10
10
|
value: Buffer;
|
|
11
11
|
version: number;
|
|
12
12
|
}>;
|
|
13
|
+
/**
|
|
14
|
+
* HMAC-SHA256 under a key derived (HKDF) from the encryption key for `purpose`, so signatures never use the
|
|
15
|
+
* encryption key itself and a signature made for one purpose does not verify for another.
|
|
16
|
+
*/
|
|
17
|
+
sign(purpose: string, value: Buffer): Buffer;
|
|
13
18
|
decrypt(value: Buffer, version: number): Promise<{
|
|
14
19
|
value: Buffer;
|
|
15
20
|
needsReEncrypt: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CryptoWrapper.d.ts","sourceRoot":"","sources":["../../../src/utils/CryptoWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC,qBAAa,aAAa;IAKxB,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC;IAJhC,OAAc,UAAU,EAAG,aAAa,CAAS;IACjD,OAAc,aAAa,SAAI;IAE/B,YACkB,aAAa,CAAC,EAAE,MAAM,CAAC,SAAS,YAAA,EAEjD;IAED,oFAAoF;IACpF,IAAI,OAAO,IAAI,OAAO,CAErB;IAEK,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAWxE;IAEK,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,cAAc,EAAE,OAAO,CAAA;KAAE,CAAC,CAmBjG;CACD"}
|
|
1
|
+
{"version":3,"file":"CryptoWrapper.d.ts","sourceRoot":"","sources":["../../../src/utils/CryptoWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC,qBAAa,aAAa;IAKxB,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC;IAJhC,OAAc,UAAU,EAAG,aAAa,CAAS;IACjD,OAAc,aAAa,SAAI;IAE/B,YACkB,aAAa,CAAC,EAAE,MAAM,CAAC,SAAS,YAAA,EAEjD;IAED,oFAAoF;IACpF,IAAI,OAAO,IAAI,OAAO,CAErB;IAEK,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAWxE;IAED;;;OAGG;IACH,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAM3C;IAEK,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,cAAc,EAAE,OAAO,CAAA;KAAE,CAAC,CAmBjG;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-http",
|
|
3
|
-
"version": "2.2.0-alpha.
|
|
3
|
+
"version": "2.2.0-alpha.18",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "./dist/production/index.js",
|
|
6
6
|
"typings": "./dist/types/index.d.ts",
|
|
@@ -26,21 +26,21 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@contember/config-loader": "2.2.0-alpha.
|
|
30
|
-
"@contember/database": "2.2.0-alpha.
|
|
31
|
-
"@contember/database-migrations": "2.2.0-alpha.
|
|
32
|
-
"@contember/dic": "2.2.0-alpha.
|
|
33
|
-
"@contember/engine-common": "2.2.0-alpha.
|
|
34
|
-
"@contember/engine-content-api": "2.2.0-alpha.
|
|
35
|
-
"@contember/engine-system-api": "2.2.0-alpha.
|
|
36
|
-
"@contember/engine-tenant-api": "2.2.0-alpha.
|
|
37
|
-
"@contember/graphql-utils": "2.2.0-alpha.
|
|
38
|
-
"@contember/logger": "2.2.0-alpha.
|
|
39
|
-
"@contember/schema": "2.2.0-alpha.
|
|
40
|
-
"@contember/schema-migrations": "2.2.0-alpha.
|
|
41
|
-
"@contember/schema-utils": "2.2.0-alpha.
|
|
42
|
-
"@contember/telemetry": "2.2.0-alpha.
|
|
43
|
-
"@contember/typesafe": "2.2.0-alpha.
|
|
29
|
+
"@contember/config-loader": "2.2.0-alpha.18",
|
|
30
|
+
"@contember/database": "2.2.0-alpha.18",
|
|
31
|
+
"@contember/database-migrations": "2.2.0-alpha.18",
|
|
32
|
+
"@contember/dic": "2.2.0-alpha.18",
|
|
33
|
+
"@contember/engine-common": "2.2.0-alpha.18",
|
|
34
|
+
"@contember/engine-content-api": "2.2.0-alpha.18",
|
|
35
|
+
"@contember/engine-system-api": "2.2.0-alpha.18",
|
|
36
|
+
"@contember/engine-tenant-api": "2.2.0-alpha.18",
|
|
37
|
+
"@contember/graphql-utils": "2.2.0-alpha.18",
|
|
38
|
+
"@contember/logger": "2.2.0-alpha.18",
|
|
39
|
+
"@contember/schema": "2.2.0-alpha.18",
|
|
40
|
+
"@contember/schema-migrations": "2.2.0-alpha.18",
|
|
41
|
+
"@contember/schema-utils": "2.2.0-alpha.18",
|
|
42
|
+
"@contember/telemetry": "2.2.0-alpha.18",
|
|
43
|
+
"@contember/typesafe": "2.2.0-alpha.18",
|
|
44
44
|
"@graphql-tools/merge": "^9.1.7",
|
|
45
45
|
"@graphql-tools/schema": "^10.0.31",
|
|
46
46
|
"@koa/cors": "^5.0.0",
|
package/src/MasterContainer.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Builder } from '@contember/dic'
|
|
2
|
+
import { createEgressGuard } from '@contember/engine-common'
|
|
2
3
|
import { SystemContainerFactory } from '@contember/engine-system-api'
|
|
3
4
|
import { TenantContainerFactory } from '@contember/engine-tenant-api'
|
|
4
5
|
import { ModificationHandlerFactory } from '@contember/schema-migrations'
|
|
@@ -115,7 +116,8 @@ export class MasterContainerFactory {
|
|
|
115
116
|
.addService('tenantConfigResolver', () => tenantConfigResolver)
|
|
116
117
|
.addService('plugins', () => plugins)
|
|
117
118
|
.addService('providers', () => createProviders())
|
|
118
|
-
.addService('
|
|
119
|
+
.addService('egressGuard', ({ serverConfig }) => createEgressGuard(serverConfig.egress))
|
|
120
|
+
.addService('tenantContainerFactory', ({ providers, egressGuard }) => new TenantContainerFactory(providers, egressGuard))
|
|
119
121
|
.addService('modificationHandlerFactory', () => new ModificationHandlerFactory(ModificationHandlerFactory.defaultFactoryMap))
|
|
120
122
|
.addService(
|
|
121
123
|
'executionContainerFactory',
|
|
@@ -320,12 +322,12 @@ export class MasterContainerFactory {
|
|
|
320
322
|
},
|
|
321
323
|
) => {
|
|
322
324
|
it.addRoute('content', '/content/:projectSlug/:stageSlug', contentApiMiddlewareFactory.create())
|
|
323
|
-
it.addRoute('tenant', '/tenant', tenantApiMiddlewareFactory.create())
|
|
325
|
+
it.addRoute('tenant', '/tenant', tenantApiMiddlewareFactory.create(), { carriesCredentials: true })
|
|
324
326
|
// Register under a dedicated, non-GraphQL module so it is excluded from `forceHttpOk`
|
|
325
327
|
// status coercion (graphqlModules = content/tenant/system): the OIDC Back-Channel Logout
|
|
326
328
|
// response is not a GraphQL `{data,errors}` envelope and its HTTP status is part of the
|
|
327
329
|
// spec contract (the IdP relies on a non-2xx status to detect/retry a failed logout).
|
|
328
|
-
it.addRoute('oidc', '/oidc/backchannel-logout', oidcBackchannelLogoutMiddlewareFactory.create())
|
|
330
|
+
it.addRoute('oidc', '/oidc/backchannel-logout', oidcBackchannelLogoutMiddlewareFactory.create(), { carriesCredentials: true })
|
|
329
331
|
it.addRoute('system', '/system/:projectSlug', systemApiMiddlewareFactory.create())
|
|
330
332
|
it.addRoute('transfer', '/import', importApiMiddlewareFactory.create())
|
|
331
333
|
it.addRoute('transfer', '/export', exportApiMiddlewareFactory.create())
|
|
@@ -6,6 +6,8 @@ export interface PanelApiMountArgs {
|
|
|
6
6
|
/** Route mask relative to the panel's API root, e.g. `actions/:projectSlug`. */
|
|
7
7
|
readonly path: string
|
|
8
8
|
readonly controller: HttpController
|
|
9
|
+
/** The API receives credentials, so its logged request bodies are masked; see `RouteOptions`. */
|
|
10
|
+
readonly carriesCredentials?: boolean
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
/** What {@link PanelApiMount.mount} needs of the application it registers into. */
|
|
@@ -20,10 +20,16 @@ import { getClientIP } from '../utils/remoteAddress.js'
|
|
|
20
20
|
import { isForceHttpOkRequested, shouldForceHttpOk } from './forceHttpOk.js'
|
|
21
21
|
import { INVALID_TRACE_ID, noopTracer, Span, SpanContext, Tracer, withSpanContext } from '@contember/telemetry'
|
|
22
22
|
import { AcceptIncomingTraceMode, resolveIncomingSpanContext } from '../telemetry/incomingTrace.js'
|
|
23
|
+
import { maskRequestBody, maskRequestUri } from './requestBodyMasking.js'
|
|
23
24
|
|
|
24
25
|
const websocketCloseTimeoutMs = 5_000
|
|
25
26
|
|
|
26
|
-
type Route<C> = { match: RequestMatcher; controller: C; module: string; mask: string }
|
|
27
|
+
type Route<C> = { match: RequestMatcher; controller: C; module: string; mask: string; carriesCredentials: boolean }
|
|
28
|
+
|
|
29
|
+
export type RouteOptions = {
|
|
30
|
+
/** The route receives engine credentials (passwords, OTP codes, tokens), so its body and URI are masked in logs. */
|
|
31
|
+
readonly carriesCredentials?: boolean
|
|
32
|
+
}
|
|
27
33
|
export class Application {
|
|
28
34
|
private middlewares: KoaMiddleware<any>[] = []
|
|
29
35
|
|
|
@@ -57,12 +63,13 @@ export class Application {
|
|
|
57
63
|
this.middlewares.push(middleware)
|
|
58
64
|
}
|
|
59
65
|
|
|
60
|
-
public addRoute(module: string, mask: string, controller: HttpController): void {
|
|
66
|
+
public addRoute(module: string, mask: string, controller: HttpController, options: RouteOptions = {}): void {
|
|
61
67
|
this.routes.push({
|
|
62
68
|
module,
|
|
63
69
|
controller,
|
|
64
70
|
mask,
|
|
65
71
|
match: createRequestMatcher(mask),
|
|
72
|
+
carriesCredentials: options.carriesCredentials ?? false,
|
|
66
73
|
})
|
|
67
74
|
}
|
|
68
75
|
|
|
@@ -72,6 +79,7 @@ export class Application {
|
|
|
72
79
|
controller,
|
|
73
80
|
mask,
|
|
74
81
|
match: createRequestMatcher(mask),
|
|
82
|
+
carriesCredentials: false,
|
|
75
83
|
})
|
|
76
84
|
}
|
|
77
85
|
|
|
@@ -81,6 +89,7 @@ export class Application {
|
|
|
81
89
|
controller,
|
|
82
90
|
mask,
|
|
83
91
|
match: createRequestMatcher(mask),
|
|
92
|
+
carriesCredentials: false,
|
|
84
93
|
})
|
|
85
94
|
}
|
|
86
95
|
|
|
@@ -203,7 +212,7 @@ export class Application {
|
|
|
203
212
|
return await this.tracer.withSpan(span, async () => {
|
|
204
213
|
const pendingWork: Promise<void>[] = []
|
|
205
214
|
try {
|
|
206
|
-
requestLogger = this.createRequestLogger(req, undefined, matchedRequest.module, clientIp, span.context)
|
|
215
|
+
requestLogger = this.createRequestLogger(req, undefined, false, matchedRequest.module, clientIp, span.context)
|
|
207
216
|
const groupContainer = await this.projectGroupResolver.resolveContainer({ request: req })
|
|
208
217
|
requestLogger = requestLogger.child({ projectGroup: groupContainer.slug, project: matchedRequest.params.projectSlug })
|
|
209
218
|
span.setAttribute('contember.project_group', groupContainer.slug ?? 'unknown')
|
|
@@ -355,7 +364,9 @@ export class Application {
|
|
|
355
364
|
) {
|
|
356
365
|
let httpContext: HttpContext | null = null
|
|
357
366
|
const clientIp = getClientIP(ctx.req, this.trustedProxies)
|
|
358
|
-
|
|
367
|
+
const carriesCredentials = matchedRequest?.carriesCredentials ?? false
|
|
368
|
+
const loggedBody = maskRequestBody(ctx.request.body, carriesCredentials)
|
|
369
|
+
let requestLogger = this.createRequestLogger(ctx.req, loggedBody, carriesCredentials, matchedRequest?.module, clientIp, span?.context)
|
|
359
370
|
const { timer, send: sendTimer } = this.createTimer()
|
|
360
371
|
|
|
361
372
|
try {
|
|
@@ -363,8 +374,8 @@ export class Application {
|
|
|
363
374
|
return this.sendHttpResponse(ctx, new HttpErrorResponse(404, 'Route not found'))
|
|
364
375
|
}
|
|
365
376
|
requestLogger.debug('Request processing started', {
|
|
366
|
-
body:
|
|
367
|
-
query: ctx.request.query,
|
|
377
|
+
body: loggedBody,
|
|
378
|
+
query: maskRequestBody(ctx.request.query, carriesCredentials),
|
|
368
379
|
})
|
|
369
380
|
|
|
370
381
|
// todo: solve without koa context (required by prom metrics)
|
|
@@ -564,18 +575,26 @@ export class Application {
|
|
|
564
575
|
)
|
|
565
576
|
}
|
|
566
577
|
|
|
567
|
-
|
|
578
|
+
/** `loggedBody` must already be masked (`maskRequestBody`): it reaches logs and error reports. */
|
|
579
|
+
private createRequestLogger(
|
|
580
|
+
request: IncomingMessage,
|
|
581
|
+
loggedBody: unknown,
|
|
582
|
+
carriesCredentials: boolean,
|
|
583
|
+
module?: string,
|
|
584
|
+
remoteAddress?: string,
|
|
585
|
+
spanContext?: SpanContext,
|
|
586
|
+
): Logger {
|
|
568
587
|
const traceAttributes = spanContext === undefined || spanContext.traceId === INVALID_TRACE_ID
|
|
569
588
|
? {}
|
|
570
589
|
: { traceId: spanContext.traceId, spanId: spanContext.spanId }
|
|
571
590
|
return this.logger.child({
|
|
572
591
|
method: request.method,
|
|
573
|
-
uri: request.url,
|
|
592
|
+
uri: maskRequestUri(request.url, carriesCredentials),
|
|
574
593
|
requestId: Math.random().toString().substring(2),
|
|
575
594
|
module,
|
|
576
595
|
remoteAddress,
|
|
577
596
|
...traceAttributes,
|
|
578
|
-
[LoggerRequestBody]:
|
|
597
|
+
[LoggerRequestBody]: loggedBody,
|
|
579
598
|
}, {
|
|
580
599
|
handler: FingerCrossedLoggerHandler.factory(),
|
|
581
600
|
})
|
|
@@ -638,7 +657,7 @@ export class Application {
|
|
|
638
657
|
for (const route of routes) {
|
|
639
658
|
const params = route.match({ url })
|
|
640
659
|
if (params !== null) {
|
|
641
|
-
return { params, controller: route.controller, module: route.module, mask: route.mask }
|
|
660
|
+
return { params, controller: route.controller, module: route.module, mask: route.mask, carriesCredentials: route.carriesCredentials }
|
|
642
661
|
}
|
|
643
662
|
}
|
|
644
663
|
return null
|
|
@@ -690,7 +709,7 @@ const createTracedReadable = (body: Readable, tracer: Tracer, span: Span): Reada
|
|
|
690
709
|
/** @deprecated use `tracer` from the request context instead */
|
|
691
710
|
export type Timer = <T>(event: string, cb: () => T) => T
|
|
692
711
|
|
|
693
|
-
export type MatchedRequest<C> = { params: Params; controller: C; module: string; mask: string }
|
|
712
|
+
export type MatchedRequest<C> = { params: Params; controller: C; module: string; mask: string; carriesCredentials: boolean }
|
|
694
713
|
|
|
695
714
|
export type BaseRequestContext = {
|
|
696
715
|
logger: Logger
|
package/src/application/index.ts
CHANGED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ASTVisitor, Kind, parse, print, ValueNode, visit } from 'graphql'
|
|
2
|
+
import { maskSecrets, REDACTED, SECRET_ARGUMENTS_BY_FIELD, SECRET_FIELD_NAMES } from '@contember/engine-tenant-api'
|
|
3
|
+
|
|
4
|
+
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
5
|
+
|
|
6
|
+
// A query that names none of these cannot reference a secret argument (GraphQL names have no escapes), so it is kept unparsed.
|
|
7
|
+
const secretNamePattern = new RegExp([...SECRET_FIELD_NAMES, ...Object.keys(SECRET_ARGUMENTS_BY_FIELD)].join('|'))
|
|
8
|
+
|
|
9
|
+
type MaskedQuery = {
|
|
10
|
+
readonly query: string
|
|
11
|
+
/** Variables bound to a secret argument; `all` when the query could not be read. */
|
|
12
|
+
readonly secretVariables: ReadonlySet<string> | 'all'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const maskQuery = (query: string): MaskedQuery => {
|
|
16
|
+
if (!secretNamePattern.test(query)) {
|
|
17
|
+
return { query, secretVariables: new Set() }
|
|
18
|
+
}
|
|
19
|
+
let document
|
|
20
|
+
try {
|
|
21
|
+
document = parse(query)
|
|
22
|
+
} catch {
|
|
23
|
+
// An unparsable query may still inline a secret literal or bind one to any variable.
|
|
24
|
+
return { query: REDACTED, secretVariables: 'all' }
|
|
25
|
+
}
|
|
26
|
+
const secretVariables = new Set<string>()
|
|
27
|
+
let redacted = false
|
|
28
|
+
const collectVariables: ASTVisitor = {
|
|
29
|
+
Variable: node => {
|
|
30
|
+
secretVariables.add(node.name.value)
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
const redact = (value: ValueNode): ValueNode => {
|
|
34
|
+
visit(value, collectVariables)
|
|
35
|
+
if (value.kind === Kind.VARIABLE) {
|
|
36
|
+
return value
|
|
37
|
+
}
|
|
38
|
+
redacted = true
|
|
39
|
+
return { kind: Kind.STRING, value: REDACTED }
|
|
40
|
+
}
|
|
41
|
+
const withLiteralsMasked = visit(document, {
|
|
42
|
+
Field: node => {
|
|
43
|
+
const secretArguments = SECRET_ARGUMENTS_BY_FIELD[node.name.value]
|
|
44
|
+
if (secretArguments === undefined || node.arguments === undefined) {
|
|
45
|
+
return undefined
|
|
46
|
+
}
|
|
47
|
+
return { ...node, arguments: node.arguments.map(arg => secretArguments.has(arg.name.value) ? { ...arg, value: redact(arg.value) } : arg) }
|
|
48
|
+
},
|
|
49
|
+
Argument: node => SECRET_FIELD_NAMES.has(node.name.value) ? { ...node, value: redact(node.value) } : undefined,
|
|
50
|
+
ObjectField: node => SECRET_FIELD_NAMES.has(node.name.value) ? { ...node, value: redact(node.value) } : undefined,
|
|
51
|
+
})
|
|
52
|
+
// Variable definitions precede the selections that reveal which variables are secret, hence a second pass.
|
|
53
|
+
const masked = visit(withLiteralsMasked, {
|
|
54
|
+
VariableDefinition: node => {
|
|
55
|
+
if (node.defaultValue === undefined || !secretVariables.has(node.variable.name.value)) {
|
|
56
|
+
return undefined
|
|
57
|
+
}
|
|
58
|
+
redacted = true
|
|
59
|
+
return { ...node, defaultValue: { kind: Kind.STRING, value: REDACTED } }
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
// Re-printing changes the formatting, so the query is kept verbatim unless a literal was masked.
|
|
63
|
+
return { query: redacted ? print(masked) : query, secretVariables }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const maskVariables = (variables: unknown, secretVariables: ReadonlySet<string> | 'all'): unknown => {
|
|
67
|
+
if (typeof variables === 'string') {
|
|
68
|
+
// GraphQL over GET sends the variables as a JSON string.
|
|
69
|
+
try {
|
|
70
|
+
return maskVariables(JSON.parse(variables), secretVariables)
|
|
71
|
+
} catch {
|
|
72
|
+
return REDACTED
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (secretVariables === 'all') {
|
|
76
|
+
return maskSecrets(variables, true)
|
|
77
|
+
}
|
|
78
|
+
if (!isRecord(variables)) {
|
|
79
|
+
return maskSecrets(variables)
|
|
80
|
+
}
|
|
81
|
+
return Object.fromEntries(Object.entries(variables).map(([name, value]) => [name, maskSecrets(value, secretVariables.has(name))]))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const maskGraphQLRequest = (body: unknown): unknown => {
|
|
85
|
+
if (Array.isArray(body)) {
|
|
86
|
+
return body.map(maskGraphQLRequest)
|
|
87
|
+
}
|
|
88
|
+
if (!isRecord(body) || typeof body.query !== 'string') {
|
|
89
|
+
return maskSecrets(body)
|
|
90
|
+
}
|
|
91
|
+
const { query, secretVariables } = maskQuery(body.query)
|
|
92
|
+
return Object.fromEntries(
|
|
93
|
+
Object.entries(body).map(([key, value]) => {
|
|
94
|
+
if (key === 'query') {
|
|
95
|
+
return [key, query]
|
|
96
|
+
}
|
|
97
|
+
return [key, key === 'variables' ? maskVariables(value, secretVariables) : maskSecrets(value, SECRET_FIELD_NAMES.has(key))]
|
|
98
|
+
}),
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* A request body (or the parsed query string) as logs and error reports may carry it: on a route that receives
|
|
104
|
+
* credentials, passwords, OTP codes, tokens and secrets are replaced by a marker, both as GraphQL variables and as
|
|
105
|
+
* literals inlined in the query. Other routes (content, system, transfer) carry project data, which the credential
|
|
106
|
+
* names would only over-mask (`code`, `token` are common column names), so their bodies are logged as sent.
|
|
107
|
+
*/
|
|
108
|
+
export const maskRequestBody = (body: unknown, carriesCredentials: boolean): unknown => carriesCredentials ? maskGraphQLRequest(body) : body
|
|
109
|
+
|
|
110
|
+
/** The request URI as it may be logged: on a route that receives credentials, the query string is masked. */
|
|
111
|
+
export const maskRequestUri = (uri: string | undefined, carriesCredentials: boolean): string | undefined => {
|
|
112
|
+
if (uri === undefined || !carriesCredentials) {
|
|
113
|
+
return uri
|
|
114
|
+
}
|
|
115
|
+
const queryStart = uri.indexOf('?')
|
|
116
|
+
return queryStart === -1 ? uri : `${uri.slice(0, queryStart)}?${REDACTED}`
|
|
117
|
+
}
|
|
@@ -3,6 +3,7 @@ import { MailerOptions } from '@contember/engine-tenant-api'
|
|
|
3
3
|
import { validateRequestMemoryBudgetOptions } from '@contember/database'
|
|
4
4
|
import { upperCaseFirst } from '../utils/strings.js'
|
|
5
5
|
import ipaddr from 'ipaddr.js'
|
|
6
|
+
import { EgressError, EgressPolicy } from '@contember/engine-common'
|
|
6
7
|
|
|
7
8
|
const dbConfigRequired = {
|
|
8
9
|
host: Typesafe.string,
|
|
@@ -150,6 +151,33 @@ export const telemetryConfigSchema = Typesafe.partial({
|
|
|
150
151
|
}),
|
|
151
152
|
})
|
|
152
153
|
|
|
154
|
+
const egressConfigSchema = Typesafe.transform(
|
|
155
|
+
Typesafe.partial({
|
|
156
|
+
guard: Typesafe.boolean,
|
|
157
|
+
allowHttp: Typesafe.boolean,
|
|
158
|
+
allowlist: (val: unknown, path: PropertyKey[] = []): string[] | undefined => {
|
|
159
|
+
if (val === undefined || val === null || val === '') {
|
|
160
|
+
return undefined
|
|
161
|
+
}
|
|
162
|
+
const raw = Array.isArray(val) ? val : String(val).split(',')
|
|
163
|
+
const entries = raw.map(it => String(it).trim()).filter(Boolean)
|
|
164
|
+
return entries.length > 0 ? entries : undefined
|
|
165
|
+
},
|
|
166
|
+
}),
|
|
167
|
+
(value, input, path) => {
|
|
168
|
+
try {
|
|
169
|
+
// Rejects a malformed allowlist at startup rather than at the first outgoing request.
|
|
170
|
+
new EgressPolicy({ allowHttp: value.allowHttp ?? false, allowlist: value.allowlist ?? [] })
|
|
171
|
+
} catch (e) {
|
|
172
|
+
if (e instanceof EgressError) {
|
|
173
|
+
return Typesafe.fail(path, e.message)
|
|
174
|
+
}
|
|
175
|
+
throw e
|
|
176
|
+
}
|
|
177
|
+
return value
|
|
178
|
+
},
|
|
179
|
+
)
|
|
180
|
+
|
|
153
181
|
export const serverConfigSchema = Typesafe.partial({
|
|
154
182
|
port: Typesafe.number,
|
|
155
183
|
http: Typesafe.partial({
|
|
@@ -281,6 +309,8 @@ export const serverConfigSchema = Typesafe.partial({
|
|
|
281
309
|
}),
|
|
282
310
|
),
|
|
283
311
|
telemetry: telemetryConfigSchema,
|
|
312
|
+
// Restrictions on outgoing requests to tenant-chosen URLs (Actions webhooks, OIDC identity providers). Opt-in.
|
|
313
|
+
egress: egressConfigSchema,
|
|
284
314
|
projectGroup: (val: unknown, path: PropertyKey[] = []) =>
|
|
285
315
|
Typesafe.valueAt(val, ['domainMapping']) === undefined
|
|
286
316
|
? undefined
|
|
@@ -148,6 +148,11 @@ export const configTemplate: any = {
|
|
|
148
148
|
},
|
|
149
149
|
},
|
|
150
150
|
},
|
|
151
|
+
egress: {
|
|
152
|
+
guard: '%?env.CONTEMBER_EGRESS_GUARD::bool%',
|
|
153
|
+
allowHttp: '%?env.CONTEMBER_EGRESS_ALLOW_HTTP::bool%',
|
|
154
|
+
allowlist: '%?env.CONTEMBER_EGRESS_ALLOWLIST::string%',
|
|
155
|
+
},
|
|
151
156
|
test: {
|
|
152
157
|
transactions: '%?env.CONTEMBER_TEST_TRANSACTIONS::bool%',
|
|
153
158
|
transactionTtlSeconds: '%?env.CONTEMBER_TEST_TRANSACTION_TTL_SECONDS::number%',
|
|
@@ -83,23 +83,31 @@ export class ProjectMembershipResolver {
|
|
|
83
83
|
}
|
|
84
84
|
try {
|
|
85
85
|
return assumeMembershipValueType(value).memberships
|
|
86
|
-
} catch (e
|
|
87
|
-
throw new HttpErrorResponse(400, `Invalid format of "assume membership": ${e.message}`)
|
|
86
|
+
} catch (e) {
|
|
87
|
+
throw new HttpErrorResponse(400, `Invalid format of "assume membership": ${e instanceof Error ? e.message : String(e)}`)
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
private readAssumeMembershipJson(req: HttpRequest): unknown {
|
|
92
92
|
if (typeof req.body === 'object' && req.body !== null && 'assumeMembership' in req.body) {
|
|
93
|
-
return
|
|
93
|
+
return req.body.assumeMembership
|
|
94
94
|
}
|
|
95
95
|
const value = req.get(assumeMembershipHeader).trim()
|
|
96
|
-
|
|
96
|
+
if (value === '') {
|
|
97
|
+
return null
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
return JSON.parse(value)
|
|
101
|
+
} catch {
|
|
102
|
+
throw new HttpErrorResponse(400, `Invalid JSON in ${assumeMembershipHeader} header`)
|
|
103
|
+
}
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
private verifyAssumedRoles(explicitMemberships: readonly Acl.Membership[], acl: Acl.Schema, assumedMemberships: readonly Acl.Membership[]) {
|
|
107
|
+
// A stored membership whose role the ACL no longer defines grants nothing, so it may assume nothing either.
|
|
100
108
|
const membershipMatcher = new MembershipMatcher(explicitMemberships.map(it => ({
|
|
101
109
|
...it,
|
|
102
|
-
matchRule: acl.roles[it.role].content?.assumeMembership ?? {},
|
|
110
|
+
matchRule: Object.hasOwn(acl.roles, it.role) ? acl.roles[it.role].content?.assumeMembership ?? {} : {},
|
|
103
111
|
})))
|
|
104
112
|
|
|
105
113
|
for (const assumed of assumedMemberships) {
|
|
@@ -134,6 +134,7 @@ export class ProjectGroupContainerFactory {
|
|
|
134
134
|
encrypt: cryptoWrapper.encrypt.bind(cryptoWrapper),
|
|
135
135
|
encryptionEnabled: cryptoWrapper.enabled,
|
|
136
136
|
},
|
|
137
|
+
dataSigner: cryptoWrapper.enabled ? (purpose, value) => cryptoWrapper.sign(purpose, value) : null,
|
|
137
138
|
})
|
|
138
139
|
},
|
|
139
140
|
)
|
|
@@ -159,6 +159,7 @@ export class ImportExecutor {
|
|
|
159
159
|
await this.requireImportAccess(groupContainer, contentSchema.schema, authResult, options.project, 'content')
|
|
160
160
|
const contentDatabaseClient = projectContainer.connection.createClient(stage.schema, {})
|
|
161
161
|
const mapping = this.contentSchemaTransferMappingFactory.createContentSchemaMapping(contentSchema.schema)
|
|
162
|
+
this.requireMappedTables(mapping, options.tables, `content schema of project ${options.project}`)
|
|
162
163
|
|
|
163
164
|
return await contentDatabaseClient.transaction(async db => {
|
|
164
165
|
await this.disableTimeouts(db)
|
|
@@ -198,6 +199,7 @@ export class ImportExecutor {
|
|
|
198
199
|
await this.requireImportAccess(groupContainer, schema, authResult, options.project, 'system')
|
|
199
200
|
const systemDatabaseContext = projectContainer.systemDatabaseContext
|
|
200
201
|
const mapping = this.systemSchemaTransferMappingFactory.build()
|
|
202
|
+
this.requireMappedTables(mapping, options.tables, `system schema of project ${options.project}`)
|
|
201
203
|
|
|
202
204
|
return await systemDatabaseContext.client.transaction(async db => {
|
|
203
205
|
await this.disableTimeouts(db)
|
|
@@ -219,6 +221,9 @@ export class ImportExecutor {
|
|
|
219
221
|
it: CommandIterator<'importSequence'>,
|
|
220
222
|
): Promise<CommandIterator | null> {
|
|
221
223
|
const [options] = it.commandArgs
|
|
224
|
+
if (!Object.hasOwn(mapping.tables, options.table) || !Object.hasOwn(mapping.tables[options.table].columns, options.column)) {
|
|
225
|
+
throw new ImportError(`Unknown column ${options.table}.${options.column}`)
|
|
226
|
+
}
|
|
222
227
|
|
|
223
228
|
await db.query(
|
|
224
229
|
'SELECT setval(pg_get_serial_sequence(?, ?), ?)',
|
|
@@ -314,6 +319,15 @@ export class ImportExecutor {
|
|
|
314
319
|
}
|
|
315
320
|
}
|
|
316
321
|
|
|
322
|
+
/** The tables an import truncates must come from the same mapping an export of this schema lists. */
|
|
323
|
+
private requireMappedTables(mapping: TransferMapping, tableNames: readonly string[], target: string) {
|
|
324
|
+
for (const tableName of tableNames) {
|
|
325
|
+
if (!Object.hasOwn(mapping.tables, tableName)) {
|
|
326
|
+
throw new ImportError(`Table ${tableName} is not part of the ${target}`)
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
317
331
|
private async fetchStageBySlug(systemDatabaseContext: DatabaseContext, projectSlug: string, stageSlug: string) {
|
|
318
332
|
const stage = await systemDatabaseContext.queryHandler.fetch(new StageBySlugQuery(stageSlug))
|
|
319
333
|
|
|
@@ -374,7 +388,7 @@ export class ImportExecutor {
|
|
|
374
388
|
tableName: string,
|
|
375
389
|
columnNames: readonly string[],
|
|
376
390
|
): Promise<InsertContext> {
|
|
377
|
-
const tableMapping = mapping.tables[tableName]
|
|
391
|
+
const tableMapping = Object.hasOwn(mapping.tables, tableName) ? mapping.tables[tableName] : undefined
|
|
378
392
|
const columns = []
|
|
379
393
|
|
|
380
394
|
if (tableMapping === undefined) {
|
|
@@ -382,7 +396,7 @@ export class ImportExecutor {
|
|
|
382
396
|
}
|
|
383
397
|
|
|
384
398
|
for (const columnName of columnNames) {
|
|
385
|
-
const column = tableMapping.columns[columnName]
|
|
399
|
+
const column = Object.hasOwn(tableMapping.columns, columnName) ? tableMapping.columns[columnName] : undefined
|
|
386
400
|
|
|
387
401
|
if (column === undefined) {
|
|
388
402
|
throw new ImportError(`Unknown column ${tableName}.${columnName}`)
|
|
@@ -27,6 +27,18 @@ export class CryptoWrapper {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* HMAC-SHA256 under a key derived (HKDF) from the encryption key for `purpose`, so signatures never use the
|
|
32
|
+
* encryption key itself and a signature made for one purpose does not verify for another.
|
|
33
|
+
*/
|
|
34
|
+
sign(purpose: string, value: Buffer): Buffer {
|
|
35
|
+
if (!this.encryptionKey) {
|
|
36
|
+
throw new Error('encryption key not provided')
|
|
37
|
+
}
|
|
38
|
+
const signingKey = Buffer.from(crypto.hkdfSync('sha256', this.encryptionKey, Buffer.alloc(0), `contember-signing:${purpose}`, 32))
|
|
39
|
+
return crypto.createHmac('sha256', signingKey).update(value).digest()
|
|
40
|
+
}
|
|
41
|
+
|
|
30
42
|
async decrypt(value: Buffer, version: number): Promise<{ value: Buffer; needsReEncrypt: boolean }> {
|
|
31
43
|
if (!this.encryptionKey) {
|
|
32
44
|
throw new Error('encryption key not provided')
|
|
@@ -74,3 +74,19 @@ test('encryption v2: corrupted auth key', async () => {
|
|
|
74
74
|
}
|
|
75
75
|
assert.strictEqual('Unsupported state or unable to authenticate data', err.message)
|
|
76
76
|
})
|
|
77
|
+
|
|
78
|
+
test('signing is deterministic and bound to its purpose and key', () => {
|
|
79
|
+
const cryptoWrapper = new CryptoWrapper(encryptionKey)
|
|
80
|
+
const value = Buffer.from('payload')
|
|
81
|
+
const signature = cryptoWrapper.sign('first', value)
|
|
82
|
+
expect(signature).toHaveLength(32)
|
|
83
|
+
expect(cryptoWrapper.sign('first', value).equals(signature)).toBe(true)
|
|
84
|
+
expect(cryptoWrapper.sign('second', value).equals(signature)).toBe(false)
|
|
85
|
+
expect(crypto.createHmac('sha256', encryptionKey).update(value).digest().equals(signature)).toBe(false)
|
|
86
|
+
const otherKey = crypto.createSecretKey(Buffer.alloc(32, 1))
|
|
87
|
+
expect(new CryptoWrapper(otherKey).sign('first', value).equals(signature)).toBe(false)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('signing without an encryption key fails', () => {
|
|
91
|
+
expect(() => new CryptoWrapper().sign('first', Buffer.from('payload'))).toThrow('encryption key not provided')
|
|
92
|
+
})
|
|
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'bun:test'
|
|
|
2
2
|
import { ProjectMembershipFetcher, ProjectMembershipResolver } from '../../../src/index.js'
|
|
3
3
|
import { createMock } from '../../utils.js'
|
|
4
4
|
import { Acl } from '@contember/schema'
|
|
5
|
+
import { HttpErrorResponse } from '../../../src/common/HttpResponse.js'
|
|
5
6
|
|
|
6
7
|
describe('membership resolver', () => {
|
|
7
8
|
it('should return implicit role', async () => {
|
|
@@ -215,4 +216,53 @@ describe('membership resolver', () => {
|
|
|
215
216
|
variables: [{ name: 'lang', condition: { eq: 'cs' } }],
|
|
216
217
|
}])
|
|
217
218
|
})
|
|
219
|
+
|
|
220
|
+
const resolveWithAssumeHeader = async (header: string, storedMemberships: readonly Acl.Membership[]): Promise<unknown> => {
|
|
221
|
+
const membershipResolver = new ProjectMembershipResolver(
|
|
222
|
+
false,
|
|
223
|
+
createMock<ProjectMembershipFetcher>({
|
|
224
|
+
fetchMemberships: () => Promise.resolve(storedMemberships),
|
|
225
|
+
}),
|
|
226
|
+
)
|
|
227
|
+
try {
|
|
228
|
+
await membershipResolver.resolveMemberships({
|
|
229
|
+
request: { body: {}, get: name => name === 'x-contember-assume-membership' ? header : '' },
|
|
230
|
+
projectSlug: 'test',
|
|
231
|
+
identity: { identityId: 'd4141336-6512-41ef-a25a-374de35a2806', roles: [] },
|
|
232
|
+
acl: {
|
|
233
|
+
roles: {
|
|
234
|
+
editor: { variables: {}, entities: {}, content: { assumeMembership: { viewer: true } } },
|
|
235
|
+
viewer: { variables: {}, entities: {} },
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
})
|
|
239
|
+
return null
|
|
240
|
+
} catch (e) {
|
|
241
|
+
return e
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
it('answers 400 for an assume-membership header that is not JSON', async () => {
|
|
246
|
+
const error = await resolveWithAssumeHeader('{not json', [{ role: 'editor', variables: [] }])
|
|
247
|
+
expect(error).toBeInstanceOf(HttpErrorResponse)
|
|
248
|
+
expect(error).toMatchObject({ code: 400, message: 'Invalid JSON in x-contember-assume-membership header' })
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
it('answers 400 for an assume-membership header of a wrong shape', async () => {
|
|
252
|
+
const error = await resolveWithAssumeHeader('{"memberships": "viewer"}', [{ role: 'editor', variables: [] }])
|
|
253
|
+
expect(error).toBeInstanceOf(HttpErrorResponse)
|
|
254
|
+
expect(error).toMatchObject({ code: 400 })
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
it('a stored role missing from the ACL may not assume a membership', async () => {
|
|
258
|
+
const header = JSON.stringify({ memberships: [{ role: 'viewer', variables: [] }] })
|
|
259
|
+
const error = await resolveWithAssumeHeader(header, [{ role: 'removed', variables: [] }])
|
|
260
|
+
expect(error).toBeInstanceOf(HttpErrorResponse)
|
|
261
|
+
expect(error).toMatchObject({ code: 403 })
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
it('a stored role with an assume rule still assumes the allowed membership', async () => {
|
|
265
|
+
const header = JSON.stringify({ memberships: [{ role: 'viewer', variables: [] }] })
|
|
266
|
+
expect(await resolveWithAssumeHeader(header, [{ role: 'editor', variables: [] }])).toBeNull()
|
|
267
|
+
})
|
|
218
268
|
})
|