-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
chore(plugin-server): Validate fetch
hostnames
#17183
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ee439fb
chore(plugin-server): Validate fetch hostnames
Twixes 6a4657e
Only apply Python host check on Cloud
Twixes 7471097
Update tests to use valid hook URLs
Twixes b80ddc6
Only apply plugin server host check in prod
Twixes 42ab93c
Update URLs in a couple more tests
Twixes eb321fc
Only check hostnames on Cloud and remove port check
Twixes 8f9883a
Fix fetch mocking
Twixes 7dc3bab
Roll out hostname guard per project
Twixes a030f1f
Fix fetch call assertions
Twixes 9962356
Make `fetchHostnameGuardTeams` optional
Twixes 02c6e0e
Merge branch 'master' into validate-app-fetch
Twixes 04eca49
Merge branch 'master' into validate-app-fetch
Twixes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file modified
BIN
-34.8 KB
(68%)
...napshots__/scenes-app-recordings--recordings-play-list-no-pinned-recordings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,73 @@ | ||
// This module wraps node-fetch with a sentry tracing-aware extension | ||
|
||
import fetch, { FetchError, Request, Response } from 'node-fetch' | ||
import { LookupAddress } from 'dns' | ||
import dns from 'dns/promises' | ||
import * as ipaddr from 'ipaddr.js' | ||
import fetch, { type RequestInfo, type RequestInit, type Response, FetchError, Request } from 'node-fetch' | ||
import { URL } from 'url' | ||
|
||
import { runInSpan } from '../sentry' | ||
|
||
function fetchWrapper(...args: Parameters<typeof fetch>): Promise<Response> { | ||
const request = new Request(...args) | ||
return runInSpan( | ||
export async function trackedFetch(url: RequestInfo, init?: RequestInit): Promise<Response> { | ||
const request = new Request(url, init) | ||
return await runInSpan( | ||
{ | ||
op: 'fetch', | ||
description: `${request.method} ${request.url}`, | ||
}, | ||
() => fetch(...args) | ||
async () => await fetch(url, init) | ||
) | ||
} | ||
|
||
fetchWrapper.isRedirect = fetch.isRedirect | ||
fetchWrapper.FetchError = FetchError | ||
trackedFetch.isRedirect = fetch.isRedirect | ||
trackedFetch.FetchError = FetchError | ||
|
||
export default fetchWrapper | ||
export async function safeTrackedFetch(url: RequestInfo, init?: RequestInit): Promise<Response> { | ||
const request = new Request(url, init) | ||
return await runInSpan( | ||
{ | ||
op: 'fetch', | ||
description: `${request.method} ${request.url}`, | ||
}, | ||
async () => { | ||
await raiseIfUserProvidedUrlUnsafe(request.url) | ||
return await fetch(url, init) | ||
} | ||
) | ||
} | ||
|
||
safeTrackedFetch.isRedirect = fetch.isRedirect | ||
safeTrackedFetch.FetchError = FetchError | ||
|
||
/** | ||
* Raise if the provided URL seems unsafe, otherwise do nothing. | ||
* | ||
* Equivalent of Django raise_if_user_provided_url_unsafe. | ||
*/ | ||
export async function raiseIfUserProvidedUrlUnsafe(url: string): Promise<void> { | ||
// Raise if the provided URL seems unsafe, otherwise do nothing. | ||
let parsedUrl: URL | ||
try { | ||
parsedUrl = new URL(url) | ||
} catch (err) { | ||
throw new FetchError('Invalid URL', 'posthog-host-guard') | ||
} | ||
if (!parsedUrl.hostname) { | ||
throw new FetchError('No hostname', 'posthog-host-guard') | ||
} | ||
if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') { | ||
throw new FetchError('Scheme must be either HTTP or HTTPS', 'posthog-host-guard') | ||
} | ||
let addrinfo: LookupAddress[] | ||
try { | ||
addrinfo = await dns.lookup(parsedUrl.hostname, { all: true }) | ||
} catch (err) { | ||
throw new FetchError('Invalid hostname', 'posthog-host-guard') | ||
} | ||
for (const { address } of addrinfo) { | ||
// Prevent addressing internal services | ||
if (ipaddr.parse(address).range() !== 'unicast') { | ||
throw new FetchError('Internal hostname', 'posthog-host-guard') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just calling out this line to any other reviewers as being the "opt in"