-
Notifications
You must be signed in to change notification settings - Fork 240
Escape quotes in headers correctly in all languages #289
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b4d7bda
Add string escaping helper method
pimterry e659f4c
Escape quotes appropriately in headers values in all snippets
pimterry 03bee2f
Fix tiny Python snippet indentation issue
pimterry ac2a9b8
Add documentation to escapeFor{Single,Double}Quotes helpers
pimterry 1f518ca
Avoid reassigning 'value' in escapeString helper
pimterry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { escapeString } from './escape'; | ||
|
|
||
| describe('Escape methods', () => { | ||
| describe('escapeString', () => { | ||
| it('does nothing to a safe string', () => { | ||
| expect( | ||
| escapeString("hello world") | ||
| ).toBe("hello world"); | ||
| }); | ||
|
|
||
| it('escapes double quotes by default', () => { | ||
| expect( | ||
| escapeString('"hello world"') | ||
| ).toBe('\\"hello world\\"'); | ||
| }); | ||
|
|
||
| it('escapes newlines by default', () => { | ||
| expect( | ||
| escapeString('hello\r\nworld') | ||
| ).toBe('hello\\r\\nworld'); | ||
| }); | ||
|
|
||
| it('escapes backslashes', () => { | ||
| expect( | ||
| escapeString('hello\\world') | ||
| ).toBe('hello\\\\world'); | ||
| }); | ||
|
|
||
| it('escapes unrepresentable characters', () => { | ||
| expect( | ||
| escapeString('hello \u0000') // 0 = ASCII 'null' character | ||
| ).toBe('hello \\u0000'); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| export interface EscapeOptions { | ||
| /** | ||
| * The delimiter that will be used to wrap the string (and so must be escaped | ||
| * when used within the string). | ||
| * Defaults to " | ||
| */ | ||
| delimiter?: string; | ||
|
|
||
| /** | ||
| * The char to use to escape the delimiter and other special characters. | ||
| * Defaults to \ | ||
| */ | ||
| escapeChar?: string; | ||
|
|
||
| /** | ||
| * Whether newlines (\n and \r) should be escaped within the string. | ||
| * Defaults to true. | ||
| */ | ||
| escapeNewlines?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Escape characters within a value to make it safe to insert directly into a | ||
| * snippet. Takes options which define the escape requirements. | ||
| * | ||
| * This is closely based on the JSON-stringify string serialization algorithm, | ||
| * but generalized for other string delimiters (e.g. " or ') and different escape | ||
| * characters (e.g. Powershell uses `) | ||
| * | ||
| * See https://tc39.es/ecma262/multipage/structured-data.html#sec-quotejsonstring | ||
| * for the complete original algorithm. | ||
| */ | ||
| export function escapeString(rawValue: any, options: EscapeOptions = {}) { | ||
| const { | ||
| delimiter = '"', | ||
| escapeChar = '\\', | ||
| escapeNewlines = true | ||
| } = options; | ||
|
|
||
| const stringValue = rawValue.toString(); | ||
|
|
||
| return [...stringValue].map((c) => { | ||
| if (c === '\b') { | ||
| return escapeChar + 'b'; | ||
| } else if (c === '\t') { | ||
| return escapeChar + 't'; | ||
| } else if (c === '\n') { | ||
| if (escapeNewlines) { | ||
| return escapeChar + 'n'; | ||
| } else { | ||
| return c; // Don't just continue, or this is caught by < \u0020 | ||
| } | ||
| } else if (c === '\f') { | ||
| return escapeChar + 'f'; | ||
| } else if (c === '\r') { | ||
| if (escapeNewlines) { | ||
| return escapeChar + 'r'; | ||
| } else { | ||
| return c; // Don't just continue, or this is caught by < \u0020 | ||
| } | ||
| } else if (c === escapeChar) { | ||
| return escapeChar + escapeChar; | ||
| } else if (c === delimiter) { | ||
| return escapeChar + delimiter; | ||
| } else if (c < '\u0020' || c > '\u007E') { | ||
| // Delegate the trickier non-ASCII cases to the normal algorithm. Some of these | ||
| // are escaped as \uXXXX, whilst others are represented literally. Since we're | ||
| // using this primarily for header values that are generally (though not 100% | ||
| // strictly?) ASCII-only, this should almost never happen. | ||
| return JSON.stringify(c).slice(1, -1); | ||
| } else { | ||
| return c; | ||
| } | ||
| }).join(''); | ||
| } | ||
|
|
||
| /** | ||
| * Make a string value safe to insert literally into a snippet within single quotes, | ||
| * by escaping problematic characters, including single quotes inside the string, | ||
| * backslashes, newlines, and other special characters. | ||
| * | ||
| * If value is not a string, it will be stringified with .toString() first. | ||
| */ | ||
| export const escapeForSingleQuotes = (value: any) => | ||
pimterry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| escapeString(value, { delimiter: "'" }); | ||
|
|
||
| /** | ||
| * Make a string value safe to insert literally into a snippet within double quotes, | ||
| * by escaping problematic characters, including double quotes inside the string, | ||
| * backslashes, newlines, and other special characters. | ||
| * | ||
| * If value is not a string, it will be stringified with .toString() first. | ||
| */ | ||
| export const escapeForDoubleQuotes = (value: any) => | ||
pimterry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| escapeString(value, { delimiter: '"' }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| (require '[clj-http.client :as client]) | ||
|
|
||
| (client/get "http://mockbin.com/har" {:headers {:x-foo "Bar" | ||
| :x-bar "Foo"} | ||
| :quoted-value "\"quoted\" 'string'"} | ||
| :accept :json}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| GET /har HTTP/1.1 | ||
| Accept: application/json | ||
| X-Foo: Bar | ||
| X-Bar: Foo | ||
| Quoted-Value: "quoted" 'string' | ||
| Host: mockbin.com | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| HttpResponse<String> response = Unirest.get("http://mockbin.com/har") | ||
| .header("accept", "application/json") | ||
| .header("x-foo", "Bar") | ||
| .header("x-bar", "Foo") | ||
| .header("quoted-value", "\"quoted\" 'string'") | ||
| .asString(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.