-
-
Notifications
You must be signed in to change notification settings - Fork 337
Fill ARGS_POST variable even if body bytes were invalid JSON when JSON body processor is running #1615
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
base: main
Are you sure you want to change the base?
Fill ARGS_POST variable even if body bytes were invalid JSON when JSON body processor is running #1615
Changes from all commits
4c44f4f
4915775
5533954
b192bf7
01e87f5
879cd8d
cb392d5
daff6e7
b59a645
ae69035
d454321
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,12 +30,13 @@ func (js *jsonBodyProcessor) ProcessRequest(reader io.Reader, v plugintypes.Tran | |
| // Process with recursion limit | ||
| col := v.ArgsPost() | ||
| data, err := readJSON(ss, bpo.RequestBodyRecursionLimit) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // The collection is populated before checking the error to still perform a best effort inspection of the payload | ||
| for key, value := range data { | ||
| col.SetIndex(key, 0, value) | ||
| } | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Store the raw JSON in the TX variable for validateSchema | ||
| // This is needed because RequestBody is a Single interface without a Set method | ||
|
|
@@ -59,12 +60,13 @@ func (js *jsonBodyProcessor) ProcessResponse(reader io.Reader, v plugintypes.Tra | |
| // Process with no recursion limit as we don't have a directive for response body | ||
| col := v.ResponseArgs() | ||
| data, err := readJSON(ss, ignoreJSONRecursionLimit) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // The collection is populated before checking the error to still perform a best effort inspection of the payload | ||
| for key, value := range data { | ||
|
M4tteoP marked this conversation as resolved.
|
||
| col.SetIndex(key, 0, value) | ||
| } | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Store the raw JSON in the TX variable for validateSchema | ||
| // This is needed because ResponseBody is a Single interface without a Set method | ||
|
|
@@ -80,12 +82,15 @@ func readJSON(s string, maxRecursion int) (map[string]string, error) { | |
| res := make(map[string]string) | ||
| key := []byte("json") | ||
|
|
||
| json := gjson.Parse(s) | ||
| err := readItems(json, key, maxRecursion, res) | ||
| if err != nil { | ||
| return res, err | ||
| } | ||
| if !gjson.Valid(s) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please explain why are we parsing the json before verifying out is valid?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is to support "best effort" parsing when no full JSON is going to be received. This is almost always the case when you have "ProcessPartial" active. Without this, the JSON parser will always fail on any incomplete JSON.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So first we try to parse whatever JSON we got into the coraza strucutures then we check for the JSON validity and fail if it was not valid. If it was the other way around then incomplete JSON will always be invalid and we never reach the "best effort" parsing part. |
||
| return res, errors.New("invalid JSON") | ||
| } | ||
| json := gjson.Parse(s) | ||
| err := readItems(json, key, maxRecursion, res) | ||
| return res, err | ||
| return res, nil | ||
| } | ||
|
|
||
| // Transform JSON to a map[string]string | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.