Skip to content

Commit b1b0af4

Browse files
committed
Implement custom HTTP and HTTPS agents with SSRF IP filtering for website fetcher
└── apps └── editor └── src └── views └── prompt ├── backend │ ├── message-handlers │ │ ├── handle-paste-url.ts │ │ └── index.ts │ ├── prompt-view-provider.ts │ └── utils │ └── website-fetcher.ts ├── frontend │ └── hooks │ └── use-prompt │ └── use-prompt.ts └── types └── messages.ts
1 parent c4d4371 commit b1b0af4

1 file changed

Lines changed: 76 additions & 17 deletions

File tree

‎apps/editor/src/views/prompt/backend/utils/website-fetcher.ts‎

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import axios from 'axios'
22
import { 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'
46
import { Readability, isProbablyReaderable } from '@mozilla/readability'
57
import TurndownServiceJoplin from '@joplin/turndown'
68
import * as turndownPluginGfm from '@joplin/turndown-plugin-gfm'
@@ -87,19 +89,79 @@ const create_turndown_service = () => {
8789
}
8890

8991
const is_private_ip = (ip: string): boolean => {
90-
if (/^127\./.test(ip)) return true
91-
if (/^10\./.test(ip)) return true
92-
if (/^172\.(1[6-9]|2[0-9]|3[0-1])\./.test(ip)) return true
93-
if (/^192\.168\./.test(ip)) return true
94-
if (/^169\.254\./.test(ip)) return true
95-
if (/^0\./.test(ip)) return true
96-
if (/^255\.255\.255\.255$/.test(ip)) return true
97-
if (ip === '::1' || ip === '::') return true
98-
if (/^fe80:/i.test(ip)) return true
99-
if (/^fc[0-9a-f]{2}:/i.test(ip) || /^fd[0-9a-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(/^::ffff:/, '')
95+
}
96+
97+
if (/^127\./.test(normalized_ip)) return true
98+
if (/^10\./.test(normalized_ip)) return true
99+
if (/^172\.(1[6-9]|2[0-9]|3[0-1])\./.test(normalized_ip)) return true
100+
if (/^192\.168\./.test(normalized_ip)) return true
101+
if (/^169\.254\./.test(normalized_ip)) return true
102+
if (/^0\./.test(normalized_ip)) return true
103+
if (/^255\.255\.255\.255$/.test(normalized_ip)) return true
104+
if (normalized_ip === '::1' || normalized_ip === '::') return true
105+
if (/^fe80:/.test(normalized_ip)) return true
106+
if (/^fc[0-9a-f]{2}:/.test(normalized_ip) || /^fd[0-9a-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+
103165
export 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

Comments
 (0)