Skip to content

Commit adf7114

Browse files
feat(web): custom search templates reject credentials and warn when empty
A %s in the URL's credentials put the query in the username instead of the search page, and an empty Custom field fell back to DuckDuckGo without saying so. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 13ec235 commit adf7114

3 files changed

Lines changed: 11 additions & 6 deletions

File tree

‎apps/web/src/components/settings/IntegrationsSettings.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,9 @@ function BrowserSearchEngineSetting() {
747747
title="Custom search URL"
748748
description="Use %s where the search terms go."
749749
status={
750-
customUrl !== "" && !isValidSearchUrlTemplate(customUrl)
751-
? "Enter an http or https URL containing %s. Searches use DuckDuckGo until then."
752-
: undefined
750+
isValidSearchUrlTemplate(customUrl)
751+
? undefined
752+
: "Enter an http or https URL containing %s. Searches use DuckDuckGo until then."
753753
}
754754
control={
755755
<DraftInput

‎packages/shared/src/preview.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ describe("resolveSearchUrlTemplate", () => {
8383
"https://search.example/?q=",
8484
"javascript:alert('%s')",
8585
"https://%s.example.com/",
86+
"https://%[email protected]/?q=%s",
8687
]) {
8788
expect(
8889
resolveSearchUrlTemplate({ browserSearchEngine: "custom", browserCustomSearchUrl }),

‎packages/shared/src/preview.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,19 @@ function fillSearchUrlTemplate(template: string, query: string): string {
8282

8383
/**
8484
* Whether a custom search URL is an http(s) URL with `%s` where the query goes.
85-
* `%s` in the host is rejected: a query with spaces would make it unreachable.
85+
* `%s` in the host or credentials is rejected: the query would never reach a
86+
* search page, and a query with spaces would make the host unreachable.
8687
*/
8788
export function isValidSearchUrlTemplate(template: string): boolean {
8889
if (!template.includes("%s")) return false;
8990
try {
90-
// Two queries land on different hosts only when `%s` is in the host.
91+
// Two queries land on different authorities only when `%s` sits before the path.
92+
const authority = (url: URL) => `${url.username}:${url.password}@${url.host}`;
9193
const url = new URL(fillSearchUrlTemplate(template, "a"));
9294
const other = new URL(fillSearchUrlTemplate(template, "b"));
93-
return (url.protocol === "http:" || url.protocol === "https:") && url.host === other.host;
95+
return (
96+
(url.protocol === "http:" || url.protocol === "https:") && authority(url) === authority(other)
97+
);
9498
} catch {
9599
return false;
96100
}

0 commit comments

Comments
 (0)