Skip to content

Commit

Permalink
fix: tus signed upload url validation (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos authored May 2, 2024
1 parent d431640 commit ec6e2f6
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/http/routes/tus/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function onIncomingRequest(
const uploadID = UploadId.fromString(id)

// Handle signed url requests
if (req.url?.startsWith(`${tusPath}${SIGNED_URL_SUFFIX}`)) {
if (req.url?.startsWith(`/upload/resumable/sign`)) {
const signature = req.headers['x-signature']
if (!signature || (signature && typeof signature !== 'string')) {
throw ERRORS.InvalidSignature('Missing x-signature header')
Expand All @@ -64,7 +64,7 @@ export async function onIncomingRequest(
}

// All other requests need to be authorized if they have permission to upload
const isUpsert = req.headers['x-upsert'] === 'true'
const isUpsert = req.upload.isUpsert
const uploader = new Uploader(req.upload.storage.backend, req.upload.storage.db)

await uploader.canUpload({
Expand All @@ -84,16 +84,13 @@ export function generateUrl(
) {
proto = process.env.NODE_ENV === 'production' ? 'https' : proto

// validate allowed paths
const allowedPaths = [path, `${path}${SIGNED_URL_SUFFIX}`]
if (!allowedPaths.includes(req.url || '')) {
throw ERRORS.InvalidSignature('The url provided is not allowed for upload')
}
const isSigned = req.url?.endsWith(SIGNED_URL_SUFFIX)
const fullPath = isSigned ? `${path}${SIGNED_URL_SUFFIX}` : path

// remove the tenant-id from the url, since we'll be using the tenant-id from the request
id = id.split('/').slice(1).join('/')
id = Buffer.from(id, 'utf-8').toString('base64url')
return `${proto}://${host}${req.url}/${id}`
return `${proto}://${host}${fullPath}/${id}`
}

/**
Expand Down

0 comments on commit ec6e2f6

Please sign in to comment.