Skip to content

Commit

Permalink
fix proxying with double slash (#319)
Browse files Browse the repository at this point in the history
* fix proxying with double slash fastify/fastify-http-proxy#307

* trigger ci

* trigger ci

* Update lib/utils.js

* used index instead of startsWith

* Update utils.js

---------

Co-authored-by: Uzlopak <[email protected]>
  • Loading branch information
rluvaton and Uzlopak authored Jun 12, 2023
1 parent affa6e9 commit d1ae303
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ 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

// To make sure we don't accidentally override the base path
if (baseOrigin && source.length > 1 && source[0] === '/' && source[1] === '/') {
source = '.' + source
}

const dest = new URL(source, reqBase)

// if base is specified, source url should not override it
Expand Down
11 changes: 9 additions & 2 deletions test/build-url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@ test('should handle default port in base', (t) => {
t.equal(url.href, 'https://localhost/hi')
})

test('should append instead of override base', (t) => {
t.plan(2)
let url = buildURL('//10.0.0.10/hi', 'http://localhost')
t.equal(url.href, 'http://localhost//10.0.0.10/hi')

url = buildURL('//httpbin.org/hi', 'http://localhost')
t.equal(url.href, 'http://localhost//httpbin.org/hi')
})

const errorInputs = [
{ source: '//10.0.0.10/hi', base: 'http://localhost' },
{ source: 'http://10.0.0.10/hi', base: 'http://localhost' },
{ source: 'https://10.0.0.10/hi', base: 'http://localhost' },
{ source: 'blah://10.0.0.10/hi', base: 'http://localhost' },
{ source: '//httpbin.org/hi', base: 'http://localhost' },
{ source: 'urn:foo:bar', base: 'http://localhost' },
{ source: 'http://localhost/private', base: 'http://localhost/exposed/' },
{ source: 'http://localhost/exposed-extra', base: 'http://localhost/exposed' },
Expand Down

0 comments on commit d1ae303

Please sign in to comment.