Skip to content

Commit

Permalink
fix: accept forwarded header on signature, increase migrations timeou…
Browse files Browse the repository at this point in the history
…t, disable host validation on queue (#449)
  • Loading branch information
fenos authored Apr 13, 2024
1 parent 6af2407 commit 577784c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type StorageConfigType = {
tusUseFileVersionSeparator: boolean
defaultMetricsEnabled: boolean
s3ProtocolPrefix: string
s3ProtocolAllowForwardedHeader: boolean
s3ProtocolEnforceRegion: boolean
s3ProtocolAccessKeyId?: string
s3ProtocolAccessKeySecret?: string
Expand Down Expand Up @@ -223,6 +224,8 @@ export function getConfig(options?: { reload?: boolean }): StorageConfigType {

// S3 Protocol
s3ProtocolPrefix: getOptionalConfigFromEnv('S3_PROTOCOL_PREFIX') || '',
s3ProtocolAllowForwardedHeader:
getOptionalConfigFromEnv('S3_ALLOW_FORWARDED_HEADER') === 'true',
s3ProtocolEnforceRegion: getOptionalConfigFromEnv('S3_PROTOCOL_ENFORCE_REGION') === 'true',
s3ProtocolAccessKeyId: getOptionalConfigFromEnv('S3_PROTOCOL_ACCESS_KEY_ID'),
s3ProtocolAccessKeySecret: getOptionalConfigFromEnv('S3_PROTOCOL_ACCESS_KEY_SECRET'),
Expand Down
2 changes: 1 addition & 1 deletion src/database/migrations/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async function connectAndMigrate(options: {

const dbConfig: ClientConfig = {
connectionString: databaseUrl,
connectionTimeoutMillis: 10_000,
connectionTimeoutMillis: 60_000,
options: `-c search_path=${searchPath}`,
ssl,
}
Expand Down
1 change: 1 addition & 0 deletions src/queue/events/base-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export abstract class BaseEvent<T extends Omit<BasePayload, '$version'>> {
superUser: adminUser,
host: payload.tenant.host,
tenantId: payload.tenant.ref,
disableHostCheck: true,
})

const db = new StorageKnexDB(client, {
Expand Down
13 changes: 13 additions & 0 deletions src/storage/protocols/s3/signature-v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ERRORS } from '../../errors'

interface SignatureV4Options {
enforceRegion: boolean
allowForwardedHeader?: boolean
credentials: Omit<Credentials, 'shortDate'> & { secretKey: string }
}

Expand Down Expand Up @@ -56,10 +57,12 @@ export const ALWAYS_UNSIGNABLE_HEADERS = {
export class SignatureV4 {
public readonly serverCredentials: SignatureV4Options['credentials']
enforceRegion: boolean
allowForwardedHeader?: boolean

constructor(options: SignatureV4Options) {
this.serverCredentials = options.credentials
this.enforceRegion = options.enforceRegion
this.allowForwardedHeader = options.allowForwardedHeader
}

static parseAuthorizationHeader(header: string) {
Expand Down Expand Up @@ -257,6 +260,16 @@ export class SignatureV4 {
.sort()
.map((header) => {
if (header === 'host') {
if (this.allowForwardedHeader) {
const forwarded = this.getHeader(request, 'forwarded')
if (forwarded) {
const extractedHost = /host="?([^";]+)/.exec(forwarded)?.[1]
if (extractedHost) {
return `host:${extractedHost.toLowerCase()}`
}
}
}

const xForwardedHost = this.getHeader(request, 'x-forwarded-host')
if (xForwardedHost) {
return `host:${xForwardedHost.toLowerCase()}`
Expand Down

0 comments on commit 577784c

Please sign in to comment.