Skip to content

Commit

Permalink
fix(request): use URL instead of path/resolve and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe committed Jan 21, 2024
1 parent 69dd54a commit 5f00074
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
10 changes: 2 additions & 8 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import type { IncomingMessage } from 'node:http'
import type { Http2ServerRequest } from 'node:http2'
import { resolve } from 'node:path'
import { Readable } from 'node:stream'

const newRequestFromIncoming = (
Expand Down Expand Up @@ -42,13 +41,8 @@ const requestPrototype: Record<string | symbol, any> = {
},

get url() {
let path = this[incomingKey]['path']
if (!path) {
const originalPath = this[incomingKey].url
path = /\.\./.test(originalPath) ? resolve(originalPath) : originalPath
this[incomingKey]['path'] = path
}
return `http://${this[incomingKey].headers.host}${path}`
const url = `http://${this[incomingKey].headers.host}${this[incomingKey].url}`
return /\.\./.test(url) ? new URL(url).href : url
},

[getRequestCache]() {
Expand Down
11 changes: 10 additions & 1 deletion test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ describe('Request', () => {
} as IncomingMessage)
expect(req).toBeInstanceOf(global.Request)
expect(req.url).toBe('http://localhost/foo.txt')
// Check if cached value is returned correctly
})

it('Should resolve double dots in host header', async () => {
const req = newRequest({
headers: {
host: 'localhost/..',
},
url: '/foo.txt',
} as IncomingMessage)
expect(req).toBeInstanceOf(global.Request)
expect(req.url).toBe('http://localhost/foo.txt')
})
})

0 comments on commit 5f00074

Please sign in to comment.