Skip to content
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

[BUG] curl code samples omit non-printable characters #2159

Open
3 tasks done
acdha opened this issue May 7, 2024 · 3 comments
Open
3 tasks done

[BUG] curl code samples omit non-printable characters #2159

acdha opened this issue May 7, 2024 · 3 comments
Assignees
Labels
Status: Needs Triage Requires initial assessment to categorize and prioritize Type: Bug Errors or unexpected behavior

Comments

@acdha
Copy link

acdha commented May 7, 2024

Checklist

  • I checked the FAQ section of the documentation
  • I looked for similar issues in the issue tracker
  • I am using the latest version of Schemathesis

Describe the bug

I had a FastAPI endpoint which accidentally leaked the header definition for X-Forwarded-For into the OpenAPI docs and that caused a Schemathesis run to fail, emitting a curl sample like this:

    curl -X GET -H 'x-forwarded-for: 0' http://localhost:8000/search/latest-magazines

That worked without error when I tried to reproduce, but I knew it was failing based on my service's logs. I added --code-sample-style=python and the cause became clear:

requests.get('http://localhost:8000/search/latest-magazines', headers={'x-forwarded-for': '0\x1f'})

To Reproduce

  1. Start a run against a service which will trigger an error on the presence of an unprintable character (in my case, the ASGI runner itself triggered the error so it wasn't something my application knows about when generating the OpenAPI docs but that's obviously not a prerequisite).

Please include a minimal API schema causing this issue:

{
    "openapi": "3.1.0",
    "paths": {
        "/search/latest-magazines": {
            "get": {
                "summary": "Get a list of the latest magazine issues",
                "operationId": "getLatestMagazines",
                "parameters": [
                    {
                        "name": "x-forwarded-for",
                        "in": "header",
                        "required": false,
                        "schema": {
                            "anyOf": [
                                {
                                    "type": "string"
                                },
                                {
                                    "type": "null"
                                }
                            ],
                            "title": "X-Forwarded-For"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/SearchResponse"
                                }
                            }
                        }
                    },
                    "422": {
                        "description": "Validation Error",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/ValidationError"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "SearchResponse": {},
            "ValidationError": {}
        }
    }
}

Expected behavior

Ideally the curl examples would either include the escaped values — but this introduces shell-specific behaviour since you'd need to use something like curl -H "X-Forwarded-For: $(printf '\01f')" … so it might be effective to simply alert the user with a text message that the curl sample is incomplete. In my case, the fact that it printed without a warning and with what appears to be a complete string was somewhat confusing

Environment

- OS: macOS
- Python version: 3.12
- Schemathesis version: 3.27.1
- Spec version: OpenAPI 3.1.0
@acdha acdha added Status: Needs Triage Requires initial assessment to categorize and prioritize Type: Bug Errors or unexpected behavior labels May 7, 2024
@Stranger6667
Copy link
Member

Thank you for reporting!
indeed, I think that for now the best course of actions would be to warn the user. This is somewhat unfortunate that properly solving the issue would introduce shell-dependent code :( not sure if it could be worked around with some built-in curl feature

@acdha
Copy link
Author

acdha commented May 9, 2024

Thank you for reporting! indeed, I think that for now the best course of actions would be to warn the user. This is somewhat unfortunate that properly solving the issue would introduce shell-dependent code :( not sure if it could be worked around with some built-in curl feature

Thinking about it a bit more, maybe just do something as simple as displaying a warning along the lines of “This test involves characters which are difficult to portably represent in shell commands. Here's the Python version:” and override the code sample language? I'd assume that most people using this tool wouldn't be terribly surprised by that and it's something of an edge case so it makes sense to switch to the alternative you already have.

@Stranger6667
Copy link
Member

I like the idea and wording that you proposed! An alternative I was thinking about is showing the cURL command without headers that contain such characters and then displaying headers separately in the warning message, but probably it would not be as useful.

Got to research if it could be possible to detect the shell and display the headers properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Triage Requires initial assessment to categorize and prioritize Type: Bug Errors or unexpected behavior
Projects
None yet
Development

No branches or pull requests

2 participants