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: s3 Signed URLs #500

Merged
merged 1 commit into from
Jun 12, 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
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type StorageConfigType = {
s3ProtocolEnforceRegion: boolean
s3ProtocolAccessKeyId?: string
s3ProtocolAccessKeySecret?: string
s3ProtocolNonCanonicalHostHeader?: string
tracingMode?: string
}

Expand Down Expand Up @@ -232,6 +233,9 @@ export function getConfig(options?: { reload?: boolean }): StorageConfigType {
s3ProtocolEnforceRegion: getOptionalConfigFromEnv('S3_PROTOCOL_ENFORCE_REGION') === 'true',
s3ProtocolAccessKeyId: getOptionalConfigFromEnv('S3_PROTOCOL_ACCESS_KEY_ID'),
s3ProtocolAccessKeySecret: getOptionalConfigFromEnv('S3_PROTOCOL_ACCESS_KEY_SECRET'),
s3ProtocolNonCanonicalHostHeader: getOptionalConfigFromEnv(
'S3_PROTOCOL_NON_CANONICAL_HOST_HEADER'
),
// Storage
storageBackendType: getOptionalConfigFromEnv('STORAGE_BACKEND') as StorageBackendType,

Expand Down
14 changes: 12 additions & 2 deletions src/http/plugins/log-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ export const logRequest = (options: RequestLoggerOptions) =>
}

const rMeth = req.method
const rUrl = redactQueryParamFromRequest(req, ['token'])
const rUrl = redactQueryParamFromRequest(req, [
'token',
'X-Amz-Credential',
'X-Amz-Signature',
'X-Amz-Security-Token',
])
const uAgent = req.headers['user-agent']
const rId = req.id
const cIP = req.ip
Expand All @@ -78,7 +83,12 @@ export const logRequest = (options: RequestLoggerOptions) =>
}

const rMeth = req.method
const rUrl = redactQueryParamFromRequest(req, ['token'])
const rUrl = redactQueryParamFromRequest(req, [
'token',
'X-Amz-Credential',
'X-Amz-Signature',
'X-Amz-Security-Token',
])
const uAgent = req.headers['user-agent']
const rId = req.id
const cIP = req.ip
Expand Down
46 changes: 26 additions & 20 deletions src/http/plugins/signature-v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,22 @@ const {
s3ProtocolEnforceRegion,
s3ProtocolAccessKeyId,
s3ProtocolAccessKeySecret,
s3ProtocolNonCanonicalHostHeader,
} = getConfig()

export const signatureV4 = fastifyPlugin(async function (fastify: FastifyInstance) {
fastify.addHook('preHandler', async (request: FastifyRequest) => {
if (typeof request.headers.authorization !== 'string') {
throw ERRORS.AccessDenied('Missing authorization header')
}
type AWSRequest = FastifyRequest<{ Querystring: { 'X-Amz-Credential'?: string } }>

const clientCredentials = SignatureV4.parseAuthorizationHeader(request.headers.authorization)
export const signatureV4 = fastifyPlugin(async function (fastify: FastifyInstance) {
fastify.addHook('preHandler', async (request: AWSRequest) => {
const clientSignature = extractSignature(request)

const sessionToken = request.headers['x-amz-security-token'] as string | undefined
const sessionToken = clientSignature.sessionToken

const {
signature: signatureV4,
claims,
token,
} = await createSignature(request.tenantId, clientCredentials, {
sessionToken: sessionToken,
})
} = await createServerSignature(request.tenantId, clientSignature)

const isVerified = signatureV4.verify({
url: request.url,
Expand All @@ -45,9 +42,7 @@ export const signatureV4 = fastifyPlugin(async function (fastify: FastifyInstanc
method: request.method,
query: request.query as Record<string, string>,
prefix: s3ProtocolPrefix,
credentials: clientCredentials.credentials,
signature: clientCredentials.signature,
signedHeaders: clientCredentials.signedHeaders,
clientSignature: clientSignature,
})

if (!isVerified && !sessionToken) {
Expand Down Expand Up @@ -94,15 +89,23 @@ export const signatureV4 = fastifyPlugin(async function (fastify: FastifyInstanc
})
})

async function createSignature(
tenantId: string,
clientSignature: ClientSignature,
session?: { sessionToken?: string }
) {
function extractSignature(req: AWSRequest) {
if (typeof req.headers.authorization === 'string') {
return SignatureV4.parseAuthorizationHeader(req.headers)
}

if (typeof req.query['X-Amz-Credential'] === 'string') {
return SignatureV4.parseQuerySignature(req.query)
}

throw ERRORS.AccessDenied('Missing signature')
}

async function createServerSignature(tenantId: string, clientSignature: ClientSignature) {
const awsRegion = storageS3Region
const awsService = 's3'

if (session?.sessionToken) {
if (clientSignature?.sessionToken) {
const tenantAnonKey = isMultitenant ? (await getTenantConfig(tenantId)).anonKey : anonKey

if (!tenantAnonKey) {
Expand All @@ -112,6 +115,7 @@ async function createSignature(
const signature = new SignatureV4({
enforceRegion: s3ProtocolEnforceRegion,
allowForwardedHeader: s3ProtocolAllowForwardedHeader,
nonCanonicalForwardedHost: s3ProtocolNonCanonicalHostHeader,
credentials: {
accessKey: tenantId,
secretKey: tenantAnonKey,
Expand All @@ -120,7 +124,7 @@ async function createSignature(
},
})

return { signature, claims: undefined, token: session.sessionToken }
return { signature, claims: undefined, token: clientSignature.sessionToken }
}

if (isMultitenant) {
Expand All @@ -132,6 +136,7 @@ async function createSignature(
const signature = new SignatureV4({
enforceRegion: s3ProtocolEnforceRegion,
allowForwardedHeader: s3ProtocolAllowForwardedHeader,
nonCanonicalForwardedHost: s3ProtocolNonCanonicalHostHeader,
credentials: {
accessKey: credential.accessKey,
secretKey: credential.secretKey,
Expand All @@ -152,6 +157,7 @@ async function createSignature(
const signature = new SignatureV4({
enforceRegion: s3ProtocolEnforceRegion,
allowForwardedHeader: s3ProtocolAllowForwardedHeader,
nonCanonicalForwardedHost: s3ProtocolNonCanonicalHostHeader,
credentials: {
accessKey: s3ProtocolAccessKeyId,
secretKey: s3ProtocolAccessKeySecret,
Expand Down
7 changes: 6 additions & 1 deletion src/monitoring/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export const logger = pino({
region,
traceId: request.id,
method: request.method,
url: redactQueryParamFromRequest(request, ['token']),
url: redactQueryParamFromRequest(request, [
'token',
'X-Amz-Credential',
'X-Amz-Signature',
'X-Amz-Security-Token',
]),
headers: whitelistHeaders(request.headers),
hostname: request.hostname,
remoteAddress: request.ip,
Expand Down
5 changes: 3 additions & 2 deletions src/storage/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum ErrorCode {
BucketAlreadyExists = 'BucketAlreadyExists',
DatabaseTimeout = 'DatabaseTimeout',
InvalidSignature = 'InvalidSignature',
ExpiredToken = 'ExpiredToken',
SignatureDoesNotMatch = 'SignatureDoesNotMatch',
AccessDenied = 'AccessDenied',
ResourceLocked = 'ResourceLocked',
Expand Down Expand Up @@ -145,9 +146,9 @@ export const ERRORS = {

ExpiredSignature: (e?: Error) =>
new StorageBackendError({
code: ErrorCode.InvalidSignature,
code: ErrorCode.ExpiredToken,
httpStatusCode: 400,
message: 'Expired signature',
message: 'The provided token has expired.',
originalError: e,
}),

Expand Down
Loading
Loading