Skip to content

fix(ai-prompt-*): handle nil err in JSON body parse path#13314

Merged
shreemaan-abhishek merged 1 commit intoapache:masterfrom
shreemaan-abhishek:fix/ai-prompt-nil-err
Apr 30, 2026
Merged

fix(ai-prompt-*): handle nil err in JSON body parse path#13314
shreemaan-abhishek merged 1 commit intoapache:masterfrom
shreemaan-abhishek:fix/ai-prompt-nil-err

Conversation

@shreemaan-abhishek
Copy link
Copy Markdown
Contributor

Description

Follow-up to #13096 (which corrected the wording of the JSON-parse error message in the AI prompt plugins). That patch left two nil-concatenation hazards behind that turn the affected plugins into 500s instead of returning the intended 4xx client error:

  1. core.request.get_body() can return nil with err == nil for empty request bodies. With:

    return nil, { message = "could not get body: " .. err }

    the concatenation crashes with attempt to concatenate local 'err' (a nil value).

  2. core.json.decode() returning nil with err == nil is much rarer but still possible, and the same concatenation hazard applies.

Affected files:

  • apisix/core/request.luaget_json_request_body_table(). The get_body() branch already had an (err or "request body is empty") guard from a previous patch; this PR adds the matching guard on the json.decode() branch.
  • apisix/plugins/ai-prompt-template.luaget_request_body_table(). Both branches need the guard.
  • apisix/plugins/ai-prompt-decorator.luaget_request_body_table(). Both branches need the guard.

Reproduction (before this PR):

POST / HTTP/1.1
Content-Type: application/json
Content-Length: 0

with ai-prompt-template enabled returns 500 instead of the configured "could not get body: request body is empty" message.

Which issue(s) this PR fixes:

Follow-up to #13096; no separate issue.

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change — the existing t/admin/routes_request_body.t exercises the nil-err empty-body path and now correctly produces the configured error message; happy to add a focused regression test in t/plugin/ai-prompt-template.t if reviewers want one.
  • I have updated the documentation to reflect this change — no doc impact; behaviour is now the documented one.
  • I have verified that this change is backward compatible

The recently-merged JSON-error-message wording fix (apache#13096) introduced
new nil-concatenation hazards on the empty-body / unparseable-body paths:

  - core.request.get_body() can return nil with err = nil for empty
    request bodies. With `"could not get body: " .. err` that crash
    turns the plugin into a 500 with
    `attempt to concatenate local 'err' (a nil value)`.
  - core.json.decode() returning nil with err = nil is much rarer but
    still possible, and the same concatenation hazard applies.

Add (err or fallback) guards in core/request.lua,
ai-prompt-template.lua, and ai-prompt-decorator.lua so an empty or
unparseable body produces the intended client error instead of a 500.

core/request.lua already had this guard on the get_body() branch;
this PR closes the matching gap on the json.decode() branch and
adds both guards to the two ai-prompt-* plugins.

Signed-off-by: Shreemaan Abhishek <shreemaan.abhishek@gmail.com>
Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Apr 29, 2026
@shreemaan-abhishek shreemaan-abhishek merged commit 6aa2ba3 into apache:master Apr 30, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants