Skip to content

Commit

Permalink
Merge pull request #1574 from stripe/anniel-webhooks-error
Browse files Browse the repository at this point in the history
Split StripeSignatureVerificationError detail field into header and payload
  • Loading branch information
anniel-stripe authored Oct 11, 2022
2 parents deb7bb5 + f9bc027 commit 38b2909
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 50 deletions.
8 changes: 7 additions & 1 deletion lib/Error.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 4 additions & 24 deletions lib/Webhooks.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,16 @@ class StripeConnectionError extends StripeError {}
* SignatureVerificationError is raised when the signature verification for a
* webhook fails
*/
class StripeSignatureVerificationError extends StripeError {}
class StripeSignatureVerificationError extends StripeError {
header: string;
payload: string;

constructor(header: string, payload: string, raw: StripeRawError = {}) {
super(raw);
this.header = header;
this.payload = payload;
}
}

/**
* IdempotencyError is raised in cases where an idempotency key was used
Expand Down
28 changes: 4 additions & 24 deletions src/Webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,14 @@ function parseEventDetails(encodedPayload, encodedHeader, expectedScheme) {
const details = parseHeader(decodedHeader, expectedScheme);

if (!details || details.timestamp === -1) {
throw new StripeSignatureVerificationError({
throw new StripeSignatureVerificationError(decodedHeader, decodedPayload, {
message: 'Unable to extract timestamp and signatures from header',
// @ts-expect-error Type '{ decodedHeader: any; decodedPayload: any; }' is not assignable to type 'string'.
detail: {
decodedHeader,
decodedPayload,
},
});
}

if (!details.signatures.length) {
throw new StripeSignatureVerificationError({
throw new StripeSignatureVerificationError(decodedHeader, decodedPayload, {
message: 'No signatures found with expected scheme',
// @ts-expect-error Type '{ decodedHeader: any; decodedPayload: any; }' is not assignable to type 'string'.
detail: {
decodedHeader,
decodedPayload,
},
});
}

Expand All @@ -207,29 +197,19 @@ function validateComputedSignature(
).length;

if (!signatureFound) {
throw new StripeSignatureVerificationError({
throw new StripeSignatureVerificationError(header, payload, {
message:
'No signatures found matching the expected signature for payload.' +
' Are you passing the raw request body you received from Stripe?' +
' https://github.com/stripe/stripe-node#webhook-signing',
// @ts-expect-error Type '{ header: any; payload: any; }' is not assignable to type 'string'.
detail: {
header,
payload,
},
});
}

const timestampAge = Math.floor(Date.now() / 1000) - details.timestamp;

if (tolerance > 0 && timestampAge > tolerance) {
throw new StripeSignatureVerificationError({
throw new StripeSignatureVerificationError(header, payload, {
message: 'Timestamp outside the tolerance zone',
// @ts-expect-error Type '{ header: any; payload: any; }' is not assignable to type 'string'.
detail: {
header,
payload,
},
});
}

Expand Down

0 comments on commit 38b2909

Please sign in to comment.