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: support x-forwarded-path #579

Merged
merged 1 commit into from
Nov 11, 2024
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
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
tenantId: string
requestUrlLengthLimit: number
requestXForwardedHostRegExp?: string
requestAllowXForwardedPrefix?: boolean
logLevel?: string
logflareEnabled?: boolean
logflareApiKey?: string
Expand Down Expand Up @@ -194,6 +195,8 @@
'REQUEST_X_FORWARDED_HOST_REGEXP',
'X_FORWARDED_HOST_REGEXP'
),
requestAllowXForwardedPrefix:
getOptionalConfigFromEnv('REQUEST_ALLOW_X_FORWARDED_PATH') === 'true',
requestUrlLengthLimit:
Number(getOptionalConfigFromEnv('REQUEST_URL_LENGTH_LIMIT', 'URL_LENGTH_LIMIT')) || 7_500,
requestTraceHeader: getOptionalConfigFromEnv('REQUEST_TRACE_HEADER', 'REQUEST_ID_HEADER'),
Expand Down Expand Up @@ -444,7 +447,7 @@
try {
const parsed = JSON.parse(jwtJWKS)
config.jwtJWKS = parsed
} catch (e: any) {

Check warning on line 450 in src/config.ts

View workflow job for this annotation

GitHub Actions / Test / OS ubuntu-20.04 / Node 20

'e' is defined but never used

Check warning on line 450 in src/config.ts

View workflow job for this annotation

GitHub Actions / Test / OS ubuntu-20.04 / Node 20

Unexpected any. Specify a different type
throw new Error('Unable to parse JWT_JWKS value to JSON')
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/http/plugins/signature-v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
serviceKey,
storageS3Region,
isMultitenant,
requestAllowXForwardedPrefix,
s3ProtocolPrefix,
s3ProtocolAllowForwardedHeader,
s3ProtocolEnforceRegion,
Expand All @@ -37,13 +38,21 @@ export const signatureV4 = fastifyPlugin(
token,
} = await createServerSignature(request.tenantId, clientSignature)

let storagePrefix = s3ProtocolPrefix
if (
requestAllowXForwardedPrefix &&
typeof request.headers['x-forwarded-prefix'] === 'string'
) {
storagePrefix = request.headers['x-forwarded-prefix']
}

const isVerified = signatureV4.verify(clientSignature, {
url: request.url,
body: request.body as string | ReadableStream | Buffer,
headers: request.headers as Record<string, string | string[]>,
method: request.method,
query: request.query as Record<string, string>,
prefix: s3ProtocolPrefix,
prefix: storagePrefix,
})

if (!isVerified && !sessionToken) {
Expand Down
11 changes: 9 additions & 2 deletions src/http/routes/tus/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { UploadId } from '@storage/protocols/tus'

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

const { storageS3Bucket, tusPath } = getConfig()
const { storageS3Bucket, tusPath, requestAllowXForwardedPrefix } = getConfig()
const reExtractFileID = /([^/]+)\/?$/

export const SIGNED_URL_SUFFIX = '/sign'
Expand Down Expand Up @@ -92,8 +92,15 @@ export function generateUrl(
}
proto = process.env.NODE_ENV === 'production' ? 'https' : proto

let basePath = path

const forwardedPath = req.headers['x-forwarded-prefix']
if (requestAllowXForwardedPrefix && typeof forwardedPath === 'string') {
basePath = forwardedPath + path
}

const isSigned = req.url?.endsWith(SIGNED_URL_SUFFIX)
const fullPath = isSigned ? `${path}${SIGNED_URL_SUFFIX}` : path
const fullPath = isSigned ? `${basePath}${SIGNED_URL_SUFFIX}` : basePath

if (req.headers['x-forwarded-host']) {
const port = req.headers['x-forwarded-port']
Expand Down
1 change: 1 addition & 0 deletions src/internal/monitoring/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const whitelistHeaders = (headers: Record<string, unknown>) => {
'x-forwarded-proto',
'x-forwarded-host',
'x-forwarded-port',
'x-forwarded-prefix',
'referer',
'content-length',
'x-real-ip',
Expand Down
4 changes: 2 additions & 2 deletions src/storage/protocols/s3/signature-v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class SignatureV4 {
const serverSignature = this.sign(clientSignature, request)
return crypto.timingSafeEqual(
Buffer.from(clientSignature.signature),
Buffer.from(serverSignature)
Buffer.from(serverSignature.signature)
)
}

Expand Down Expand Up @@ -223,7 +223,7 @@ export class SignatureV4 {
serverCredentials.service
)

return this.hmac(signingKey, stringToSign).toString('hex')
return { signature: this.hmac(signingKey, stringToSign).toString('hex'), canonicalRequest }
}

protected getPayloadHash(clientSignature: ClientSignature, request: SignatureRequest) {
Expand Down
Loading