Skip to content

Commit

Permalink
Report invalid public suffix inputs as error instead of warning
Browse files Browse the repository at this point in the history
For example, previously https://com would be reported as the warning
"URL components other than site (https://null) will be ignored".
  • Loading branch information
apasel422 committed Dec 10, 2024
1 parent 7bddb39 commit 09d9e03
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
10 changes: 10 additions & 0 deletions ts/src/header-validator/source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ const testCases: TestCase[] = [
},
],
},
{
name: 'destination-uses-public-suffix',
input: `{"destination": "https://com"}`,
expectedErrors: [
{
msg: 'hostname com does not correspond to a valid site',
path: ['destination'],
},
],
},

{
name: 'filter-data-wrong-type',
Expand Down
32 changes: 17 additions & 15 deletions ts/src/header-validator/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ function suitableScope(
s: string,
ctx: Context,
label: string,
scope: (url: URL) => string
scope: (url: URL, ctx: Context) => Maybe<string>
): Maybe<string> {
let url
try {
Expand All @@ -361,24 +361,26 @@ function suitableScope(
return Maybe.None
}

const scoped = scope(url)
if (url.toString() !== new URL(scoped).toString()) {
ctx.warning(
`URL components other than ${label} (${scoped}) will be ignored`
)
}
return Maybe.some(scoped)
return scope(url, ctx).peek((scoped) => {
if (url.toString() !== new URL(scoped).toString()) {
ctx.warning(
`URL components other than ${label} (${scoped}) will be ignored`
)
}
})
}

export function suitableOrigin(s: string, ctx: Context): Maybe<string> {
return suitableScope(s, ctx, 'origin', (u) => u.origin)
return suitableScope(s, ctx, 'origin', (u) => Maybe.some(u.origin))
}

export function suitableSite(s: string, ctx: Context): Maybe<string> {
return suitableScope(
s,
ctx,
'site',
(u) => `${u.protocol}//${psl.get(u.hostname)}`
)
return suitableScope(s, ctx, 'site', (u, ctx) => {
const site = psl.get(u.hostname)
if (site === null) {
ctx.error(`hostname ${u.hostname} does not correspond to a valid site`)
return Maybe.None
}
return Maybe.some(`${u.protocol}//${site}`)
})
}

0 comments on commit 09d9e03

Please sign in to comment.