@@ -26,6 +26,20 @@ const ABSOLUTE_HTTP = /^https?:\/\//i;
2626const EXPLICIT_SCHEME = / ^ [ a - z ] [ a - z 0 - 9 + \- . ] * : / i;
2727const PROTOCOL_RELATIVE = / ^ \/ \/ / ;
2828
29+ function getRawGitHubCommitRoot ( assetBaseUrl : string ) : URL | null {
30+ try {
31+ const baseUrl = new URL ( assetBaseUrl ) ;
32+ if ( baseUrl . protocol !== "https:" || baseUrl . hostname !== "raw.githubusercontent.com" ) {
33+ return null ;
34+ }
35+ const [ owner , repo , commit ] = baseUrl . pathname . split ( "/" ) . filter ( Boolean ) ;
36+ if ( ! owner || ! repo || ! commit ) return null ;
37+ return new URL ( `/${ owner } /${ repo } /${ commit } /` , baseUrl . origin ) ;
38+ } catch {
39+ return null ;
40+ }
41+ }
42+
2943function resolveRelativeSrc ( src : string , assetBaseUrl : string | undefined ) : string | null {
3044 if ( ! assetBaseUrl ) return null ;
3145 if ( ! src ) return null ;
@@ -38,7 +52,12 @@ function resolveRelativeSrc(src: string, assetBaseUrl: string | undefined): stri
3852 // pulling random repo-root files.
3953 if ( src . startsWith ( "/" ) ) return null ;
4054 try {
41- return new URL ( src , assetBaseUrl ) . toString ( ) ;
55+ const resolved = new URL ( src , assetBaseUrl ) ;
56+ const commitRoot = getRawGitHubCommitRoot ( assetBaseUrl ) ;
57+ if ( ! commitRoot ) return null ;
58+ if ( resolved . origin !== commitRoot . origin ) return null ;
59+ if ( ! resolved . pathname . startsWith ( commitRoot . pathname ) ) return null ;
60+ return resolved . toString ( ) ;
4261 } catch {
4362 return null ;
4463 }
0 commit comments