Merge pull request #954 from bbassie/feature/redact-mod-portal-token

Redact credentials from URLs in download errors
This commit is contained in:
Cooldude2606 authored and GitHub committed 2026-07-29 23:02:12 +01:00
commit e4c2ccd58f
4 files changed
+104 -1

No files matched your search

+3 -1
View File
@@ -7,6 +7,8 @@ import fs from "node:fs/promises";
import path from "path";
import stream from "stream";
import { redactUrl } from "./helpers";
/**
* Returns the newest file in a directory
@@ -311,7 +313,7 @@ export async function downloadFile(
writeStream.end();
await events.once(writeStream, "close");
await fs.unlink(tempFilePath);
throw new Error(`Failed to download ${url}: ${response.status} ${response.statusText}`);
throw new Error(`Failed to download ${redactUrl(url)}: ${response.status} ${response.statusText}`);
}
const writer = stream.Writable.toWeb(writeStream);
await response.body.pipeTo(writer);
+45
View File
@@ -268,6 +268,51 @@ export function formatBytes(bytes: number, prefixes: "si" | "binary" = "si") {
return significant.toFixed(fractionDigits) + units[exponent];
}
/**
* Query parameters whose values are credentials and must never be logged.
*
* Matched case insensitively against the full parameter name.
*/
const sensitiveSearchParams = new Set([
"access_token",
"api_key",
"apikey",
"password",
"secret",
"token",
]);
/**
* Replace credentials in a URL with a placeholder
*
* Returns the URL as a string with the values of any query parameter
* carrying a credential replaced by `REDACTED`, so that the URL can
* safely be included in log output and error messages. The parameter
* names are kept intact to preserve the diagnostic value of the URL.
*
* Returns the input unchanged if it is a string which does not parse as
* a URL, as there are then no query parameters to redact.
*
* @param url - URL to redact credentials from.
* @returns url with credentials replaced by `REDACTED`.
*/
export function redactUrl(url: URL | string) {
let parsed;
try {
parsed = new URL(url);
} catch {
return String(url);
}
const searchParams = new URLSearchParams();
for (const [name, value] of parsed.searchParams) {
searchParams.append(name, sensitiveSearchParams.has(name.toLowerCase()) ? "REDACTED" : value);
}
parsed.search = searchParams.toString();
return parsed.toString();
}
function skipWhitespace(pos: number, input: string) {
// whitespace = 1*" "
while (pos < input.length && input.charAt(pos) === " ") {