security: authorize and validate presigned upload requests

POST /api/v1/uploads minted an upload key from caller input with no
ownership check, no content-type allowlist, and the raw filename
interpolated into the S3 key.

- Ownership: a caller may only mint upload URLs for a route/activity
  they own, enforced via the branded loadOwnedRoute/loadOwnedActivity
  (404 on miss, no existence leak). Closes writing into another
  user's resource key-space.
- Content type: the request schema now constrains contentType to an
  image/gpx allowlist, so active content (HTML/SVG/JS) that would
  execute if served inline is rejected at the request boundary.
- Filename: sanitizeUploadFilename() reduces the client filename to a
  safe basename (charset-restricted, no path components, no leading
  dots, length-bounded) before it becomes part of the key.

Tests: schema allow/deny + filename sanitization in @trails-cool/api;
handler tests covering owned-success, not-owner 404, disallowed
content-type, and the route-vs-activity ownership branch.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-06-10 22:23:14 +02:00
parent e64155b490
commit 01d4832edb
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9
5 changed files with 203 additions and 5 deletions

View file

@ -55,5 +55,6 @@ export {
// Uploads
export {
PresignedUploadRequestSchema, PresignedUploadResponseSchema,
ALLOWED_UPLOAD_CONTENT_TYPES, sanitizeUploadFilename,
type PresignedUploadRequest, type PresignedUploadResponse,
} from "./uploads.ts";

View file

@ -0,0 +1,63 @@
import { describe, it, expect } from "vitest";
import {
PresignedUploadRequestSchema,
sanitizeUploadFilename,
} from "./uploads.ts";
const ROUTE_ID = "11111111-1111-4111-8111-111111111111";
describe("PresignedUploadRequestSchema", () => {
it("accepts an allowed content type", () => {
const r = PresignedUploadRequestSchema.safeParse({
filename: "photo.jpg",
contentType: "image/jpeg",
resourceType: "activity",
resourceId: ROUTE_ID,
});
expect(r.success).toBe(true);
});
it("rejects active/disallowed content types", () => {
for (const contentType of ["text/html", "image/svg+xml", "application/javascript"]) {
const r = PresignedUploadRequestSchema.safeParse({
filename: "x", contentType, resourceType: "route", resourceId: ROUTE_ID,
});
expect(r.success, contentType).toBe(false);
}
});
it("rejects a non-uuid resourceId and empty filename", () => {
expect(PresignedUploadRequestSchema.safeParse({
filename: "a.png", contentType: "image/png", resourceType: "route", resourceId: "nope",
}).success).toBe(false);
expect(PresignedUploadRequestSchema.safeParse({
filename: "", contentType: "image/png", resourceType: "route", resourceId: ROUTE_ID,
}).success).toBe(false);
});
});
describe("sanitizeUploadFilename", () => {
it("strips path components (no traversal into the key)", () => {
expect(sanitizeUploadFilename("../../etc/passwd")).toBe("passwd");
expect(sanitizeUploadFilename("/abs/path/img.png")).toBe("img.png");
expect(sanitizeUploadFilename("a\\b\\c.jpg")).toBe("c.jpg");
});
it("restricts to a conservative charset and collapses runs", () => {
expect(sanitizeUploadFilename("my photo (1)!.jpg")).toBe("my_photo_1_.jpg");
});
it("strips leading dots so dotfiles / .. can't form the segment", () => {
expect(sanitizeUploadFilename("...hidden")).toBe("hidden");
expect(sanitizeUploadFilename("..")).toBe("upload");
});
it("never returns empty", () => {
expect(sanitizeUploadFilename("")).toBe("upload");
expect(sanitizeUploadFilename("////")).toBe("upload");
});
it("bounds the length", () => {
expect(sanitizeUploadFilename("a".repeat(500)).length).toBeLessThanOrEqual(128);
});
});

View file

@ -1,12 +1,40 @@
import { z } from "zod";
/**
* Content types accepted for uploads. Constrained so a caller can't
* stash active content (HTML/SVG/JS) that would execute if the object
* were ever served inline. Keep in sync with what the upload UI sends.
*/
export const ALLOWED_UPLOAD_CONTENT_TYPES = [
"image/jpeg",
"image/png",
"image/webp",
"image/gif",
"application/gpx+xml",
] as const;
export const PresignedUploadRequestSchema = z.object({
filename: z.string(),
contentType: z.string(),
filename: z.string().min(1).max(255),
contentType: z.enum(ALLOWED_UPLOAD_CONTENT_TYPES),
resourceType: z.enum(["route", "activity"]),
resourceId: z.uuid(),
});
/**
* Reduce a client-supplied filename to a safe S3 object-key segment:
* basename only (drop any path), restrict to a conservative charset,
* collapse runs, strip leading dots, and bound the length. Never empty.
*/
export function sanitizeUploadFilename(filename: string): string {
const base = filename.split(/[/\\]/).pop() ?? "";
const cleaned = base
.replace(/[^A-Za-z0-9._-]/g, "_")
.replace(/_{2,}/g, "_")
.replace(/^\.+/, "")
.slice(0, 128);
return cleaned.length > 0 ? cleaned : "upload";
}
export const PresignedUploadResponseSchema = z.object({
uploadUrl: z.url(),
storageKey: z.string(),