Skip to content
Myr

fix(context): encode the redirect location (#4297)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(context): encode the redirect location * Moved safeEncodeURI

authored by yayugu · committed by GitHub · parent 89f4c96 · Browse files · 4 files+44 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+44 added−1 removed

  • tests:+24 added−0 removed
  • other:+20 added−1 removed

4 files+44 added−1 removed

modified:src/utils/url.test.ts+17 added
5 unmodified lines
6 getQueryParam,7 getQueryParams,8 getQueryStrings,9 mergePath,10 safeEncodeURI,11 splitPath,12 splitRoutingPath,13} from './url'14
288 unmodified lines
303 toString: [''],304 })305 })306 })307308 describe('safeEncodeURI', () => {309 it('Encode multibytes', async () => {310 expect(safeEncodeURI('こんにちは')).toBe('%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF')311 })312313 it('Encode unencoded, but URI invalid ASCII chars', async () => {314 expect(safeEncodeURI('https://example.com/%hello')).toBe('https://example.com/%25hello')315 })316317 it('Do not change the encoded string', async () => {318 expect(319 safeEncodeURI('https://example.com/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF?abc')320 ).toBe('https://example.com/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF?abc')321 })322 })323})
modified:src/utils/url.ts+18 added
308 unmodified lines
309310// `decodeURIComponent` is a long name.311// By making it a function, we can use it commonly when minified, reducing the amount of code.312export const decodeURIComponent_ = decodeURIComponent313314/**315 * This function ensures that the string is safely encoded,316 * and not double-encoded.317 * The input string may or may not escaped by encodeURI.318 */319export const safeEncodeURI = (str: string): string => {320 try {321 // decodeURI will throw if str contains %322 // In that case, we can assume that str is not encoded323 return encodeURI(decodeURI(str))324 } catch (e) {325 if (e instanceof URIError) {326 return encodeURI(str)327 }328 throw e329 }330}
modified:src/context.test.ts+7 added
106 unmodified lines
107 expect(res.status).toBe(302)108 expect(res.headers.get('Location')).toBe('https://example.com/destination')109 })110111 it('c.redirect() w/ multibytes', async () => {112 const res = c.redirect('https://example.com/こんにちは')113 expect(res.headers.get('Location')).toBe(114 'https://example.com/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'115 )116 })117118 it('c.header()', async () => {119 c.header('X-Foo', 'Bar')120 const res = c.body('Hi')121 const foo = res.headers.get('X-Foo')
462 unmodified lines
modified:src/context.ts+2 added-1 removed
18 unmodified lines
19 JSONParsed,20 JSONValue,21 SimplifyDeepArray,22} from './utils/types'23import { safeEncodeURI } from './utils/url'2425type HeaderRecord =26 | Record<'Content-Type', BaseMime>27 | Record<ResponseHeader, string | string[]>
718 unmodified lines
746 redirect = <T extends RedirectStatusCode = 302>(747 location: string | URL,748 status?: T749 ): Response & TypedResponse<undefined, T, 'redirect'> => {749 this.header('Location', String(location))750 this.header('Location', safeEncodeURI(String(location)))751 return this.newResponse(null, status ?? 302) as any752 }753754 /**
16 unmodified lines