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

Relax parseOrigin validation to allow paths withe "/" #4000

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function parseURL (url) {
function parseOrigin (url) {
url = parseURL(url)

if (url.pathname !== '/' || url.search || url.hash) {
if (url.search || url.hash) {
throw new InvalidArgumentError('invalid url')
}

Expand Down
34 changes: 34 additions & 0 deletions test/issue-3999.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const { parseOrigin } = require('../lib/core/util')

// Test cases for parseOrigin functionality
test('parseOrigin allows URLs with paths and rejects invalid ones', async (t) => {
// Valid URL with path
const validUrl = 'https://api.domain.com/elastic'
const result = parseOrigin(validUrl)
assert.strictEqual(result.href, validUrl, 'Should return the input URL with the path for valid cases')

// Invalid URL with query string
const invalidQueryUrl = 'https://api.domain.com/elastic?query=test'
assert.throws(
() => parseOrigin(invalidQueryUrl),
/InvalidArgumentError/,
'Should throw InvalidArgumentError for URLs with query strings'
)

// Invalid URL with fragment
const invalidFragmentUrl = 'https://api.domain.com/elastic#section'
assert.throws(
() => parseOrigin(invalidFragmentUrl),
/InvalidArgumentError/,
'Should throw InvalidArgumentError for URLs with fragments'
)

// Valid URL without path
const validSimpleUrl = 'https://api.domain.com'
const simpleResult = parseOrigin(validSimpleUrl)
assert.strictEqual(simpleResult.href, `${validSimpleUrl}/`, 'Should return the input URL with the trailing slash')
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it impact undici.request() and other methods? Can you add a test for that?