Skip to content

Commit 06858c2

Browse files
committed
fix: should not replace literal + with in the query string (#943)
1 parent 6bba36d commit 06858c2

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

src/compose.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ export const composeHandler = ({
711711
} else {
712712
fnLiteral +=
713713
'if(c.qi!==-1){' +
714-
`let url = '&' + decodeURIComponent(c.url.slice(c.qi + 1))\n`
714+
`let url = '&' + decodeURIComponent(c.url.slice(c.qi + 1).replaceAll('+', ' '))\n`
715715

716716
let index = 0
717717
for (const {
@@ -739,8 +739,8 @@ export const composeHandler = ({
739739
`else\n` +
740740
`a${index}+=','\n` +
741741
`let temp\n` +
742-
`if(memory===-1)temp=decodeURIComponent(url.slice(start).replace(/\\+|%20/g,' '))\n` +
743-
`else temp=decodeURIComponent(url.slice(start, memory).replace(/\\+|%20/g,' '))\n` +
742+
`if(memory===-1)temp=decodeURIComponent(url.slice(start))\n` +
743+
`else temp=decodeURIComponent(url.slice(start, memory))\n` +
744744
`const charCode = temp.charCodeAt(0)\n` +
745745
`if(charCode !== 91 && charCode !== 123)\n` +
746746
`temp='"'+temp+'"'\n` +
@@ -763,10 +763,10 @@ export const composeHandler = ({
763763
`if(a${index}===undefined)` +
764764
`a${index}=[]\n` +
765765
`if(memory===-1){` +
766-
`a${index}.push(decodeURIComponent(url.slice(start)).replace(/\\+|%20/g,' '))\n` +
766+
`a${index}.push(decodeURIComponent(url.slice(start)))\n` +
767767
`break` +
768768
`}` +
769-
`else a${index}.push(decodeURIComponent(url.slice(start, memory)).replace(/\\+|%20/g,' '))\n` +
769+
`else a${index}.push(decodeURIComponent(url.slice(start, memory)))\n` +
770770
`memory=url.indexOf('&${key}=',memory)\n` +
771771
`if(memory===-1) break\n` +
772772
`}`
@@ -776,8 +776,8 @@ export const composeHandler = ({
776776
`if(memory!==-1){` +
777777
`const start=memory+${key.length + 2}\n` +
778778
`memory=url.indexOf('&',start)\n` +
779-
`if(memory===-1)a${index}=decodeURIComponent(url.slice(start).replace(/\\+|%20/g,' '))` +
780-
`else a${index}=decodeURIComponent(url.slice(start,memory).replace(/\\+|%20/g,' '))` +
779+
`if(memory===-1)a${index}=decodeURIComponent(url.slice(start))` +
780+
`else a${index}=decodeURIComponent(url.slice(start,memory))` +
781781
`if(a${index}!==undefined)` +
782782
`try{` +
783783
`a${index}=JSON.parse(a${index})` +
@@ -790,9 +790,9 @@ export const composeHandler = ({
790790
`if(memory!==-1){` +
791791
`const start=memory+${key.length + 2}\n` +
792792
`memory=url.indexOf('&',start)\n` +
793-
`if(memory===-1)a${index}=decodeURIComponent(url.slice(start).replace(/\\+|%20/g,' '))\n` +
793+
`if(memory===-1)a${index}=decodeURIComponent(url.slice(start))\n` +
794794
`else{` +
795-
`a${index}=decodeURIComponent(url.slice(start,memory).replace(/\\+|%20/g,' '))`
795+
`a${index}=decodeURIComponent(url.slice(start,memory))`
796796

797797
if (anyOf)
798798
fnLiteral +=
@@ -805,8 +805,8 @@ export const composeHandler = ({
805805
`if(first)first=false\n` +
806806
`else deepMemory = url.indexOf('&', start)\n` +
807807
`let value\n` +
808-
`if(deepMemory===-1)value=decodeURIComponent(url.slice(start).replace(/\\+|%20/g,' '))\n` +
809-
`else value=decodeURIComponent(url.slice(start, deepMemory).replace(/\\+|%20/g,' '))\n` +
808+
`if(deepMemory===-1)value=decodeURIComponent(url.slice(start))\n` +
809+
`else value=decodeURIComponent(url.slice(start, deepMemory))\n` +
810810
`const vStart=value.charCodeAt(0)\n` +
811811
`const vEnd=value.charCodeAt(value.length - 1)\n` +
812812
`if((vStart===91&&vEnd===93)||(vStart===123&&vEnd===125))\n` +

test/validator/query.test.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -703,23 +703,43 @@ describe('Query Validator', () => {
703703
})
704704
})
705705

706-
it('parse + in query', async () => {
706+
it('parse query with space', async () => {
707707
const api = new Elysia().get('', ({ query }) => query, {
708708
query: t.Object({
709709
keyword: t.String()
710710
})
711711
})
712712

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

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

721721
expect(result).toEqual({
722-
keyword: 'hello world'
722+
keyword: 'with space'
723+
})
724+
})
725+
726+
it('parse query with +', async () => {
727+
const api = new Elysia().get('', ({ query }) => query, {
728+
query: t.Object({
729+
keyword: t.String()
730+
})
731+
})
732+
733+
const url = new URL("http://localhost:3000/");
734+
url.searchParams.append("keyword", "with+plus");
735+
console.log(url.href) // http://localhost:3000/?keyword=with%2Bplus
736+
737+
const result = await api
738+
.handle(new Request(url.href))
739+
.then((response) => response.json())
740+
741+
expect(result).toEqual({
742+
keyword: 'with+plus'
723743
})
724744
})
725745

0 commit comments

Comments
 (0)