Skip to content

Commit

Permalink
Merge 9280098 into 2b99cdb
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Sep 22, 2021
2 parents 2b99cdb + 9280098 commit 82a46d6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ function stripHttp1ConnectionHeaders (headers) {

// issue ref: https://github.com/fastify/fast-proxy/issues/42
function buildURL (source, reqBase) {
let baseOrigin = reqBase ? new URL(reqBase).href : undefined
const dest = new URL(source, reqBase)

// if base is specified, source url should not override it
if (reqBase) {
if (!reqBase.endsWith('/') && dest.href.length > reqBase.length) {
reqBase = reqBase + '/'
if (baseOrigin) {
if (!baseOrigin.endsWith('/') && dest.href.length > baseOrigin.length) {
baseOrigin = baseOrigin + '/'
}

if (!dest.href.startsWith(reqBase)) {
if (!dest.href.startsWith(baseOrigin)) {
throw new Error('source must be a relative path string')
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/build-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ test('should handle lack of trailing slash in base', (t) => {
t.equal(url.href, 'http://localhost/hi/more')
})

test('should handle default port in base', (t) => {
t.plan(2)
let url = buildURL('/hi', 'http://localhost:80/hi')
t.equal(url.href, 'http://localhost/hi')

url = buildURL('/hi', 'https://localhost:443/hi')
t.equal(url.href, 'https://localhost/hi')
})

const errorInputs = [
{ source: '//10.0.0.10/hi', base: 'http://localhost' },
{ source: 'http://10.0.0.10/hi', base: 'http://localhost' },
Expand Down

0 comments on commit 82a46d6

Please sign in to comment.