mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
Redact credentials from logged request urls (#1074)
The controller logs the url and query of every HTTP request. Plugins doing OAuth get the authorization code as a query parameter on their callback route, so it ended up in the log. redactUrl now accepts a path, code joins the redacted parameter names, and redactQuery covers the parsed query in the log metadata. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2c506df38a
commit
24586a4b7c
3 files changed
+58
-12
No files matched your search
@@ -277,6 +277,8 @@ const sensitiveSearchParams = new Set([
|
||||
"access_token",
|
||||
"api_key",
|
||||
"apikey",
|
||||
// OAuth2 authorization code, plugins doing OAuth receive it on their callback route
|
||||
"code",
|
||||
"password",
|
||||
"secret",
|
||||
"token",
|
||||
@@ -290,19 +292,25 @@ const sensitiveSearchParams = new Set([
|
||||
* 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.
|
||||
* Accepts a path with a query string, like the url of an incoming HTTP
|
||||
* request, and returns a path in that case. 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.
|
||||
* @param url - URL or path to redact credentials from.
|
||||
* @returns url with credentials replaced by `REDACTED`.
|
||||
*/
|
||||
export function redactUrl(url: URL | string) {
|
||||
const isPath = typeof url === "string" && url.startsWith("/") && !url.startsWith("//");
|
||||
let parsed;
|
||||
try {
|
||||
parsed = new URL(url);
|
||||
parsed = isPath ? new URL(url, "http://localhost") : new URL(url);
|
||||
} catch {
|
||||
return String(url);
|
||||
}
|
||||
if (![...parsed.searchParams.keys()].some(name => sensitiveSearchParams.has(name.toLowerCase()))) {
|
||||
return String(url);
|
||||
}
|
||||
|
||||
const searchParams = new URLSearchParams();
|
||||
for (const [name, value] of parsed.searchParams) {
|
||||
@@ -310,7 +318,25 @@ export function redactUrl(url: URL | string) {
|
||||
}
|
||||
|
||||
parsed.search = searchParams.toString();
|
||||
return parsed.toString();
|
||||
return isPath ? parsed.pathname + parsed.search + parsed.hash : parsed.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy of parsed query parameters with credentials replaced by `REDACTED`
|
||||
*
|
||||
* Uses the same parameter names as {@link redactUrl}.
|
||||
*
|
||||
* @param query - query parameters, for example `req.query` from express.
|
||||
* @returns query safe to write to the log.
|
||||
*/
|
||||
export function redactQuery(query: Record<string, unknown>) {
|
||||
const result = { ...query };
|
||||
for (const name of Object.keys(result)) {
|
||||
if (sensitiveSearchParams.has(name.toLowerCase())) {
|
||||
result[name] = "REDACTED";
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function skipWhitespace(pos: number, input: string) {
|
||||
|
||||
Reference in new issue
Block a user