Skip to content
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

fix: handle uppercase encoding #32

Merged
merged 3 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
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
26 changes: 15 additions & 11 deletions src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ const EQUAL_RE = /=/g // %3D
const IM_RE = /\?/g // %3F
const PLUS_RE = /\+/g // %2B

const ENC_BRACKET_OPEN_RE = /%5B/g // [
const ENC_BRACKET_CLOSE_RE = /%5D/g // ]
const ENC_CARET_RE = /%5E/g // ^
const ENC_BACKTICK_RE = /%60/g // `
const ENC_CURLY_OPEN_RE = /%7B/g // {
const ENC_PIPE_RE = /%7C/g // |
const ENC_CURLY_CLOSE_RE = /%7D/g // }
const ENC_SPACE_RE = /%20/g
const ENC_SLASH_RE = /%2F/g
const ENC_ENC_SLASH_RE = /%252F/g
const ENC_BRACKET_OPEN_RE = /%5B/gi // [
const ENC_BRACKET_CLOSE_RE = /%5D/gi // ]
const ENC_CARET_RE = /%5E/gi // ^
const ENC_BACKTICK_RE = /%60/gi // `
const ENC_CURLY_OPEN_RE = /%7B/gi // {
const ENC_PIPE_RE = /%7C/gi // |
const ENC_CURLY_CLOSE_RE = /%7D/gi // }
const ENC_SPACE_RE = /%20/gi
const ENC_SLASH_RE = /%2F/gi
const ENC_ENC_SLASH_RE = /%252F/gi

/**
* Encode characters that need to be encoded on the path, search and hash
Expand Down Expand Up @@ -87,7 +87,11 @@ export function encodeQueryKey (text: string | number): string {
* @returns encoded string
*/
export function encodePath (text: string | number): string {
return encode(text).replace(HASH_RE, '%23').replace(IM_RE, '%3F').replace(ENC_ENC_SLASH_RE, '%2F')
return encode(text)
.replace(HASH_RE, '%23')
.replace(IM_RE, '%3F')
.replace(ENC_ENC_SLASH_RE, '%2F')
.replace(AMPERSAND_RE, '%26')
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ describe('normalizeURL', () => {
'http://localhost/?redirect=http://google.com?q=test': 'http://localhost/?redirect=http://google.com?q=test',
'http://localhost/[email protected]': 'http://localhost/[email protected]',
'http://localhost/?email=some%2Bv1%40email.com': 'http://localhost/?email=some%[email protected]',
'http://localhost/abc/deg%2F%[email protected]': 'http://localhost/abc/deg%2F%[email protected]'
'http://localhost/abc/deg%2F%[email protected]': 'http://localhost/abc/deg%2F%[email protected]',
'http://localhost/abc/deg%2f%3f%[email protected]&foo=bar': 'http://localhost/abc/deg%2F%3F%[email protected]&foo=bar'
}

const validURLS = [
Expand Down