Skip to content

fix: should not replace literal + with a space in the query string #960

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
38 changes: 18 additions & 20 deletions src/compose.ts
Original file line number Diff line number Diff line change
@@ -678,15 +678,13 @@ export const composeHandler = ({
}

if (hasQuery) {
const destructured = <
{
key: string
isArray: boolean
isNestedObjectArray: boolean
isObject: boolean
anyOf: boolean
}[]
>[]
const destructured = [] as {
key: string
isArray: boolean
isNestedObjectArray: boolean
isObject: boolean
anyOf: boolean
}[]

// @ts-ignore
if (validator.query && validator.query.schema.type === 'object') {
@@ -748,7 +746,7 @@ export const composeHandler = ({
} else {
fnLiteral +=
'if(c.qi!==-1){' +
`let url = '&' + decodeURIComponent(c.url.slice(c.qi + 1))\n`
`let url = '&' + decodeURIComponent(c.url.slice(c.qi + 1).replace(/\\+/g,' '))\n`

let index = 0
for (const {
@@ -776,8 +774,8 @@ export const composeHandler = ({
`else\n` +
`a${index}+=','\n` +
`let temp\n` +
`if(memory===-1)temp=decodeURIComponent(url.slice(start).replace(/\\+/g,' '))\n` +
`else temp=decodeURIComponent(url.slice(start, memory).replace(/\\+/g,' '))\n` +
`if(memory===-1)temp=decodeURIComponent(url.slice(start))\n` +
`else temp=decodeURIComponent(url.slice(start, memory))\n` +
`const charCode = temp.charCodeAt(0)\n` +
`if(charCode !== 91 && charCode !== 123)\n` +
`temp='"'+temp+'"'\n` +
@@ -800,10 +798,10 @@ export const composeHandler = ({
`if(a${index}===undefined)` +
`a${index}=[]\n` +
`if(memory===-1){` +
`a${index}.push(decodeURIComponent(url.slice(start)).replace(/\\+/g,' '))\n` +
`a${index}.push(decodeURIComponent(url.slice(start)))\n` +
`break` +
`}` +
`else a${index}.push(decodeURIComponent(url.slice(start, memory)).replace(/\\+/g,' '))\n` +
`else a${index}.push(decodeURIComponent(url.slice(start, memory)))\n` +
`memory=url.indexOf('&${key}=',memory)\n` +
`if(memory===-1) break\n` +
`}`
@@ -813,8 +811,8 @@ export const composeHandler = ({
`if(memory!==-1){` +
`const start=memory+${key.length + 2}\n` +
`memory=url.indexOf('&',start)\n` +
`if(memory===-1)a${index}=decodeURIComponent(url.slice(start).replace(/\\+/g,' '))` +
`else a${index}=decodeURIComponent(url.slice(start,memory).replace(/\\+/g,' '))` +
`if(memory===-1)a${index}=decodeURIComponent(url.slice(start))` +
`else a${index}=decodeURIComponent(url.slice(start,memory))` +
`if(a${index}!==undefined)` +
`try{` +
`a${index}=JSON.parse(a${index})` +
@@ -827,9 +825,9 @@ export const composeHandler = ({
`if(memory!==-1){` +
`const start=memory+${key.length + 2}\n` +
`memory=url.indexOf('&',start)\n` +
`if(memory===-1)a${index}=decodeURIComponent(url.slice(start).replace(/\\+/g,' '))\n` +
`if(memory===-1)a${index}=decodeURIComponent(url.slice(start))\n` +
`else{` +
`a${index}=decodeURIComponent(url.slice(start,memory).replace(/\\+/g,' '))`
`a${index}=decodeURIComponent(url.slice(start,memory))`

if (anyOf)
fnLiteral +=
@@ -842,8 +840,8 @@ export const composeHandler = ({
`if(first)first=false\n` +
`else deepMemory = url.indexOf('&', start)\n` +
`let value\n` +
`if(deepMemory===-1)value=decodeURIComponent(url.slice(start).replace(/\\+/g,' '))\n` +
`else value=decodeURIComponent(url.slice(start, deepMemory).replace(/\\+/g,' '))\n` +
`if(deepMemory===-1)value=decodeURIComponent(url.slice(start))\n` +
`else value=decodeURIComponent(url.slice(start, deepMemory))\n` +
`const vStart=value.charCodeAt(0)\n` +
`const vEnd=value.charCodeAt(value.length - 1)\n` +
`if((vStart===91&&vEnd===93)||(vStart===123&&vEnd===125))\n` +
29 changes: 24 additions & 5 deletions test/validator/query.test.ts
Original file line number Diff line number Diff line change
@@ -332,7 +332,6 @@ describe('Query Validator', () => {
check() {
const { state } = ctx.query

// @ts-expect-error
if (!checker.check(ctx, name, state ?? ctx.query.state))
throw new Error('State mismatch')
}
@@ -704,23 +703,43 @@ describe('Query Validator', () => {
})
})

it('parse + in query', async () => {
it('parse query with space', async () => {
const api = new Elysia().get('', ({ query }) => query, {
query: t.Object({
keyword: t.String()
})
})

const url = new URL('http://localhost:3000/')
url.searchParams.append('keyword', 'hello world')
console.log(url.href) //http://localhost:3000/?keyword=hello+world
url.searchParams.append('keyword', 'with space')
console.log(url.href) // http://localhost:3000/?keyword=with+space

const result = await api
.handle(new Request(url.href))
.then((response) => response.json())

expect(result).toEqual({
keyword: 'hello world'
keyword: 'with space'
})
})

it('parse query with +', async () => {
const api = new Elysia().get('', ({ query }) => query, {
query: t.Object({
keyword: t.String()
})
})

const url = new URL("http://localhost:3000/");
url.searchParams.append("keyword", "with+plus");
console.log(url.href) // http://localhost:3000/?keyword=with%2Bplus

const result = await api
.handle(new Request(url.href))
.then((response) => response.json())

expect(result).toEqual({
keyword: 'with+plus'
})
})

Loading
Oops, something went wrong.