Skip to content
Myr

fix(context): use `isByteString` in `c.redirect` (#4307)

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

* fix(utils/url): use `isByteString` in `safeEncodeURI` * move logics to `context.ts` * remove the function not exists

authored by Yusuke Wada · committed by GitHub · parent c128f0d · Browse files · 4 files+21 added−49 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+21 added−49 removed

  • tests:+14 added−27 removed
  • other:+7 added−22 removed

4 files+21 added−49 removed

modified:src/utils/url.test.ts-27 removed
5 unmodified lines
6 getQueryParam,7 getQueryParams,8 getQueryStrings,9 mergePath,10 safeEncodeURI,10 splitPath,11 splitRoutingPath,12} from './url'13
288 unmodified lines
302 toString: [''],303 })304 })305 })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 })322323 it('Should be the same strings', async () => {324 expect(325 safeEncodeURI(326 'https://login.microsoftonline.com/redacted-id&scope=email%20openid%20profile%20offline_access&redirect_uri=https%3A%2F%2Fredacted-domain.org%2Fapi%2Fauth%2Fcallback&client-request-id=redacted-id&response_mode=query&client_info=1&x-client-SKU=msal.js.node&x-client-VER=3.6.4&x-client-OS=linux&x-client-CPU=x64&response_type=code&code_challenge=7C75MO8zdoJZn7_sIpxcFEQzPvzWIMzD2tUf-lqevR0&code_challenge_method=S256'327 )328 ).toBe(329 'https://login.microsoftonline.com/redacted-id&scope=email%20openid%20profile%20offline_access&redirect_uri=https%3A%2F%2Fredacted-domain.org%2Fapi%2Fauth%2Fcallback&client-request-id=redacted-id&response_mode=query&client_info=1&x-client-SKU=msal.js.node&x-client-VER=3.6.4&x-client-OS=linux&x-client-CPU=x64&response_type=code&code_challenge=7C75MO8zdoJZn7_sIpxcFEQzPvzWIMzD2tUf-lqevR0&code_challenge_method=S256'330 )331 })332 })306})
modified:src/utils/url.ts-20 removed
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 // Try to decode the string322 const decoded = decodeURI(str)323 // If successful, encode only if it's different from the original324 return decoded === str ? encodeURI(str) : str325 } catch (e) {326 if (e instanceof URIError) {327 // If decoding fails, the string is not encoded, so encode it328 return encodeURI(str)329 }330 throw e331 }332}
modified:src/context.test.ts+14 added
113 unmodified lines
114 'https://example.com/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'115 )116 })117118 const unchangedURLString = [119 'https://example.com/%hello', // invalid ASCII chars120 'https://example.com/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF?abc',121 'https://localhost/api?redirect_uri=https%3A%2F%2Fexample.com', // with ://122 'https://localhost/api?redirect_uri=https%3A%2F%2Fexample.com&scope=email%20profile', // with spaces and ://123 ]124125 unchangedURLString.forEach((urlString) => {126 it(`c.redirect() w/ ${urlString}`, () => {127 const res = c.redirect(urlString)128 expect(res.headers.get('Location')).toBe(urlString)129 })130 })131132 it('c.header()', async () => {133 c.header('X-Foo', 'Bar')134 const res = c.body('Hi')135 const foo = res.headers.get('X-Foo')
462 unmodified lines
modified:src/context.ts+7 added-2 removed
18 unmodified lines
19 JSONParsed,20 JSONValue,21 SimplifyDeepArray,22} from './utils/types'23import { safeEncodeURI } from './utils/url'2324type HeaderRecord =25 | Record<'Content-Type', BaseMime>26 | Record<ResponseHeader, string | string[]>
718 unmodified lines
745 redirect = <T extends RedirectStatusCode = 302>(746 location: string | URL,747 status?: T748 ): Response & TypedResponse<undefined, T, 'redirect'> => {750 this.header('Location', safeEncodeURI(String(location)))749 const locationString = String(location)750 this.header(751 'Location',752 // Multibyes should be encoded753 // eslint-disable-next-line no-control-regex754 !/[^\x00-\xFF]/.test(locationString) ? locationString : encodeURI(locationString)755 )756 return this.newResponse(null, status ?? 302) as any757 }758759 /**
16 unmodified lines