Skip to content

Commit a152286

Browse files
authored
fix: prevent IPs from being used as long URL (#2418)
* fix: prevent IPs from being used as long URL * chore: add test for ip addresses * chore: add try/catch loop we do this so that if there's a drift in validation we can still return a result * chore: npm i
1 parent f88ff0e commit a152286

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/shared/util/validation.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,19 @@ export function isHttps(url: string, useWhitelist = false): boolean {
4545
// Check if a URL string is valid.
4646
export function isValidUrl(url: string, useWhitelist = false): boolean {
4747
if (useWhitelist && isWhitelisted(url)) return true
48-
return validator.isURL(url, URL_OPTS)
48+
if (!validator.isURL(url, URL_OPTS)) return false
49+
50+
// NOTE: Reject URLs with IP addresses to prevent the use of internal or private IPs
51+
// as long URLs. This is a security measure to mitigate risks such as
52+
// Server-Side Request Forgery (SSRF) and unauthorized access to internal resources.
53+
try {
54+
// NOTE: try/catch to avoid drifts in validation
55+
// between validator library and `URL` constructor
56+
const host = new URL(url).hostname
57+
return !validator.isIP(host)
58+
} catch {
59+
return false
60+
}
4961
}
5062

5163
// Tests if a short link consists of alphanumeric and hyphen characters.

test/shared/util/validation.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ describe('Test valid url check', () => {
7777
const url = 'http://localhost:4566/local-bucket/file1.pdf'
7878
expect(validation.isValidUrl(url, true)).toBe(true)
7979
})
80+
81+
test('IP addresses fails check', () => {
82+
const url = 'https://8.8.8.8:4566/local-bucket/file1.pdf'
83+
expect(validation.isValidUrl(url)).toBe(false)
84+
})
8085
})
8186

8287
describe('Test short url check', () => {

0 commit comments

Comments
 (0)