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

fix: tus signed upload url validation #462

Merged
merged 1 commit into from
May 2, 2024
Merged
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
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
Loading