File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -133,6 +133,8 @@ export function safeDomain(dn: unknown): string | null {
133133 . replace ( / ^ h t t p s ? : \/ \/ / , "" )
134134 . replace ( / [ / ? # & ] .* $ / , "" ) ;
135135 if ( ! / ^ [ a - z 0 - 9 . - ] { 3 , 253 } $ / . test ( d ) || ! d . includes ( "." ) || d . includes ( ".." ) ) return null ;
136+ if ( d . startsWith ( "." ) || d . endsWith ( "." ) ) return null ;
137+ if ( d . split ( "." ) . some ( ( label ) => label . startsWith ( "-" ) || label . endsWith ( "-" ) ) ) return null ;
136138 return d ;
137139}
138140
Original file line number Diff line number Diff line change 1+ import assert from "node:assert/strict" ;
2+ import test from "node:test" ;
3+
4+ import { safeDomain } from "../lib/config.ts" ;
5+
6+ test ( "safeDomain strips forwarded paths and accepts ordinary domains" , ( ) => {
7+ assert . equal ( safeDomain ( "https://example.com/path?ref=abc" ) , "example.com" ) ;
8+ assert . equal ( safeDomain ( "Sub.Example.COM#top" ) , "sub.example.com" ) ;
9+ } ) ;
10+
11+ test ( "safeDomain rejects empty labels and edge punctuation" , ( ) => {
12+ assert . equal ( safeDomain ( ".example.com" ) , null ) ;
13+ assert . equal ( safeDomain ( "example.com." ) , null ) ;
14+ assert . equal ( safeDomain ( "example..com" ) , null ) ;
15+ assert . equal ( safeDomain ( "-example.com" ) , null ) ;
16+ assert . equal ( safeDomain ( "example-.com" ) , null ) ;
17+ assert . equal ( safeDomain ( "example.-com" ) , null ) ;
18+ assert . equal ( safeDomain ( "example.com-" ) , null ) ;
19+ } ) ;
You can’t perform that action at this time.
0 commit comments