Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type StorageConfigType = {
requestUrlLengthLimit: number
requestXForwardedHostRegExp?: string
requestAllowXForwardedPrefix?: boolean
storagePublicUrl?: string
logLevel?: string
logflareEnabled?: boolean
logflareApiKey?: string
Expand Down Expand Up @@ -284,6 +285,7 @@ export function getConfig(options?: { reload?: boolean }): StorageConfigType {
),
requestAllowXForwardedPrefix:
getOptionalConfigFromEnv('REQUEST_ALLOW_X_FORWARDED_PATH') === 'true',
storagePublicUrl: getOptionalConfigFromEnv('STORAGE_PUBLIC_URL'),
requestUrlLengthLimit:
Number(getOptionalConfigFromEnv('REQUEST_URL_LENGTH_LIMIT', 'URL_LENGTH_LIMIT')) || 7_500,
requestTraceHeader: getOptionalConfigFromEnv('REQUEST_TRACE_HEADER', 'REQUEST_ID_HEADER'),
Expand Down
10 changes: 8 additions & 2 deletions src/http/routes/tus/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import type { ServerRequest as Request } from 'srvx'

import { getConfig } from '../../../config'

const { storageS3Bucket, tusPath, requestAllowXForwardedPrefix } = getConfig()
const { storageS3Bucket, tusPath, requestAllowXForwardedPrefix, storagePublicUrl } = getConfig()
const parsedPublicUrl = storagePublicUrl ? new URL(storagePublicUrl) : undefined
const reExtractFileID = /([^/]+)\/?$/

export const SIGNED_URL_SUFFIX = '/sign'
Expand Down Expand Up @@ -112,6 +113,11 @@ export function generateUrl(
throw ERRORS.InvalidParameter('url')
}

if (parsedPublicUrl) {
proto = parsedPublicUrl.protocol.replace(':', '')
host = parsedPublicUrl.host
}

proto = process.env.NODE_ENV === 'production' ? 'https' : proto

let basePath = path
Expand All @@ -125,7 +131,7 @@ export function generateUrl(
const isSigned = req.url?.endsWith(SIGNED_URL_SUFFIX)
const fullPath = isSigned ? `${basePath}${SIGNED_URL_SUFFIX}` : basePath

if (req.headers['x-forwarded-host']) {
if (!parsedPublicUrl && req.headers['x-forwarded-host']) {
const port = req.headers['x-forwarded-port']

if (typeof port === 'string' && port && !['443', '80'].includes(port)) {
Expand Down