Skip to content

Commit

Permalink
fix: should not replace literal + with in the query string (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
macabeus committed Dec 23, 2024
1 parent 6bba36d commit 06858c2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
22 changes: 11 additions & 11 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,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).replaceAll('+', ' '))\n`

let index = 0
for (const {
Expand Down Expand Up @@ -739,8 +739,8 @@ export const composeHandler = ({
`else\n` +
`a${index}+=','\n` +
`let temp\n` +
`if(memory===-1)temp=decodeURIComponent(url.slice(start).replace(/\\+|%20/g,' '))\n` +
`else temp=decodeURIComponent(url.slice(start, memory).replace(/\\+|%20/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` +
Expand All @@ -763,10 +763,10 @@ export const composeHandler = ({
`if(a${index}===undefined)` +
`a${index}=[]\n` +
`if(memory===-1){` +
`a${index}.push(decodeURIComponent(url.slice(start)).replace(/\\+|%20/g,' '))\n` +
`a${index}.push(decodeURIComponent(url.slice(start)))\n` +
`break` +
`}` +
`else a${index}.push(decodeURIComponent(url.slice(start, memory)).replace(/\\+|%20/g,' '))\n` +
`else a${index}.push(decodeURIComponent(url.slice(start, memory)))\n` +
`memory=url.indexOf('&${key}=',memory)\n` +
`if(memory===-1) break\n` +
`}`
Expand All @@ -776,8 +776,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(/\\+|%20/g,' '))` +
`else a${index}=decodeURIComponent(url.slice(start,memory).replace(/\\+|%20/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})` +
Expand All @@ -790,9 +790,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(/\\+|%20/g,' '))\n` +
`if(memory===-1)a${index}=decodeURIComponent(url.slice(start))\n` +
`else{` +
`a${index}=decodeURIComponent(url.slice(start,memory).replace(/\\+|%20/g,' '))`
`a${index}=decodeURIComponent(url.slice(start,memory))`

if (anyOf)
fnLiteral +=
Expand All @@ -805,8 +805,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(/\\+|%20/g,' '))\n` +
`else value=decodeURIComponent(url.slice(start, deepMemory).replace(/\\+|%20/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` +
Expand Down
28 changes: 24 additions & 4 deletions test/validator/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,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'
})
})

Expand Down

0 comments on commit 06858c2

Please sign in to comment.