Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for filtering username/password URL authority. #751

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,27 @@ it.each([
])('passes through other URLs unfiltered', (url) => {
expect(defaultUrlFilter(url)).toBe(url);
});

it('filters out username and password from URLs', () => {
const urls = [
// Username only
{
input: 'https://[email protected]/',
expected: 'https://[email protected]/',
},
// Password only
{
input: 'https://:[email protected]/',
expected: 'https://:[email protected]/',
},
// Both username and password
{
input: 'https://user:[email protected]/',
expected: 'https://redacted:[email protected]/',
},
];

urls.forEach(({ input, expected }) => {
expect(defaultUrlFilter(input)).toBe(expected);
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
const pollingRegex = /sdk\/evalx\/[^/]+\/contexts\/(?<context>[^/?]*)\??.*?/;
const streamingREgex = /\/eval\/[^/]+\/(?<context>[^/?]*)\??.*?/;

/**
* Filter which redacts user information (auth) from a URL.
*
* If a username/password is present, then they are replaced with 'redacted'.
* Authority reference: https://developer.mozilla.org/en-US/docs/Web/URI/Authority
*
* @param url URL to filter.
* @returns A filtered URL.
*/
function authorityUrlFilter(url: string): string {
// This will work in browser environments, but in the future we may want to consider an approach
// which doesn't rely on the browser's URL parsing. This is because other environments we may
// want to target, such as ReactNative, may not have as robust URL parsing.
const urlObj = new URL(url);
let hadAuth = false;
if (urlObj.username) {
urlObj.username = 'redacted';
hadAuth = true;
}
if (urlObj.password) {
urlObj.password = 'redacted';
hadAuth = true;
}
if (hadAuth) {
return urlObj.toString();
}
// If there was no auth information, then we don't need to modify the URL.
return url;
}

/**
* Filter which removes context information for browser JavaScript endpoints.
*
* @param url URL to filter.
* @returns A filtered URL.
*/
export default function defaultUrlFilter(url: string): string {
function ldUrlFilter(url: string): string {
// TODO: Maybe we consider a way to identify LD requests so they can be filtered without
// regular expressions.

Expand All @@ -27,3 +57,13 @@ export default function defaultUrlFilter(url: string): string {
}
return url;
}

/**
* Filter which redacts user information and removes context information for browser JavaScript endpoints.
*
* @param url URL to filter.
* @returns A filtered URL.
*/
export default function defaultUrlFilter(url: string): string {
return ldUrlFilter(authorityUrlFilter(url));
}
Loading