@contember/engine-actions 2.2.0-alpha.16 → 2.2.0-alpha.17
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/CLAUDE.md +9 -1
- package/dist/development/src/dispatch/WebhookTargetHandler.cjs +14 -7
- package/dist/development/src/dispatch/WebhookTargetHandler.cjs.map +1 -1
- package/dist/development/src/dispatch/WebhookTargetHandler.js +14 -7
- package/dist/development/src/dispatch/WebhookTargetHandler.js.map +1 -1
- package/dist/development/src/dispatch/redactRequestError.cjs +24 -0
- package/dist/development/src/dispatch/redactRequestError.cjs.map +1 -0
- package/dist/development/src/dispatch/redactRequestError.js +24 -0
- package/dist/development/src/dispatch/redactRequestError.js.map +1 -0
- package/dist/production/src/dispatch/WebhookTargetHandler.cjs +14 -7
- package/dist/production/src/dispatch/WebhookTargetHandler.cjs.map +1 -1
- package/dist/production/src/dispatch/WebhookTargetHandler.js +14 -7
- package/dist/production/src/dispatch/WebhookTargetHandler.js.map +1 -1
- package/dist/production/src/dispatch/redactRequestError.cjs +24 -0
- package/dist/production/src/dispatch/redactRequestError.cjs.map +1 -0
- package/dist/production/src/dispatch/redactRequestError.js +24 -0
- package/dist/production/src/dispatch/redactRequestError.js.map +1 -0
- package/dist/tests/cases/unit/redactRequestError.test.d.ts +2 -0
- package/dist/tests/cases/unit/redactRequestError.test.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/dispatch/WebhookTargetHandler.d.ts.map +1 -1
- package/dist/types/dispatch/redactRequestError.d.ts +7 -0
- package/dist/types/dispatch/redactRequestError.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -17
- package/src/dispatch/WebhookTargetHandler.ts +18 -7
- package/src/dispatch/redactRequestError.ts +32 -0
- package/tests/cases/unit/redactRequestError.test.ts +45 -0
- package/tests/cases/unit/webhookHandler.test.ts +47 -0
package/package.json
CHANGED
|
@@ -22,27 +22,27 @@
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@contember/authorization": "2.2.0-alpha.
|
|
26
|
-
"@contember/database": "2.2.0-alpha.
|
|
27
|
-
"@contember/database-migrations": "2.2.0-alpha.
|
|
28
|
-
"@contember/engine-common": "2.2.0-alpha.
|
|
29
|
-
"@contember/engine-content-api": "2.2.0-alpha.
|
|
30
|
-
"@contember/engine-http": "2.2.0-alpha.
|
|
31
|
-
"@contember/engine-plugins": "2.2.0-alpha.
|
|
32
|
-
"@contember/engine-system-api": "2.2.0-alpha.
|
|
33
|
-
"@contember/engine-tenant-api": "2.2.0-alpha.
|
|
34
|
-
"@contember/graphql-utils": "2.2.0-alpha.
|
|
35
|
-
"@contember/logger": "2.2.0-alpha.
|
|
36
|
-
"@contember/schema": "2.2.0-alpha.
|
|
37
|
-
"@contember/schema-utils": "2.2.0-alpha.
|
|
38
|
-
"@contember/telemetry": "2.2.0-alpha.
|
|
39
|
-
"@contember/typesafe": "2.2.0-alpha.
|
|
25
|
+
"@contember/authorization": "2.2.0-alpha.17",
|
|
26
|
+
"@contember/database": "2.2.0-alpha.17",
|
|
27
|
+
"@contember/database-migrations": "2.2.0-alpha.17",
|
|
28
|
+
"@contember/engine-common": "2.2.0-alpha.17",
|
|
29
|
+
"@contember/engine-content-api": "2.2.0-alpha.17",
|
|
30
|
+
"@contember/engine-http": "2.2.0-alpha.17",
|
|
31
|
+
"@contember/engine-plugins": "2.2.0-alpha.17",
|
|
32
|
+
"@contember/engine-system-api": "2.2.0-alpha.17",
|
|
33
|
+
"@contember/engine-tenant-api": "2.2.0-alpha.17",
|
|
34
|
+
"@contember/graphql-utils": "2.2.0-alpha.17",
|
|
35
|
+
"@contember/logger": "2.2.0-alpha.17",
|
|
36
|
+
"@contember/schema": "2.2.0-alpha.17",
|
|
37
|
+
"@contember/schema-utils": "2.2.0-alpha.17",
|
|
38
|
+
"@contember/telemetry": "2.2.0-alpha.17",
|
|
39
|
+
"@contember/typesafe": "2.2.0-alpha.17",
|
|
40
40
|
"@graphql-tools/schema": "^10.0.31",
|
|
41
41
|
"graphql-tag": "^2.12.6",
|
|
42
42
|
"prom-client": "^12.0.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@contember/database-tester": "2.2.0-alpha.
|
|
45
|
+
"@contember/database-tester": "2.2.0-alpha.17",
|
|
46
46
|
"@types/bun": "^1.3.10",
|
|
47
47
|
"@types/ws": "^8.18.1",
|
|
48
48
|
"graphql": "^16.13.1"
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"pg": "^8.20.0"
|
|
53
53
|
},
|
|
54
54
|
"type": "module",
|
|
55
|
-
"version": "2.2.0-alpha.
|
|
55
|
+
"version": "2.2.0-alpha.17",
|
|
56
56
|
"repository": {
|
|
57
57
|
"type": "git",
|
|
58
58
|
"url": "git+https://github.com/contember/contember.git",
|
|
@@ -6,6 +6,7 @@ import { FetcherResponse, WebhookFetcher } from './WebhookFetcher.js'
|
|
|
6
6
|
import { Logger } from '@contember/logger'
|
|
7
7
|
import { formatTraceparent, INVALID_TRACE_ID, Tracer } from '@contember/telemetry'
|
|
8
8
|
import { replaceActionsVariables } from '@contember/schema-utils'
|
|
9
|
+
import { redactRequestError } from './redactRequestError.js'
|
|
9
10
|
|
|
10
11
|
const DEFAULT_TIMEOUT_MS = 30_000 // 30 seconds
|
|
11
12
|
|
|
@@ -24,11 +25,12 @@ const ResponseType = Typesafe.noExtraProps(Typesafe.object({
|
|
|
24
25
|
|
|
25
26
|
type EventResponseFactory = (eventRow: EventRow) => { ok: boolean; code?: number; response?: string; errorMessage?: string; durationMs?: number }
|
|
26
27
|
|
|
28
|
+
/** Carries the name and code of a failed request, and a reason only when it was redacted. Never the cause, which may quote credentials. */
|
|
27
29
|
class WebhookRequestError extends Error {
|
|
28
30
|
public readonly code?: string | number
|
|
29
31
|
|
|
30
|
-
constructor(error: unknown) {
|
|
31
|
-
super('Webhook request failed')
|
|
32
|
+
constructor(error: unknown, redactedReason?: string) {
|
|
33
|
+
super(redactedReason === undefined ? 'Webhook request failed' : `Webhook request failed: ${redactedReason}`)
|
|
32
34
|
try {
|
|
33
35
|
if (error instanceof Error) {
|
|
34
36
|
this.name = error.name
|
|
@@ -63,6 +65,8 @@ export class UndefinedVariableError extends Error {
|
|
|
63
65
|
type ResolvedRequest = {
|
|
64
66
|
url: string
|
|
65
67
|
headers: Record<string, string>
|
|
68
|
+
/** The resolved URL, header values and interpolated variable values, which an error message must not quote. */
|
|
69
|
+
secrets: ReadonlySet<string>
|
|
66
70
|
}
|
|
67
71
|
|
|
68
72
|
export type WebhookTargetHandlerOptions = {
|
|
@@ -119,14 +123,15 @@ export class WebhookTargetHandler implements InvokeHandler<Actions.WebhookTarget
|
|
|
119
123
|
logger,
|
|
120
124
|
})
|
|
121
125
|
} catch (e) {
|
|
122
|
-
|
|
123
|
-
const errorMessages = this.extractErrorMessages(e)
|
|
126
|
+
// Request errors may quote the resolved URL or a header value, so only a redacted reason is logged and stored.
|
|
127
|
+
const errorMessages = this.extractErrorMessages(e).map(it => redactRequestError(it, request.secrets))
|
|
128
|
+
const errorMessage = errorMessages.length ? errorMessages.join('; ') : undefined
|
|
129
|
+
logger.warn(new WebhookRequestError(e, errorMessage), { target: target.name })
|
|
124
130
|
const result = {
|
|
125
131
|
ok: false,
|
|
126
|
-
errorMessage
|
|
132
|
+
errorMessage,
|
|
127
133
|
durationMs: getDuration(),
|
|
128
134
|
}
|
|
129
|
-
eventResponseFactory = () => ({ ok: false, errorMessage: errorMessages.length ? errorMessages.join('; ') : undefined })
|
|
130
135
|
|
|
131
136
|
return events.map(it => ({ target, row: it, result }))
|
|
132
137
|
}
|
|
@@ -320,6 +325,7 @@ export class WebhookTargetHandler implements InvokeHandler<Actions.WebhookTarget
|
|
|
320
325
|
/** Throws {@link UndefinedVariableError} naming every variable without a value, an empty one included. */
|
|
321
326
|
private resolveVariables(target: Actions.WebhookTarget, variables: VariablesMap): ResolvedRequest {
|
|
322
327
|
const undefinedVariables = new Set<string>()
|
|
328
|
+
const secrets = new Set<string>()
|
|
323
329
|
const resolve = (subject: string) =>
|
|
324
330
|
replaceActionsVariables(subject, name => {
|
|
325
331
|
const value = resolveVariable(variables, name)
|
|
@@ -327,6 +333,7 @@ export class WebhookTargetHandler implements InvokeHandler<Actions.WebhookTarget
|
|
|
327
333
|
undefinedVariables.add(name)
|
|
328
334
|
return ''
|
|
329
335
|
}
|
|
336
|
+
secrets.add(value)
|
|
330
337
|
return value
|
|
331
338
|
})
|
|
332
339
|
const url = resolve(target.url)
|
|
@@ -334,7 +341,11 @@ export class WebhookTargetHandler implements InvokeHandler<Actions.WebhookTarget
|
|
|
334
341
|
if (undefinedVariables.size > 0) {
|
|
335
342
|
throw new UndefinedVariableError([...undefinedVariables])
|
|
336
343
|
}
|
|
337
|
-
|
|
344
|
+
secrets.add(url)
|
|
345
|
+
for (const value of Object.values(headers)) {
|
|
346
|
+
secrets.add(value)
|
|
347
|
+
}
|
|
348
|
+
return { url, headers, secrets }
|
|
338
349
|
}
|
|
339
350
|
}
|
|
340
351
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// scheme, optional userinfo up to the last @ of the authority, host and port, then path, query and fragment;
|
|
2
|
+
// a URL ends at whitespace or a quote, and starts only where a run of scheme characters starts, which keeps the scan linear
|
|
3
|
+
const urlPattern = /(?<![a-z\d+.-])([a-z][a-z\d+.-]*:\/\/)(?:([^\s/?#"'`<>]*)@)?([^\s/?#"'`<>]*)([^\s"'`<>]*)/gi
|
|
4
|
+
|
|
5
|
+
const redactedMarker = '[redacted]'
|
|
6
|
+
|
|
7
|
+
// fetch strips HTTP whitespace around a header value before quoting it in an error
|
|
8
|
+
const trimHttpWhitespace = (value: string): string => value.replace(/^[\t\n\r ]+/, '').replace(/[\t\n\r ]+$/, '')
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Removes what may carry a credential from a message about a failed webhook request: every occurrence of the given secrets,
|
|
12
|
+
* also with surrounding HTTP whitespace stripped, and then the userinfo, path, query and fragment of every URL it still quotes.
|
|
13
|
+
* The scheme, host and port of such a URL stay for diagnostics.
|
|
14
|
+
*/
|
|
15
|
+
export const redactRequestError = (message: string, secrets: Iterable<string>): string => {
|
|
16
|
+
const variants = new Set<string>()
|
|
17
|
+
for (const secret of secrets) {
|
|
18
|
+
variants.add(secret)
|
|
19
|
+
variants.add(trimHttpWhitespace(secret))
|
|
20
|
+
}
|
|
21
|
+
// the longest first, so a secret containing a shorter one is not left partly visible
|
|
22
|
+
const sortedSecrets = [...variants].filter(it => it !== '').sort((a, b) => b.length - a.length)
|
|
23
|
+
let redacted = message
|
|
24
|
+
for (const secret of sortedSecrets) {
|
|
25
|
+
redacted = redacted.replaceAll(secret, redactedMarker)
|
|
26
|
+
}
|
|
27
|
+
return redacted.replace(urlPattern, (_, scheme: string, userinfo: string | undefined, host: string, rest: string) => {
|
|
28
|
+
const redactedUserinfo = userinfo === undefined ? '' : `${redactedMarker}@`
|
|
29
|
+
const redactedRest = rest === '' || rest === '/' ? rest : `/${redactedMarker}`
|
|
30
|
+
return `${scheme}${redactedUserinfo}${host}${redactedRest}`
|
|
31
|
+
})
|
|
32
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import { redactRequestError } from '../../../src/dispatch/redactRequestError.js'
|
|
3
|
+
|
|
4
|
+
test('keeps a message without a URL or a secret', () => {
|
|
5
|
+
expect(redactRequestError('getaddrinfo ENOTFOUND example.com', [])).toBe('getaddrinfo ENOTFOUND example.com')
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
test('keeps only the scheme, host and port of a URL', () => {
|
|
9
|
+
expect(redactRequestError('Failed to parse URL from https://user:pw@example.com:8443/a/b?c=d#e', []))
|
|
10
|
+
.toBe('Failed to parse URL from https://[redacted]@example.com:8443/[redacted]')
|
|
11
|
+
expect(redactRequestError('Invalid URL http://example.com/?token=abc.', [])).toBe('Invalid URL http://example.com/[redacted]')
|
|
12
|
+
expect(redactRequestError('Unable to connect to http://example.com/ or http://example.org', []))
|
|
13
|
+
.toBe('Unable to connect to http://example.com/ or http://example.org')
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('redacts every URL, a quoted one included', () => {
|
|
17
|
+
expect(redactRequestError('Redirect from "https://a.example/x?t=1" to https://b.example/y', []))
|
|
18
|
+
.toBe('Redirect from "https://a.example/[redacted]" to https://b.example/[redacted]')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('redacts every occurrence of a secret, the longest first', () => {
|
|
22
|
+
expect(redactRequestError('expected abc, got abcdef and abc', ['abc', 'abcdef', ''])).toBe('expected [redacted], got [redacted] and [redacted]')
|
|
23
|
+
expect(redactRequestError('invalid header value "Bearer abc"', ['Bearer abc', 'abc'])).toBe('invalid header value "[redacted]"')
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('redacts a password containing @, / or ?', () => {
|
|
27
|
+
expect(redactRequestError('Request cannot be constructed from a URL that includes credentials: https://user:p@ss@example.com/hooks', []))
|
|
28
|
+
.toBe('Request cannot be constructed from a URL that includes credentials: https://[redacted]@example.com/[redacted]')
|
|
29
|
+
// such a password ends the authority early, so only the known value redacts all of it
|
|
30
|
+
expect(redactRequestError('Failed to parse URL from https://user:pa/s?s@example.com/hooks', ['pa/s?s']))
|
|
31
|
+
.toBe('Failed to parse URL from https://[redacted]@example.com/[redacted]')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('redacts a secret quoted without its surrounding whitespace', () => {
|
|
35
|
+
expect(redactRequestError('Headers.append: "Bearer abc\ndef" is an invalid header value.', [' Bearer abc\ndef\n', 'abc\ndef\n']))
|
|
36
|
+
.toBe('Headers.append: "[redacted]" is an invalid header value.')
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('scans a long message without a URL in linear time', () => {
|
|
40
|
+
const start = performance.now()
|
|
41
|
+
expect(redactRequestError('a-'.repeat(100_000), [])).toHaveLength(200_000)
|
|
42
|
+
expect(redactRequestError('a.'.repeat(100_000), [])).toHaveLength(200_000)
|
|
43
|
+
// the quadratic scan took seconds on a tenth of this
|
|
44
|
+
expect(performance.now() - start).toBeLessThan(1_000)
|
|
45
|
+
})
|
|
@@ -194,6 +194,53 @@ describe('webhook variables', () => {
|
|
|
194
194
|
// the dispatcher reports it once for the whole target, the handler stays quiet
|
|
195
195
|
assert.deepStrictEqual(testLoggerHandler.messages, [])
|
|
196
196
|
})
|
|
197
|
+
|
|
198
|
+
test('a failed request is logged and stored without the credentials its URL and headers carry', async () => {
|
|
199
|
+
const testLoggerHandler = new TestLoggerHandler()
|
|
200
|
+
const webhookHandler = createHandler({
|
|
201
|
+
async fetch(url: string, init: RequestInit): Promise<FetcherResponse> {
|
|
202
|
+
// messages as Node's fetch words them: the URL quoted raw, an invalid header value quoted trimmed
|
|
203
|
+
const authorization = new Headers(init.headers).get('Authorization')?.trim()
|
|
204
|
+
throw Object.assign(new TypeError(`Request cannot be constructed from a URL that includes credentials: ${url}`), {
|
|
205
|
+
cause: Object.assign(new TypeError(`Headers.append: "${authorization}" is an invalid header value.`), {
|
|
206
|
+
cause: Object.assign(new Error('connect ECONNREFUSED 127.0.0.1:443 after sending token-3 to https://example.com:8443/hooks?key=static-key'), {
|
|
207
|
+
code: 'ECONNREFUSED',
|
|
208
|
+
url,
|
|
209
|
+
}),
|
|
210
|
+
}),
|
|
211
|
+
})
|
|
212
|
+
},
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
const result = await webhookHandler.handle({
|
|
216
|
+
logger: createLogger(testLoggerHandler),
|
|
217
|
+
target: {
|
|
218
|
+
...target,
|
|
219
|
+
url: 'https://deliveries:{{PASSWORD}}@example.com:8443/hooks/{{PATH}}?key=static-key#fragment',
|
|
220
|
+
headers: { Authorization: 'Bearer {{TOKEN}}', 'X-Api-Key': 'static-header-key' },
|
|
221
|
+
},
|
|
222
|
+
events: [createTestEvent(0)],
|
|
223
|
+
// a password with @ and / ends the URL authority early, a value with an edge newline is quoted trimmed
|
|
224
|
+
variables: { PASSWORD: 'p@ss/1', PATH: 'path-2', TOKEN: 'token-3\n' },
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
const reason = 'Request cannot be constructed from a URL that includes credentials: [redacted]; '
|
|
228
|
+
+ 'Headers.append: "[redacted]" is an invalid header value.; '
|
|
229
|
+
+ 'connect ECONNREFUSED 127.0.0.1:443 after sending [redacted] to https://example.com:8443/[redacted]'
|
|
230
|
+
assert.deepStrictEqual(result.map(it => it.result.errorMessage), [reason])
|
|
231
|
+
assert.deepStrictEqual(testLoggerHandler.messages.map(it => [it.level.name, it.message, it.ownAttributes]), [
|
|
232
|
+
['warn', `Webhook request failed: ${reason}`, { target: 'test_target' }],
|
|
233
|
+
])
|
|
234
|
+
const error = testLoggerHandler.messages[0].error
|
|
235
|
+
expect(error).toBeInstanceOf(Error)
|
|
236
|
+
expect(error).toHaveProperty('name', 'TypeError')
|
|
237
|
+
expect(error).toHaveProperty('code', 'ECONNREFUSED')
|
|
238
|
+
expect(error).not.toHaveProperty('cause')
|
|
239
|
+
const logged = JSON.stringify([error, error instanceof Error ? error.stack : null])
|
|
240
|
+
for (const secret of ['deliveries', 'ss/1', 'hooks', 'path-2', 'static-key', 'fragment', 'token-3', 'static-header-key']) {
|
|
241
|
+
expect(logged).not.toContain(secret)
|
|
242
|
+
}
|
|
243
|
+
})
|
|
197
244
|
})
|
|
198
245
|
|
|
199
246
|
describe('webhook response', () => {
|