(scheme: string, host: string, incomingUrl: string)
| 12 | const reValidHost = /^[a-z0-9._-]+(?::(?:[1-5]\d{3,4}|[6-9]\d{3}))?$/ |
| 13 | |
| 14 | export const buildUrl = (scheme: string, host: string, incomingUrl: string) => { |
| 15 | const url = `${scheme}://${host}${incomingUrl}` |
| 16 | |
| 17 | if (!reValidHost.test(host)) { |
| 18 | const urlObj = new URL(url) |
| 19 | |
| 20 | // if suspicious, check by host. host header sometimes contains port. |
| 21 | if ( |
| 22 | urlObj.hostname.length !== host.length && |
| 23 | urlObj.hostname !== (host.includes(':') ? host.replace(/:\d+$/, '') : host).toLowerCase() |
| 24 | ) { |
| 25 | throw new RequestError('Invalid host header') |
| 26 | } |
| 27 | return urlObj.href |
| 28 | } else if (incomingUrl.length === 0) { |
| 29 | return url + '/' |
| 30 | } else { |
| 31 | if (incomingUrl.charCodeAt(0) !== 0x2f) { |
| 32 | // '/' |
| 33 | throw new RequestError('Invalid URL') |
| 34 | } |
| 35 | |
| 36 | if (!reValidRequestUrl.test(incomingUrl) || reDotSegment.test(incomingUrl)) { |
| 37 | return new URL(url).href |
| 38 | } |
| 39 | |
| 40 | return url |
| 41 | } |
| 42 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…