Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate that webhook does not point back to app #475

Merged
merged 4 commits into from
Oct 15, 2020
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
13 changes: 11 additions & 2 deletions src/app/modules/webhook/webhook.utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { promises as dns } from 'dns'
import ip from 'ip'

import config from '../../../config/config'
import { isValidHttpsUrl } from '../../../shared/util/url-validation'

import { WebhookValidationError } from './webhook.errors'
Expand All @@ -18,9 +19,17 @@ export const validateWebhookUrl = (webhookUrl: string): Promise<any> => {
new WebhookValidationError(`${webhookUrl} is not a valid HTTPS URL.`),
)
}
const urlParsed = new URL(webhookUrl)
const webhookUrlParsed = new URL(webhookUrl)
const appUrlParsed = new URL(config.app.appUrl)
if (webhookUrlParsed.hostname === appUrlParsed.hostname) {
return reject(
new WebhookValidationError(
`You cannot send responses back to ${config.app.appUrl}.`,
),
)
}
dns
.resolve(urlParsed.hostname)
.resolve(webhookUrlParsed.hostname)
.then((addresses) => {
if (!addresses.length) {
return reject(
Expand Down
3 changes: 2 additions & 1 deletion tests/.test-basic-env
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ AWS_ACCESS_KEY_ID=fakeAccessKeyId
AWS_SECRET_ACCESS_KEY=fakeSecretAccessKey
SESSION_SECRET=sandcrawler-138577

AWS_ENDPOINT=http://localhost:4566
AWS_ENDPOINT=http://localhost:4566
APP_URL=https://example.com
4 changes: 3 additions & 1 deletion tests/.test-full-env
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ MOCK_WEBHOOK_PORT=4000
AWS_ACCESS_KEY_ID=fakeAccessKeyId
AWS_SECRET_ACCESS_KEY=fakeSecretAccessKey

AWS_ENDPOINT=http://localhost:4566
AWS_ENDPOINT=http://localhost:4566

APP_URL=https://example.com
10 changes: 10 additions & 0 deletions tests/unit/backend/modules/webhook/webhook.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ describe('Webhook URL validation', () => {
),
)
})

it('should reject URLs in the same domain as the app URL', async () => {
await expect(
validateWebhookUrl('https://example.com/test'),
).rejects.toStrictEqual(
new WebhookValidationError(
'You cannot send responses back to https://example.com.',
),
)
})
})