@@ -70,13 +70,49 @@ export const DEFAULT_OPTICS = [
7070 } ,
7171] satisfies DefaultOpticOption [ ] ;
7272
73+ const isPrivateIp4 = ( url : string ) => {
74+ const parts = url . split ( '://' ) ;
75+ const ip =
76+ parts . length > 0
77+ ? parts [ parts . length - 1 ] . replace ( / \/ $ / , '' ) . split ( ':' ) [ 0 ]
78+ : url . replace ( / \/ $ / , '' ) . split ( ':' ) [ 0 ] ;
79+
80+ if ( / ^ ( 1 0 ) \. ( .* ) \. ( .* ) \. ( .* ) $ / . test ( ip ) ) return true ;
81+ if ( / ^ ( 1 7 2 ) \. ( 1 [ 6 - 9 ] | 2 [ 0 - 9 ] | 3 [ 0 - 1 ] ) \. ( .* ) \. ( .* ) $ / . test ( ip ) ) return true ;
82+ if ( / ^ ( 1 9 2 ) \. 1 6 8 \. ( .* ) \. ( .* ) $ / . test ( ip ) ) return true ;
83+ if ( / ^ ( 1 2 7 ) \. ( 0 ) \. ( 0 ) \. ( 1 ) $ / . test ( ip ) ) return true ;
84+ if ( / ^ ( 1 0 0 ) \. ( 6 [ 4 - 9 ] | [ 7 - 9 ] [ 0 - 9 ] | 1 [ 0 - 1 ] [ 0 - 9 ] | 1 2 [ 0 - 7 ] ) \. ( .* ) \. ( .* ) $ / . test ( ip ) ) return true ;
85+
86+ return false ;
87+ } ;
88+
89+ const isPrivateIp6 = ( url : string ) => {
90+ const parts = url . split ( '://' ) ;
91+ const ip =
92+ parts . length > 0
93+ ? parts [ parts . length - 1 ] . replace ( / \/ $ / , '' ) . split ( ':' ) [ 0 ]
94+ : url . replace ( / \/ $ / , '' ) ;
95+
96+ if ( / ^ f e 8 0 : : / i. test ( ip ) ) return true ;
97+ if ( / ^ f d [ 0 - 9 a - f ] { 2 } : / i. test ( ip ) ) return true ;
98+
99+ return false ;
100+ } ;
101+
102+ const isPrivateIp = ( url : string ) => isPrivateIp4 ( url ) || isPrivateIp6 ( url ) ;
103+
73104/**
74105 * Fetces the given `opticUrl` if allowed. The rules for which are allowed
75106 * should consider potentially malicious URLs such as `file://` or
76107 * internal/local IP addresses.
77108 */
78109export const fetchRemoteOptic = async ( opts : { opticUrl : string ; fetch ?: typeof fetch } ) => {
79110 if ( opts . opticUrl . startsWith ( 'file://' ) ) return void 0 ;
111+ if ( isPrivateIp ( opts . opticUrl ) ) return void 0 ;
112+
80113 const response = await ( opts . fetch ?? fetch ) ( opts . opticUrl ) ;
114+
115+ if ( isPrivateIp ( response . url ) ) return void 0 ;
116+
81117 return await response . text ( ) ;
82118} ;
0 commit comments