mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-11-17 17:44:09 +01:00
fix: retain actual anonymous request paths across outcomes
This commit is contained in:
1 parent
b54cf72bf8
commit
953767b2cd
8 files changed
+57
-22
No files matched your search
@@ -71,11 +71,9 @@ export function logRecord(level: string, module: string, args: unknown[]) {
|
||||
if (typeof route === "string" && LOG_ROUTES.includes(route)) record.route = route;
|
||||
if (context.method && allowed(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "OTHER"], context.method)) record.method = context.method;
|
||||
Object.assign(record, safeDiagnosticFields(details));
|
||||
// Reproduction data is restricted to failures, not successful traffic/metrics.
|
||||
if (["error", "fatal"].includes(level) || Number(details.status || details.httpStatus) >= 400) {
|
||||
const requestPath = safeReproductionPath(context.requestPath || details.requestPath);
|
||||
if (requestPath) Object.assign(record, requestReproduction(requestPath));
|
||||
}
|
||||
// Anonymous request paths are diagnostic fields, never metric labels.
|
||||
const requestPath = safeReproductionPath(context.requestPath || details.requestPath);
|
||||
if (requestPath) Object.assign(record, requestReproduction(requestPath));
|
||||
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;
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
// Explicit admin diagnostics: public anonymous repository ID and request pathname only.
|
||||
// Never collect upstream owner/repository URLs, headers, query strings or bodies.
|
||||
const fileRequest = /^\/api\/repo\/([A-Za-z0-9_-]{3,200})\/file\/([A-Za-z0-9._~!$&'()+,;=@%/-]+)$/;
|
||||
const anonymousRequest = /^\/api\/repo\/([A-Za-z0-9_-]{3,200})(?:\/(file\/[A-Za-z0-9._~!$&'()+,;=@%/-]+|files(?:\/[A-Za-z0-9._~!$&'()+,;=@%/-]*)?|(?:zip|options|refresh|coauthors|extend)\/?))?\/?$/;
|
||||
export function safeReproductionPath(value: unknown): string | undefined {
|
||||
if (typeof value !== "string" || value.length > 2048) return undefined;
|
||||
const match = fileRequest.exec(value);
|
||||
const match = anonymousRequest.exec(value);
|
||||
if (!match || /%(?![a-fA-F0-9]{2})/.test(value)) return undefined;
|
||||
try {
|
||||
const path = decodeURIComponent(match[2]);
|
||||
const path = decodeURIComponent(match[2] || "");
|
||||
// Reject controls, traversal, URL syntax, encoded delimiters and double encoding.
|
||||
// eslint-disable-next-line no-control-regex -- reject control characters from stored paths
|
||||
if (/[\x00-\x1f\x7f-\x9f\u2028-\u202e\u2066-\u2069\ufffd\\:?#%]/.test(path) || path.startsWith("/") || path.split("/").some(part => part === "." || part === "..")) return undefined;
|
||||
if (/[\x00-\x1f\x7f-\x9f\u2028-\u202e\u2066-\u2069\ufffd\\:?#%]/.test(path) || path.includes("//") || path.split("/").some(part => part === "." || part === "..")) return undefined;
|
||||
} catch { return undefined; }
|
||||
return value;
|
||||
}
|
||||
export function requestReproduction(url: string) {
|
||||
const requestPath = safeReproductionPath(url.split("?", 1)[0]);
|
||||
return requestPath ? { requestPath, repoId: fileRequest.exec(requestPath)![1] } : {};
|
||||
return requestPath ? { requestPath, repoId: anonymousRequest.exec(requestPath)![1] } : {};
|
||||
}
|
||||
@@ -158,7 +158,7 @@ export function monitorRequests(service: Service, metrics = requestMetrics,
|
||||
metrics.observe("request", route, method, ms, { bytes, aborted, error: res.statusCode >= 500, slow });
|
||||
if (firstByteMs != null) metrics.observe("first_byte", route, method, firstByteMs);
|
||||
const responseMs = firstByteMs == null ? null : Math.max(0, ms - firstByteMs);
|
||||
const detail = { ...(res.statusCode >= 400 ? reproduction : {}), requestId: id, service, method, route, status: res.statusCode,
|
||||
const detail = { ...reproduction, requestId: id, service, method, route, status: res.statusCode,
|
||||
outcome: aborted ? "interrupted" : "completed", ms: Math.round(ms), firstByteMs: firstByteMs == null ? null : Math.round(firstByteMs),
|
||||
responseMs: responseMs == null ? null : Math.round(responseMs),
|
||||
bytesPerSecond: responseMs != null && responseMs >= 1 && bytes > 0 ? Math.round(bytes * 1000 / responseMs) : null,
|
||||
|
||||
Reference in new issue
Block a user