Skip to content

Exporting a curl command with escaped characters results in a different request #7517

Closed

Description

@proteusvacuum

Problem Description

When copying a command to curl that contains newline or other characters, the resulting curl request is different to the original.

Steps to reproduce the behavior:

  1. Make a request that includes escaped characters:
    curl -H 'content-type: application/octet-stream' -d $'foo\nbar' -x localhost:8080 http://example.com

    In mitmproxy, you will see that the request body is

    foo
    bar
    
  2. Copy this request as curl from mitmproxy (export -> curl)
    You get:

    curl -H 'User-Agent: curl/8.5.0' -H 'Accept: */*' -H 'Proxy-Connection: Keep-Alive' -H 'content-type: application/octet-stream' -X POST http://example.com/ -d 'foo\x0abar'
    
  3. Run the resulting command (appending the proxy again)

    curl -H 'User-Agent: curl/8.5.0' -H 'Accept: */*' -H 'Proxy-Connection: Keep-Alive' -H 'content-type: application/octet-stream' -X POST http://example.com/ -d 'foo\x0abar' -x localhost:8080
    
  4. You see that the request body is not the same as the original:

    foo\x0abar
    

The escape characters that are added here are not being unquoted when being sent by curl, and so are just "characters".

I think the solution would be to let bash replace those characters using $ in front of the string: ... -d $'foo\x0abar or using --data-binary with a temporary file.

System Information

Mitmproxy: 11.1.0
Python:    3.12.2
OpenSSL:   OpenSSL 3.4.0 22 Oct 2024
Platform:  Linux-6.5.0-1021-oem-x86_64-with-glibc2.39

Activity

  1. added
    kind/triage
    on Jan 27, 2025
  2. mhils commented on Jan 27, 2025

    @mhils
    Member

    Thanks for the report. Happy to merge a fix. My preferred solution would be to use something like

    -d "$(printf 'foo\nbar')"

    In contrast to $''-style strings, that works with a bunch of non-bash shells as well (e.g. fish shell).

  3. proteusvacuum commented on Jan 27, 2025

    @proteusvacuum
    ContributorAuthor

    Thanks @mhils , I'll put together a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions