|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { |
| 5 | + API_TRIGGER_SIGNATURE_KEY, |
| 6 | + ApiTriggerSignatureSchema, |
| 7 | + apiTriggerSignatureRefusals, |
| 8 | + apiTriggerVerifierOf, |
| 9 | +} from './api-trigger-signature.zod'; |
| 10 | +import { FlowSchema } from './flow.zod'; |
| 11 | + |
| 12 | +// `config.signature` — an `api` flow's start node declares how a third-party |
| 13 | +// sender signs its posts, keyed by `config.secretRef`. These pins hold the |
| 14 | +// contract half: the three senders the card names parse whole, the one reader |
| 15 | +// answers exactly one verifier per hook, and every refusal lands on the start |
| 16 | +// node's `config.signature`. What the verifier must do with a post (the |
| 17 | +// constant-time compare, the replay window, the one 401) is the implementer's |
| 18 | +// obligation in `contracts/trigger-api-service.ts`, pinned by the server slice. |
| 19 | + |
| 20 | +const DOCUSIGN = { |
| 21 | + signs: 'body', |
| 22 | + header: 'X-DocuSign-Signature-1', |
| 23 | + algorithm: 'hmac-sha256', |
| 24 | + encoding: 'base64', |
| 25 | +} as const; |
| 26 | + |
| 27 | +const STRIPE_STYLE = { |
| 28 | + signs: 'timestamp.body', |
| 29 | + header: 'Stripe-Signature', |
| 30 | + algorithm: 'hmac-sha256', |
| 31 | + encoding: 'hex', |
| 32 | + timestampElement: 't', |
| 33 | + digestElement: 'v1', |
| 34 | +} as const; |
| 35 | + |
| 36 | +const GITHUB_STYLE = { |
| 37 | + signs: 'body', |
| 38 | + header: 'X-Hub-Signature-256', |
| 39 | + algorithm: 'hmac-sha256', |
| 40 | + encoding: 'hex', |
| 41 | + prefix: 'sha256=', |
| 42 | +} as const; |
| 43 | + |
| 44 | +function flow(type: string, config: Record<string, unknown>) { |
| 45 | + return { |
| 46 | + name: 'clm_envelope_status', |
| 47 | + label: 'Envelope status', |
| 48 | + type, |
| 49 | + nodes: [ |
| 50 | + { id: 'start', type: 'start', label: 'Start', config }, |
| 51 | + { id: 'end', type: 'end', label: 'End' }, |
| 52 | + ], |
| 53 | + edges: [{ id: 'e1', source: 'start', target: 'end' }], |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +const KEYED = { hookId: 'docusign', secretRef: 'DOCUSIGN_CONNECT_KEY' }; |
| 58 | + |
| 59 | +/** The refusals the flow parse raises at `nodes.0.config.signature` and below, by path. */ |
| 60 | +function refusalsUnderSignature(result: ReturnType<typeof FlowSchema.safeParse>) { |
| 61 | + expect(result.success).toBe(false); |
| 62 | + const issues = result.error!.issues.filter((i) => i.path.join('.').startsWith('nodes.0.config.signature')); |
| 63 | + expect(issues.length).toBeGreaterThan(0); |
| 64 | + for (const issue of issues) expect(issue.code).toBe('custom'); |
| 65 | + return issues; |
| 66 | +} |
| 67 | + |
| 68 | +const pathsOf = (issues: ReadonlyArray<{ path: PropertyKey[] }>) => issues.map((i) => i.path.join('.')); |
| 69 | + |
| 70 | +describe('config.signature — the three senders the card names parse whole', () => { |
| 71 | + it.each([ |
| 72 | + ['DocuSign Connect (base64 HMAC-SHA256 of the raw body)', DOCUSIGN], |
| 73 | + ['Stripe-style t=…,v1=… (hex HMAC-SHA256 of <t>.<raw body>)', STRIPE_STYLE], |
| 74 | + ['GitHub-style sha256=<hex> (a prefix before the digest)', GITHUB_STYLE], |
| 75 | + ])('%s: a type api flow keyed by secretRef parses, the declaration served as written', (_, signature) => { |
| 76 | + const result = FlowSchema.safeParse(flow('api', { ...KEYED, signature })); |
| 77 | + expect(result.success).toBe(true); |
| 78 | + expect(result.data!.nodes[0]!.config).toEqual({ ...KEYED, signature }); |
| 79 | + }); |
| 80 | + |
| 81 | + it("a start node triggerType: 'api' binds the hook too, so the declaration is read there", () => { |
| 82 | + expect( |
| 83 | + FlowSchema.safeParse(flow('autolaunched', { triggerType: 'api', secretRef: 'DOCUSIGN_CONNECT_KEY', signature: DOCUSIGN })) |
| 84 | + .success, |
| 85 | + ).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('the key is spelled signature', () => { |
| 89 | + expect(API_TRIGGER_SIGNATURE_KEY).toBe('signature'); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('one verifier per hook — apiTriggerVerifierOf decides on the key, never on whether it parses', () => { |
| 94 | + it('no signature: the hook verifies x-objectstack-signature exactly as before, whatever its secret source', () => { |
| 95 | + for (const config of [{ secretRef: 'SRM_HEARTBEAT_SECRET' }, { secret: 'literal' }, {}, undefined, 'not a config']) { |
| 96 | + expect(apiTriggerVerifierOf(config)).toEqual({ kind: 'objectstack-signature' }); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + it('a usable declaration REPLACES that check: the reader answers the declared scheme and the key it names', () => { |
| 101 | + expect(apiTriggerVerifierOf({ ...KEYED, signature: DOCUSIGN })).toEqual({ |
| 102 | + kind: 'declared', |
| 103 | + signature: DOCUSIGN, |
| 104 | + secretRef: 'DOCUSIGN_CONNECT_KEY', |
| 105 | + }); |
| 106 | + expect(apiTriggerVerifierOf({ ...KEYED, signature: STRIPE_STYLE })).toMatchObject({ |
| 107 | + kind: 'declared', |
| 108 | + signature: STRIPE_STYLE, |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('a declaration that is present and unusable is never read as absent, so it cannot fall back to the platform check', () => { |
| 113 | + for (const config of [ |
| 114 | + { ...KEYED, signature: { ...DOCUSIGN, encoding: 'base64url' } }, |
| 115 | + { ...KEYED, signature: 'X-DocuSign-Signature-1' }, |
| 116 | + { ...KEYED, signature: null }, |
| 117 | + { signature: DOCUSIGN }, |
| 118 | + { secret: 'connect-key', signature: DOCUSIGN }, |
| 119 | + { secret: 'connect-key', secretRef: 'DOCUSIGN_CONNECT_KEY', signature: DOCUSIGN }, |
| 120 | + { secretRef: 'OS_AUTH_SECRET', signature: DOCUSIGN }, |
| 121 | + { ...KEYED, signature: { ...STRIPE_STYLE, digestElement: 't' } }, |
| 122 | + ]) { |
| 123 | + expect(apiTriggerVerifierOf(config)).toEqual({ kind: 'unusable' }); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + it('is not optional and does not stack: the shape has no key that asks for a second check or a fallback', () => { |
| 128 | + for (const extra of [{ optional: true }, { fallback: 'x-objectstack-signature' }, { also: 'x-objectstack-signature' }]) { |
| 129 | + expect(ApiTriggerSignatureSchema.safeParse({ ...DOCUSIGN, ...extra }).success).toBe(false); |
| 130 | + expect(ApiTriggerSignatureSchema.safeParse({ ...STRIPE_STYLE, ...extra }).success).toBe(false); |
| 131 | + } |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +describe('the replay bound follows what is signed', () => { |
| 136 | + it('a scheme that signs a timestamp must say where it comes from: timestamp.body without a timestamp source is refused', () => { |
| 137 | + const { timestampElement: _t, ...noSource } = STRIPE_STYLE; |
| 138 | + const issues = refusalsUnderSignature(FlowSchema.safeParse(flow('api', { ...KEYED, signature: noSource }))); |
| 139 | + expect(pathsOf(issues)).toEqual(['nodes.0.config.signature.timestampElement']); |
| 140 | + }); |
| 141 | + |
| 142 | + it('a body-only scheme signs no time, so it cannot name a timestamp source (no replay bound is invented for it)', () => { |
| 143 | + for (const extra of [{ timestampElement: 't' }, { timestampElement: 't', digestElement: 'v1' }]) { |
| 144 | + const issues = refusalsUnderSignature(FlowSchema.safeParse(flow('api', { ...KEYED, signature: { ...DOCUSIGN, ...extra } }))); |
| 145 | + expect(issues).toHaveLength(1); |
| 146 | + expect(issues[0]!.message).toMatch(/signs no time/); |
| 147 | + } |
| 148 | + }); |
| 149 | + |
| 150 | + it('a timestamp.body scheme names its digest element too, distinct from the timestamp, and takes no prefix', () => { |
| 151 | + const { digestElement: _d, ...noDigest } = STRIPE_STYLE; |
| 152 | + expect(pathsOf(refusalsUnderSignature(FlowSchema.safeParse(flow('api', { ...KEYED, signature: noDigest }))))).toEqual([ |
| 153 | + 'nodes.0.config.signature.digestElement', |
| 154 | + ]); |
| 155 | + expect( |
| 156 | + pathsOf(refusalsUnderSignature(FlowSchema.safeParse(flow('api', { ...KEYED, signature: { ...STRIPE_STYLE, digestElement: 't' } })))), |
| 157 | + ).toEqual(['nodes.0.config.signature.digestElement']); |
| 158 | + expect( |
| 159 | + refusalsUnderSignature(FlowSchema.safeParse(flow('api', { ...KEYED, signature: { ...STRIPE_STYLE, prefix: 'v1=' } }))), |
| 160 | + ).toHaveLength(1); |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
| 164 | +describe('refused at parse, at the start node config.signature', () => { |
| 165 | + it('a verifier with no key reference', () => { |
| 166 | + const issues = refusalsUnderSignature(FlowSchema.safeParse(flow('api', { hookId: 'docusign', signature: DOCUSIGN }))); |
| 167 | + expect(pathsOf(issues)).toEqual(['nodes.0.config.signature']); |
| 168 | + expect(issues[0]!.message).toMatch(/names no key/); |
| 169 | + }); |
| 170 | + |
| 171 | + it('a verifier beside a literal secret, the cleared form included: a vendor key is never inline', () => { |
| 172 | + for (const secret of ['connect-key', '']) { |
| 173 | + const issues = refusalsUnderSignature(FlowSchema.safeParse(flow('api', { secret, signature: DOCUSIGN }))); |
| 174 | + expect(pathsOf(issues)).toEqual(['nodes.0.config.signature']); |
| 175 | + expect(issues[0]!.message).toMatch(/never an inline one/); |
| 176 | + } |
| 177 | + }); |
| 178 | + |
| 179 | + it('an unknown algorithm: HMAC-SHA256 is the one closed value', () => { |
| 180 | + for (const algorithm of ['hmac-sha1', 'HMAC-SHA256', 'sha256', 'hmac-sha512', '', 256, undefined]) { |
| 181 | + const issues = refusalsUnderSignature(FlowSchema.safeParse(flow('api', { ...KEYED, signature: { ...DOCUSIGN, algorithm } }))); |
| 182 | + expect(pathsOf(issues)).toEqual(['nodes.0.config.signature.algorithm']); |
| 183 | + } |
| 184 | + }); |
| 185 | + |
| 186 | + it('an unknown encoding: hex or base64 only', () => { |
| 187 | + for (const encoding of ['base64url', 'HEX', 'binary', 'utf8', '', undefined]) { |
| 188 | + const issues = refusalsUnderSignature(FlowSchema.safeParse(flow('api', { ...KEYED, signature: { ...DOCUSIGN, encoding } }))); |
| 189 | + expect(pathsOf(issues)).toEqual(['nodes.0.config.signature.encoding']); |
| 190 | + } |
| 191 | + }); |
| 192 | + |
| 193 | + it('a signs that is neither form, or none', () => { |
| 194 | + for (const signs of ['timestamp+body', 'raw', 'headers', undefined]) { |
| 195 | + const issues = refusalsUnderSignature(FlowSchema.safeParse(flow('api', { ...KEYED, signature: { ...DOCUSIGN, signs } }))); |
| 196 | + expect(issues).toHaveLength(1); |
| 197 | + } |
| 198 | + }); |
| 199 | + |
| 200 | + it('a header that is not an HTTP header name, or none', () => { |
| 201 | + for (const header of ['', 'X DocuSign Signature', 'X-Signature:', 'X-Sigé', 42, undefined]) { |
| 202 | + const issues = refusalsUnderSignature(FlowSchema.safeParse(flow('api', { ...KEYED, signature: { ...DOCUSIGN, header } }))); |
| 203 | + expect(pathsOf(issues)).toEqual(['nodes.0.config.signature.header']); |
| 204 | + } |
| 205 | + }); |
| 206 | + |
| 207 | + it('a prefix with a space or a comma, or a blank one', () => { |
| 208 | + for (const prefix of ['', 'sha256 =', 'a,b']) { |
| 209 | + const issues = refusalsUnderSignature(FlowSchema.safeParse(withPrefix(prefix))); |
| 210 | + expect(pathsOf(issues)).toEqual(['nodes.0.config.signature.prefix']); |
| 211 | + } |
| 212 | + }); |
| 213 | + |
| 214 | + it('a value that is not a declaration object', () => { |
| 215 | + for (const signature of ['X-DocuSign-Signature-1', ['body'], null, 7]) { |
| 216 | + refusalsUnderSignature(FlowSchema.safeParse(flow('api', { ...KEYED, signature }))); |
| 217 | + } |
| 218 | + }); |
| 219 | + |
| 220 | + it('a verifier on a flow that binds no api trigger: nothing would read it', () => { |
| 221 | + const issues = refusalsUnderSignature(FlowSchema.safeParse(flow('autolaunched', { ...KEYED, signature: DOCUSIGN }))); |
| 222 | + expect(pathsOf(issues)).toEqual(['nodes.0.config.signature']); |
| 223 | + expect(issues[0]!.message).toMatch(/binds no inbound `api` trigger/); |
| 224 | + }); |
| 225 | + |
| 226 | + it('a start node without signature raises nothing here', () => { |
| 227 | + expect(apiTriggerSignatureRefusals(flow('api', { secretRef: 'SRM_HEARTBEAT_SECRET' }))).toEqual([]); |
| 228 | + expect(apiTriggerSignatureRefusals(flow('api', { secret: 'x' }))).toEqual([]); |
| 229 | + expect(apiTriggerSignatureRefusals('not a flow')).toEqual([]); |
| 230 | + }); |
| 231 | +}); |
| 232 | + |
| 233 | +/** A type api flow keyed by secretRef whose GitHub-style declaration carries `prefix`. */ |
| 234 | +function withPrefix(prefix: unknown) { |
| 235 | + return flow('api', { ...KEYED, signature: { ...GITHUB_STYLE, prefix } }); |
| 236 | +} |
0 commit comments