-
Notifications
You must be signed in to change notification settings - Fork 96
fix(blob): infer callback URL from node headers #1044
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| "@vercel/blob": patch | ||
| --- | ||
|
|
||
| fix(blob): infer handleUpload callback urls from node request headers | ||
|
|
||
| When `handleUpload()` is used with `onUploadCompleted()` and no explicit | ||
| `callbackUrl` is returned from `onBeforeGenerateToken()`, the SDK now falls back | ||
| to Node request headers such as `x-forwarded-host`, `x-forwarded-proto`, and | ||
| `host` to infer the callback URL. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -881,6 +881,11 @@ function getCallbackUrl(request: RequestType): string | undefined { | |||||||||||||||||||||||||||||||||||||
| return `${process.env.VERCEL_BLOB_CALLBACK_URL}${reqPath}`; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const callbackUrlFromHeaders = getCallbackUrlFromHeaders(request, reqPath); | ||||||||||||||||||||||||||||||||||||||
| if (callbackUrlFromHeaders) { | ||||||||||||||||||||||||||||||||||||||
| return callbackUrlFromHeaders; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Not hosted on Vercel and no VERCEL_BLOB_CALLBACK_URL | ||||||||||||||||||||||||||||||||||||||
| if (process.env.VERCEL !== '1') { | ||||||||||||||||||||||||||||||||||||||
| console.warn( | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -910,6 +915,58 @@ function getCallbackUrl(request: RequestType): string | undefined { | |||||||||||||||||||||||||||||||||||||
| return undefined; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| function getCallbackUrlFromHeaders( | ||||||||||||||||||||||||||||||||||||||
| request: RequestType, | ||||||||||||||||||||||||||||||||||||||
| reqPath: string, | ||||||||||||||||||||||||||||||||||||||
| ): string | undefined { | ||||||||||||||||||||||||||||||||||||||
| if (!request.headers) { | ||||||||||||||||||||||||||||||||||||||
| return undefined; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const rawHost = hasHeadersGet(request.headers) | ||||||||||||||||||||||||||||||||||||||
| ? request.headers.get('x-forwarded-host') ?? | ||||||||||||||||||||||||||||||||||||||
| request.headers.get('host') ?? | ||||||||||||||||||||||||||||||||||||||
| undefined | ||||||||||||||||||||||||||||||||||||||
| : getFirstHeaderValue( | ||||||||||||||||||||||||||||||||||||||
| request.headers['x-forwarded-host'] ?? request.headers.host, | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (!rawHost) { | ||||||||||||||||||||||||||||||||||||||
| return undefined; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const host = rawHost.split(',')[0]?.trim(); | ||||||||||||||||||||||||||||||||||||||
| if (!host) { | ||||||||||||||||||||||||||||||||||||||
| return undefined; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const rawProtocol = hasHeadersGet(request.headers) | ||||||||||||||||||||||||||||||||||||||
| ? request.headers.get('x-forwarded-proto') ?? undefined | ||||||||||||||||||||||||||||||||||||||
| : getFirstHeaderValue(request.headers['x-forwarded-proto']); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const protocol = rawProtocol?.split(',')[0]?.trim() || inferProtocol(host); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| return `${protocol}://${host}${reqPath}`; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| function getFirstHeaderValue( | ||||||||||||||||||||||||||||||||||||||
| headerValue: string | string[] | undefined, | ||||||||||||||||||||||||||||||||||||||
| ): string | undefined { | ||||||||||||||||||||||||||||||||||||||
| return Array.isArray(headerValue) ? headerValue[0] : headerValue; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| function hasHeadersGet( | ||||||||||||||||||||||||||||||||||||||
| headers: IncomingMessage['headers'] | Headers | undefined, | ||||||||||||||||||||||||||||||||||||||
| ): headers is Headers { | ||||||||||||||||||||||||||||||||||||||
| return typeof headers === 'object' && headers !== null && 'get' in headers; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| function inferProtocol(host: string): 'http' | 'https' { | ||||||||||||||||||||||||||||||||||||||
| return host.startsWith('localhost') || host.startsWith('127.0.0.1') | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| return host.startsWith('localhost') || host.startsWith('127.0.0.1') | |
| const normalizedHost = host.trim().toLowerCase(); | |
| let hostname: string; | |
| if (normalizedHost.startsWith('[')) { | |
| const closingBracketIndex = normalizedHost.indexOf(']'); | |
| hostname = | |
| closingBracketIndex === -1 | |
| ? normalizedHost.slice(1) | |
| : normalizedHost.slice(1, closingBracketIndex); | |
| } else { | |
| hostname = normalizedHost.split(':', 1)[0] ?? normalizedHost; | |
| } | |
| return hostname === 'localhost' || | |
| hostname === '127.0.0.1' || | |
| hostname === '::1' || | |
| hostname === '0.0.0.0' |
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.
hasHeadersGet()can return a false positive when the NodeIncomingMessage.headersobject contains a header namedget(header names are lowercased), because'get' in headerswill then be true butheaders.getwill be a string/string[]. This would cause a runtimeTypeErrorwhen callingrequest.headers.get(...). Consider tightening the guard to check thatgetis actually a function (e.g.,typeof (headers as any).get === 'function') before treating it as aHeadersinstance.