Skip to content

ModSecurity fails to parse request body with special characters when using JSON requestBodyProcessor  #1879

@msamad

Description

@msamad

Hi,
ModSecurity (v3.0.2) fails the JSON requestBodyProcessor for a request when there are special characters such as '\r', '\n' etc in the body.

e.g. Following request results in 400 Bad Request where rule 200002 fails.

curl --request POST \
--header "Content-Type:application/json"  \
--data {"test":"Testing new line. \r\n Another line."} \
http://localhost:8080/some-resource

ModSecurity transaction log

{
	"transaction": {
		"client_ip": "172.30.0.1",
		"messages": [{
				"message": "Failed to parse request body.",
				"details": {
					"ver": "",
					"severity": "2",
					"reference": "v862,1",
					"ruleId": "200002",
					"tags": [],
					"rev": "",
					"maturity": "0",
					"file": "/etc/nginx/modsecurity/modsecurity.conf",
					"lineNumber": "44",
					"data": "JSON parsing error: lexical error: invalid string in json text.\n",
					"match": "Matched \"Operator `Eq' with parameter `0' against variable `REQBODY_ERROR' (Value: `1' )",
					"accuracy": "0"
				}
			}
		],
		"producer": {
			"connector": "ModSecurity-nginx v1.0.0",
			"components": ["OWASP_CRS/3.0.2\""],
			"modsecurity": "ModSecurity v3.0.2 (Linux)",
			"secrules_engine": "Enabled"
		},
		"host_port": 8080,
		"request": {
			"headers": {
				"Content-Length": "43",
				"Accept": "*/*",
				"User-Agent": "curl/7.51.0",
				"Host": "localhost:8080",
				"Content-Type": "application/json"
			},
			"http_version": 1.1,
			"method": "POST",
			"uri": "/some-resource"
		},
		"server_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
		"host_ip": "172.30.0.1",
		"client_port": 56170,
		"time_stamp": "Mon Aug 27 03:45:07 2018",
		"response": {
			"http_code": 400
		},
		"id": "15353415074.588818"
	}
}

Nginx access log escapes the request body properly.

{
	"time_local": "27/Aug/2018:03:45:07 +0000",
	"remote_addr": "172.30.0.1",
	"remote_port": "56170",
	"remote_user": "",
	"request": "POST /some-resource HTTP/1.1",
	"status": 400,
	"request_length": "854",
	"request_method": "POST",
	"content_length": "43",
	"content_type": "application/json",
	"bytes_sent": "589",
	"body_bytes_sent": "176",
	"request_body": "{test:Testing new line. \\r\\n Another line.}",
	"http_user_agent": "curl/7.51.0"
}

It seems that ModSecurity is not escaping the '\r', \n' etc which results in a failure when parsing into a JSON object.

Thanks.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions