Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Return 401 on webhook validation errors #425

Merged
merged 2 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

- Return a 401 instead of 403 when webhooks fail validation [#425](https://github.com/Shopify/shopify-api-node/pull/425)
- Add optional new methods `deleteSession` and `findSessionsByShop` to `SessionStorage`, with the corresponding implementations for the various session storage adapters [#418](https://github.com/Shopify/shopify-api-node/pull/418)

## [4.1.0] - 2022-07-14
Expand Down
8 changes: 4 additions & 4 deletions src/webhooks/__tests__/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ describe('ShopifyWebhooks.Registry.process', () => {
.expect(StatusCode.Ok);
});

it('handles the request and returns Forbidden when topic is not registered', async () => {
it('handles the request and returns Not Found when topic is not registered', async () => {
ShopifyWebhooks.Registry.addHandler('NONSENSE_TOPIC', {
path: '/webhooks',
webhookHandler: genericWebhookHandler,
Expand All @@ -409,10 +409,10 @@ describe('ShopifyWebhooks.Registry.process', () => {
.post('/webhooks')
.set(headers({hmac: hmac(Context.API_SECRET_KEY, rawBody)}))
.send(rawBody)
.expect(StatusCode.Forbidden);
.expect(StatusCode.NotFound);
});

it('handles the request and returns Forbidden when hmac does not match', async () => {
it('handles the request and returns Unauthorized when hmac does not match', async () => {
ShopifyWebhooks.Registry.addHandler('PRODUCTS', {
path: '/webhooks',
webhookHandler: genericWebhookHandler,
Expand All @@ -434,7 +434,7 @@ describe('ShopifyWebhooks.Registry.process', () => {
.post('/webhooks')
.set(headers({hmac: hmac('incorrect secret', rawBody)}))
.send(rawBody)
.expect(StatusCode.Forbidden);
.expect(StatusCode.Unauthorized);
});

it('fails if the given body is empty', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/webhooks/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,13 @@ const WebhooksRegistry: RegistryInterface = {
responseError = error;
}
} else {
statusCode = StatusCode.Forbidden;
statusCode = StatusCode.NotFound;
responseError = new ShopifyErrors.InvalidWebhookError(
`No webhook is registered for topic ${topic}`,
);
}
} else {
statusCode = StatusCode.Forbidden;
statusCode = StatusCode.Unauthorized;
responseError = new ShopifyErrors.InvalidWebhookError(
`Could not validate request for topic ${topic}`,
);
Expand Down