mirror of
https://github.com/morluto/rea
synced 2026-10-27 14:42:18 -04:00
21 lines
700 B
JavaScript
21 lines
700 B
JavaScript
/** Redact only HTTP URL authentication; keep paths, names, queries and other text. */
|
|
export function redactTransportAuthentication(text) {
|
|
return text.replace(/https?:\/\/[^\s"<>\\]+/giu, (value) => {
|
|
try {
|
|
const parsed = new URL(value);
|
|
if (!parsed.username && !parsed.password) return value;
|
|
const start = value.indexOf("://") + 3;
|
|
const tail = value.slice(start),
|
|
end = tail.search(/[/?#]/u);
|
|
const authority = end < 0 ? tail : tail.slice(0, end);
|
|
return (
|
|
value.slice(0, start) +
|
|
authority.slice(authority.lastIndexOf("@") + 1) +
|
|
(end < 0 ? "" : tail.slice(end))
|
|
);
|
|
} catch {
|
|
return value;
|
|
}
|
|
});
|
|
}
|