11import axios from 'axios'
22import { JSDOM } from 'jsdom'
3- import * as dns from 'dns/promises'
3+ import * as dns from 'dns'
4+ import * as http from 'http'
5+ import * as https from 'https'
46import { Readability , isProbablyReaderable } from '@mozilla/readability'
57import TurndownServiceJoplin from '@joplin/turndown'
68import * as turndownPluginGfm from '@joplin/turndown-plugin-gfm'
@@ -87,19 +89,79 @@ const create_turndown_service = () => {
8789}
8890
8991const is_private_ip = ( ip : string ) : boolean => {
90- if ( / ^ 1 2 7 \. / . test ( ip ) ) return true
91- if ( / ^ 1 0 \. / . test ( ip ) ) return true
92- if ( / ^ 1 7 2 \. ( 1 [ 6 - 9 ] | 2 [ 0 - 9 ] | 3 [ 0 - 1 ] ) \. / . test ( ip ) ) return true
93- if ( / ^ 1 9 2 \. 1 6 8 \. / . test ( ip ) ) return true
94- if ( / ^ 1 6 9 \. 2 5 4 \. / . test ( ip ) ) return true
95- if ( / ^ 0 \. / . test ( ip ) ) return true
96- if ( / ^ 2 5 5 \. 2 5 5 \. 2 5 5 \. 2 5 5 $ / . test ( ip ) ) return true
97- if ( ip === '::1' || ip === '::' ) return true
98- if ( / ^ f e 8 0 : / i. test ( ip ) ) return true
99- if ( / ^ f c [ 0 - 9 a - f ] { 2 } : / i. test ( ip ) || / ^ f d [ 0 - 9 a - f ] { 2 } : / i. test ( ip ) ) return true
92+ let normalized_ip = ip . toLowerCase ( )
93+ if ( normalized_ip . startsWith ( '::ffff:' ) ) {
94+ normalized_ip = normalized_ip . replace ( / ^ : : f f f f : / , '' )
95+ }
96+
97+ if ( / ^ 1 2 7 \. / . test ( normalized_ip ) ) return true
98+ if ( / ^ 1 0 \. / . test ( normalized_ip ) ) return true
99+ if ( / ^ 1 7 2 \. ( 1 [ 6 - 9 ] | 2 [ 0 - 9 ] | 3 [ 0 - 1 ] ) \. / . test ( normalized_ip ) ) return true
100+ if ( / ^ 1 9 2 \. 1 6 8 \. / . test ( normalized_ip ) ) return true
101+ if ( / ^ 1 6 9 \. 2 5 4 \. / . test ( normalized_ip ) ) return true
102+ if ( / ^ 0 \. / . test ( normalized_ip ) ) return true
103+ if ( / ^ 2 5 5 \. 2 5 5 \. 2 5 5 \. 2 5 5 $ / . test ( normalized_ip ) ) return true
104+ if ( normalized_ip === '::1' || normalized_ip === '::' ) return true
105+ if ( / ^ f e 8 0 : / . test ( normalized_ip ) ) return true
106+ if ( / ^ f c [ 0 - 9 a - f ] { 2 } : / . test ( normalized_ip ) || / ^ f d [ 0 - 9 a - f ] { 2 } : / . test ( normalized_ip ) ) return true
100107 return false
101108}
102109
110+ const custom_lookup = (
111+ hostname : string ,
112+ options :
113+ | dns . LookupOneOptions
114+ | ( (
115+ err : NodeJS . ErrnoException | null ,
116+ address : string ,
117+ family : number
118+ ) => void ) ,
119+ callback ?: (
120+ err : NodeJS . ErrnoException | null ,
121+ address : string ,
122+ family : number
123+ ) => void
124+ ) => {
125+ const cb = ( typeof options === 'function' ? options : callback ) as (
126+ err : NodeJS . ErrnoException | null ,
127+ address : string ,
128+ family : number
129+ ) => void
130+ const opts = typeof options === 'function' ? { } : options
131+
132+ dns . lookup ( hostname , opts , ( err , address , family ) => {
133+ if ( err ) return cb ( err , address , family )
134+
135+ if ( typeof address === 'string' && is_private_ip ( address ) ) {
136+ return cb (
137+ new Error ( `SSRF blocked: private IP address ${ address } ` ) ,
138+ address ,
139+ family
140+ )
141+ }
142+
143+ if ( Array . isArray ( address ) ) {
144+ for ( const record of address ) {
145+ if (
146+ typeof record . address === 'string' &&
147+ is_private_ip ( record . address )
148+ ) {
149+ return cb (
150+ new Error ( `SSRF blocked: private IP address ${ record . address } ` ) ,
151+ address as any ,
152+ family
153+ )
154+ }
155+ }
156+ }
157+
158+ cb ( null , address as any , family )
159+ } )
160+ }
161+
162+ const http_agent = new http . Agent ( { lookup : custom_lookup as any } )
163+ const https_agent = new https . Agent ( { lookup : custom_lookup as any } )
164+
103165export const fetch_website = async (
104166 url : string ,
105167 abort_signal ?: AbortSignal
@@ -110,18 +172,15 @@ export const fetch_website = async (
110172 return null
111173 }
112174
113- const { address } = await dns . lookup ( parsed_url . hostname )
114- if ( is_private_ip ( address ) ) {
115- return null
116- }
117-
118175 const response = await axios . get ( url , {
119176 headers : {
120177 'User-Agent' :
121178 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36'
122179 } ,
123180 timeout : 5000 ,
124- signal : abort_signal
181+ signal : abort_signal ,
182+ httpAgent : http_agent ,
183+ httpsAgent : https_agent
125184 } )
126185
127186 if ( response . status === 200 && typeof response . data === 'string' ) {
0 commit comments