Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(request): use URL instead of path/resolve and add a test #125

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
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
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')
})
})