fix: require anonymous endpoint methods for logged paths

This commit is contained in:
Thomas Durieux committed 2026-11-15 00:14:01 +00:00
1 parent 7d044525ce
commit 1723f5e927
9 files changed
+67 -22

No files matched your search

+2 -2
View File
@@ -72,8 +72,8 @@ export function logRecord(level: string, module: string, args: unknown[]) {
if (context.method && allowed(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "OTHER"], context.method)) record.method = context.method;
Object.assign(record, safeDiagnosticFields(details));
// Anonymous request paths are diagnostic fields, never metric labels.
const requestPath = safeReproductionPath(context.requestPath || details.requestPath);
if (requestPath) Object.assign(record, requestReproduction(requestPath));
const requestPath = safeReproductionPath(context.requestPath || details.requestPath, record.method);
if (requestPath) Object.assign(record, requestReproduction(requestPath, record.method));
if (allowed(["download", "remove", "cache"], details.queue)) record.queue = details.queue;
if (details.stages && typeof details.stages === "object") for (const key of stages) {
let value: unknown;
+8 -3
View File
@@ -3,10 +3,15 @@
// Listing folders use query parameters; a wildcard here would capture upstream
// /:owner/files/{readme,branches,tree,file} source lookups.
const anonymousRequest = /^\/api\/repo\/([A-Za-z0-9_-]{3,200})(?:\/(file\/[A-Za-z0-9._~!$&'()+,;=@%/-]+|files(?:\/(?:counts|search))?\/?|(?:zip|options|refresh|coauthors|extend)\/?))?\/?$/;
export function safeReproductionPath(value: unknown): string | undefined {
export function safeReproductionPath(value: unknown, method: unknown): string | undefined {
if (typeof value !== "string" || value.length > 2048) return undefined;
const match = anonymousRequest.exec(value);
if (!match || /%(?![a-fA-F0-9]{2})/.test(value)) return undefined;
const action = (match[2] || "").replace(/\/+$/, "");
const allowedMethods = !action ? ["GET", "HEAD", "POST", "DELETE"]
: ["refresh", "extend", "coauthors"].includes(action) ? ["POST"] : ["GET", "HEAD"];
// GET coauthors is shadowed by the earlier source metadata handler.
if (typeof method !== "string" || !allowedMethods.includes(method)) return undefined;
try {
const path = decodeURIComponent(match[2] || "");
// Reject controls, traversal, URL syntax, encoded delimiters and double encoding.
@@ -15,7 +20,7 @@ export function safeReproductionPath(value: unknown): string | undefined {
} catch { return undefined; }
return value;
}
export function requestReproduction(url: string) {
const requestPath = safeReproductionPath(url.split("?", 1)[0]);
export function requestReproduction(url: string, method: unknown) {
const requestPath = safeReproductionPath(url.split("?", 1)[0], method);
return requestPath ? { requestPath, repoId: anonymousRequest.exec(requestPath)![1] } : {};
}
+1 -1
View File
@@ -117,7 +117,7 @@ export function monitorRequests(service: Service, metrics = requestMetrics,
: req.originalUrl.startsWith("/w/") ? "website" : "file";
const trace = { id, service, traffic, stages: {} };
const route = requestRoute(req.originalUrl, service);
const reproduction = service === "api" ? requestReproduction(req.originalUrl) : {};
const reproduction = service === "api" ? requestReproduction(req.originalUrl, req.method) : {};
const method = methods.has(req.method) ? req.method : "OTHER";
const started = performance.now();
let firstByteMs: number | undefined;