Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ const fetchWithRetry = async (url, options, retries = 0) => {
}
}

const isDisallowedExternalUrl = (url) => {
try {
const { protocol, hostname } = new URL(url)

if (!/^https?:$/.test(protocol)) {
return true
}

return /^(?:localhost|0\.|10\.|127\.|169\.254\.|172\.(?:1[6-9]|2\d|3[01])\.|192\.168\.|\[?::1\]?|\[?fd[0-9a-f]{2}:)/i.test(hostname)
} catch {
return true
}
}

class Api {
static _sendVerbatim = async (path, verb, optionsRaw) => {
const { getAgentForUrl, ...options } = optionsRaw || {}
Expand All @@ -52,6 +66,10 @@ class Api {
throw new errors.ConfigurationError('Base URL has not been set - use Files.setBaseUrl() to set it')
}

if (isExternal && isDisallowedExternalUrl(path)) {
throw new errors.ConfigurationError('Refusing to send request to a disallowed URL')
}

const url = isExternal
? path
: `${baseUrl}${Files.getEndpointPrefix()}${path}`
Expand Down