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

Use example values when importing OpenAPI definitions #5168

Closed
mcruz-gh opened this issue Jan 2, 2019 · 20 comments · Fixed by zaproxy/zap-extensions#2364
Closed

Use example values when importing OpenAPI definitions #5168

mcruz-gh opened this issue Jan 2, 2019 · 20 comments · Fixed by zaproxy/zap-extensions#2364
Assignees

Comments

@mcruz-gh
Copy link

mcruz-gh commented Jan 2, 2019

Good afternoon! I've noticed that the API scanner doesn't include the examples described in the definitions file imported from Swagger. ZAP API Scanner forms the POST requests with random data, and not with the data included in the OpenApi 2.0 definitions file.

If I use the following JSON to import URL's into the scanner:

{
    "swagger": "2.0",
    "info": {
        "version": "1.0",
        "title": "Title"
    },
    "host": "redacted.com",
    "basePath": "/redacted",
    "schemes": [
        "https"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ],
    "paths": {
        "/redacted": {
            "post": {
                "parameters": [
                    {
                        "in": "body",
                        "name": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/redacted"
                        },
                        "x-examples": {
                            "default": "{\"AAA\": \"aaa\", \"BBB\": \"bbb\", \"CCC\": \"ccc\"}"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "200 OK"
                    }
                }
            }
        }
    },
    "definitions": {
        "redacted": {
            "type": "object",
            "properties": {
                "AAA": {
                    "type": "string",
                    "example": "aaa"
                },
                "BBB": {
                    "type": "string",
                    "example": "bbb"
                },
                "CCC": {
                    "type": "string",
                    "example": "ccc"
                }
            }
        }
    }
}

ZAP API Scanner sends the following request to the endpoint:

POST /redacted HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0
Pragma: no-cache
Cache-Control: no-cache
Content-Length: redacted
Connection: close

{"AAA":"John Doe","BBB":"John Doe","CCC":"John Doe"}

Instead of:

POST /redacted HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0
Pragma: no-cache
Cache-Control: no-cache
Content-Length: redacted
Connection: close

{"AAA":"aaa","BBB":"bbb","CCC":"ccc"}

This prevents ZAP from consuming services without errors. How could I solve this problem? Thanks in advance!

@psiinon
Copy link
Member

psiinon commented Jan 3, 2019

Yeah, sounds like a restriction that it would be good to fix.
@tinplinplin are you ok with trying to make the relevant changes? If so we can provide advice and guidance..

@mcruz-gh
Copy link
Author

mcruz-gh commented Jan 3, 2019

Of course, I use this tool every day. I would be very happy to make some contributions for its improvement. Thanks!

@thc202
Copy link
Member

thc202 commented Jan 3, 2019

The code that generates the values for the parameters is:
https://github.com/zaproxy/zap-extensions/blob/d5b8667d9201d7c094124b3bb1c4dbc45d5c62a7/src/org/zaproxy/zap/extension/openapi/generators/DataGenerator.java#L112

so, default value to the example if set.

@mcruz-gh
Copy link
Author

mcruz-gh commented Jan 4, 2019

On first sight it seems that default values ​​are used for auto-populates body payloads, instead of values ​​that are supposed to be (example values). The Swagger Inflector library contains the ExampleBuilder class, used for this purpose. Is this the correct approach?

@mxbrandi
Copy link

👍

@kingthorin
Copy link
Member

kingthorin commented Mar 14, 2019

@tinplinplin I can't speak to the use of that particular library class. But, basically it needs to get/parse the examples (when available) and use those values instead of defaults. If there are (or can be) multiple examples then pick first/last (as long as we document it) or most complete (# of param if some are optional) [I assume that's a thing ...]

@ghost
Copy link

ghost commented Apr 12, 2019

We faced the same issue while API scan using swagger file for POST methods which expects a request body. The ZAP scan takes the default value as "John Doe" for all types. Due to this, our the scan response is always 400 bad request. Is there any way we can customise the inputs? How can we make sure that ZAP does pen testing(active scan) and the alerts are correct, since we are seeing each request is handling with the input value as 'John Doe' only.

@psiinon could you please help on this?

@psiinon
Copy link
Member

psiinon commented Apr 12, 2019

@greeshg this is explained in the help: https://github.com/zaproxy/zap-extensions/wiki/HelpAddonsOpenapiOpenapi#user-specified-values
The form handler add-on is https://github.com/zaproxy/zap-extensions/wiki/HelpAddonsFormhandlerFormHandlerHelp

@user23march
Copy link

Hi,
I am too facing the same issues that zap is replacing the request body field/parameter values with "john doe".

I tried by passing as "default" parameter and value for those particular request body parameters in openAPI swagger json file, but it didn't take those default values while running active scan through ZAP tool, it always takes "john doe" for all string parameters in request body.

I tried those options which is given in above links with using 'replacer option" in ZAP tool in that way it worked, but it is so lengthy and tedious task to do as I have more around 100 or more fields.

Does anybody know if there is any option to pass default values for request body paramaters in openAPI swagger jason file itself which should work for ZAP tool?

Thank you in advance.

@psiinon
Copy link
Member

psiinon commented Mar 23, 2020

@user23march
Copy link

@tinplinplin - did you fix this issue?
I am still facing this issue with zap 2.9.
ZAP is not reading the example values for request body parameters from openApi swagger file.
@psiinon - the above link which you sent is for replacer option. It's possible when i have only few like 5 or 6 param to replace in request body but I want example values to be read during scan from
openApi swagger. As I have 100s of params or may be more in requestBody , so I need to be passed through example in openApi swagger. Can you please tell me the solution?

Thanks in advance.

@thc202
Copy link
Member

thc202 commented Apr 5, 2020

This was not yet implemented (issues are usually closed once implemented).

There are only workarounds mentioned above.

@thc202 thc202 changed the title ZAP Api Scanner: POST request body issues Use example values when importing OpenAPI definitions Apr 5, 2020
@J12934
Copy link
Contributor

J12934 commented Apr 6, 2020

Hi 👋
I've started working on this related to another related issue (#5913).
I've opened up a Draft Pull Request (zaproxy/zap-extensions#2364) as this is still work in progress, but to give everybody here a chance to help and give feedback change.

@user23march
Copy link

Hi,
Just wanted to know when this change is going to come and with which ZAP version?
I hope it includes -> reading example values of request body parameters from swagger json file (openAPI).
Please let me know.

Thanks in advance.

@thc202
Copy link
Member

thc202 commented May 27, 2020

Answered in zaproxy/zap-extensions#2364 (comment)

@thc202
Copy link
Member

thc202 commented Jun 9, 2020

Released in version 16 of OpenAPI add-on.

@user23march
Copy link

@thc202 Hi, thank you so much.
can you please provide the link for version 16?
I see version 15 only in zap addons link.

@thc202
Copy link
Member

thc202 commented Jun 12, 2020

You can update/install it from within ZAP, if you need to manually download it it's in: https://github.com/zaproxy/zap-extensions/releases/tag/openapi-v16

@thc202
Copy link
Member

thc202 commented Jun 12, 2020

For the record, the add-ons page should now be showing the latest link/info.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging a pull request may close this issue.

7 participants