Skip to content

feat: return clear error for invalid JSON when EnableAcceptJsonRequest is enabled#1610

Merged
hsluoyz merged 3 commits intomasterfrom
copilot/fix-json-arguments-parsing
Dec 19, 2025
Merged

feat: return clear error for invalid JSON when EnableAcceptJsonRequest is enabled#1610
hsluoyz merged 3 commits intomasterfrom
copilot/fix-json-arguments-parsing

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 19, 2025

When EnableAcceptJsonRequest(true) is enabled, invalid JSON strings (e.g., containing \x escape sequences not supported by JSON spec) fail to parse but errors are silently ignored. The string remains unparsed, causing confusing downstream errors like "Unable to access 'Name', 'r_sub' is not a struct or map" when matchers attempt field access.

Changes

enforcer.go

  • Only attempt JSON parsing for strings starting with { or [
  • Return descriptive error when JSON parsing fails instead of silent fallthrough
  • Error format: "failed to parse JSON parameter at index %d: %w"

enforcer_json_test.go (new)

  • Test invalid JSON returns clear error
  • Test valid JSON parses correctly
  • Test plain strings are not treated as JSON

Example

Before:

e.EnableAcceptJsonRequest(true)
e.Enforce(`{"Name": "\x20"}`, "data", "read")
// Error: Unable to access 'Name', 'r_sub' is not a struct or map

After:

e.EnableAcceptJsonRequest(true)
e.Enforce(`{"Name": "\x20"}`, "data", "read")
// Error: failed to parse JSON parameter at index 0: invalid character 'x' in string escape code

Note: JSON only supports \", \\, \/, \b, \f, \n, \r, \t, and \uXXXX escape sequences. \x is not valid JSON.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug] JSON string arguments are not parsed correctly in Enforce despite EnableAcceptJsonRequest(true)</issue_title>
<issue_description>
When EnableAcceptJsonRequest(true) is enabled, passing a valid JSON string as a parameter to Enforce() fails if the matcher attempts to access a field within that JSON object (e.g., r.sub.Name). The enforcer returns an error indicating that the parameter is not a struct or map, implying that the JSON string was not automatically parsed into an object before evaluation.

To Reproduce
Reproduction code:

func TestReproduce(t *testing.T) {
	modelText := `
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub.Name == "\x20"
`

	m, err := model.NewModelFromString(modelText)
	if err != nil {
		t.Fatalf("Failed to create model: %v", err)
	}
	e, err := NewEnforcer(m)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
	}
	dataJSON := `{"Name": "\x20"}`
	e.EnableAcceptJsonRequest(true)
	res, err := e.Enforce(dataJSON, true, "read")
	fmt.Printf("result: %v, err: %v\n", res, err)  -> “result: false, err: Unable to access 'Name', 'r_sub' is not a struct or map”
}

And I also try to reproduce it in editor. However, the editor return false without any error. Their behaviours are inconsistent as well. https://editor.casbin.org/#ETVDB3RF7
</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@hsluoyz hsluoyz marked this pull request as ready for review December 19, 2025 13:29
Copilot AI and others added 2 commits December 19, 2025 13:35
When EnableAcceptJsonRequest is enabled, strings that look like JSON (starting with { or [) are now parsed. If parsing fails, a clear error is returned instead of silently ignoring the error and causing confusing "is not a struct or map" errors later.

Changes:
- Only attempt JSON parsing for strings starting with { or [
- Return descriptive error when JSON parsing fails
- Add test case for invalid JSON handling

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix JSON string arguments parsing in Enforce Return clear error for invalid JSON when EnableAcceptJsonRequest is enabled Dec 19, 2025
Copilot AI requested a review from hsluoyz December 19, 2025 13:45
@hsluoyz hsluoyz changed the title Return clear error for invalid JSON when EnableAcceptJsonRequest is enabled feat: return clear error for invalid JSON when EnableAcceptJsonRequest is enabled Dec 19, 2025
@hsluoyz hsluoyz merged commit 84c825d into master Dec 19, 2025
4 of 6 checks passed
hsluoyz added a commit that referenced this pull request Dec 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[question] JSON string arguments are not parsed correctly in Enforce despite EnableAcceptJsonRequest(true)

3 participants