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

Aberta
#397 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
35/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
javascript, nodejs, python, shell
Domínio
devtools

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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/

Linguagem predominante
TypeScript
Estrelas
1.2k
Forks
245
Métricas de merge de PRs
Nenhum PR com merge em 30d

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de Kong/httpsnippet

Todas as issues de Kong/httpsnippet

Issues semelhantes

Mais issues de TypeScript

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.