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

未關閉
#397 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
活躍
領域
devtools

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

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/

主要語言
TypeScript
星號
1.2k
分支
245
PR 合併指標
30 天內沒有已合併 PR

環境準備

  • 提供 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

Kong/httpsnippet 的其他 Issue

查看 Kong/httpsnippet 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。