Snippets corrupt values with backslashes/quotes/+ and & or non-ASCII bodies; node axios/fetch snippets don't run

Open
#397 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Domain
devtools

Research direction

The bugs sit in several target generators: string-literal escaping in the javascript fetch, axios and xhr targets and the node native, axios and fetch targets, the url-encoding choice in src/targets/shell/curl/client.ts (around lines 143-151), and the body encoding in the python http.client and requests targets. Start by reproducing one case from the issue with a local echo server. Done means each affected snippet round-trips its values and the node snippets run as generated.

Written by the indexing model from the issue text.

Description

I ran the generated snippets against a local echo server and compared what arrived with the HAR. shell:wget, ruby:native and go:native round-tripped every value; the cases below did not.

// npm i [email protected]
const {HTTPSnippet} = require("httpsnippet");
const base = {method: "POST", url: "http://localhost:3000/p", headers: [], queryString: [], cookies: [], httpVersion: "HTTP/1.1"};
const gen = (h, t, c) => console.log(new HTTPSnippet({...base, ...h}).convert(t, c));

gen({postData: {mimeType: "application/json", text: JSON.stringify({path: "C:\\temp", q: 'say "hi"'})}}, "javascript", "fetch");
// body: '{"path":"C:\\temp","q":"say \"hi\""}'   -> sends {"path":"C:\temp","q":"say "hi""} (invalid JSON)
gen({queryString: [{name: "name", value: "O'Brien"}]}, "javascript", "fetch");
// const url = 'http://localhost:3000/p?name=O'Brien';   -> SyntaxError
gen({postData: {mimeType: "application/x-www-form-urlencoded", params: [{name: "expr", value: "1+1"}, {name: "q", value: "a&b=c"}]}}, "shell", "curl");
// --data 'expr=1+1' --data 'q=a&b=c'   -> server reads expr="1 1", q="a", b="c"
gen({postData: {mimeType: "application/json", text: JSON.stringify({city: "東京"})}}, "python", "python3");
// payload = "{\"city\":\"東京\"}"; conn.request(...)   -> UnicodeEncodeError: 'latin-1' codec
gen({postData: {mimeType: "application/x-www-form-urlencoded", params: [{name: "path", value: "C:\\temp"}]}}, "python", "requests");
// payload = { "path": "C:\temp" }   -> sends a TAB

1. JS string literals don't escape backslashes, and some don't escape '. In javascript:fetch, javascript:axios, javascript:xhr, node:native and node:axios, a value a\b is emitted as 'a\b' (a backspace), a\nb becomes a newline (and Node rejects it as a header value), and a JSON body with \" turns into invalid JSON. A ' in the query string or body produces a syntax error in javascript:fetch, node:fetch and javascript:xhr (ruby:faraday has the same problem with the query string). Expected the literal to evaluate to the original value.

2. shell:curl sends url-encoded form values unencoded. --data-urlencode is only chosen when the name needs encoding (the fix for #197), so a value with +, &, = or % under a plain name is sent raw: 1+1 arrives as 1 1, and a&b=c becomes an extra field.

3. python:python3 and python:requests fail on non-Latin-1 bodies. The body is passed as a str, which http.client encodes as ISO-8859-1, so 東京 raises UnicodeEncodeError and é is sent as the single byte 0xE9 instead of UTF-8. Encoding the payload (payload.encode("utf-8")) would fix it. Separately, python:requests builds form/JSON payload dicts without escaping backslashes ("C:\temp" above).

4. node:axios and node:fetch mix require() with top-level await. The output (const axios = require('axios').default; ... const { data } = await axios.request(options);) is a SyntaxError as CommonJS and a ReferenceError (require is not defined) as an ES module, so it can't run as generated.

Version: [email protected] (npm latest); same generator code on master 795d98e (e.g. src/targets/shell/curl/client.ts lines 143-151).

Credit: https://ucsbplse.github.io/

Dominant language
TypeScript
Stars
1.2k
Forks
245
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Kong/httpsnippet

All issues in Kong/httpsnippet

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.