Skip to content

fix: invalidate req_post_args and post_arg.* cache on set_body_data - #14043

Open
bhuvan-somisetty wants to merge 2 commits into
apache:masterfrom
bhuvan-somisetty:fix-patch-set-body-data-invalidation
Open

bhuvan-somisetty wants to merge 2 commits into
apache:masterfrom
bhuvan-somisetty:fix-patch-set-body-data-invalidation

Conversation

@bhuvan-somisetty

Copy link
Copy Markdown

Description

In apisix/patch.lua, monkey-patched ngx.req.set_body_data only cleared _request_body_table and _request_body_type, leaving api_ctx.req_post_args populated with stale cached form arguments from the pre-rewrite body. In addition, invalidation of post_arg.* variables in var._cache was nested inside if api_ctx and api_ctx._request_body_table then, which skipped variable cache invalidation whenever _request_body_table was not yet populated or was nil.

This PR updates ngx.req.set_body_data:

  1. Always clears api_ctx.req_post_args so that subsequent calls to core.request.get_post_args(ctx) re-parse arguments from the updated body.
  2. Unconditionally clears post_arg.* variables in api_ctx.var._cache whenever ngx.req.set_body_data is invoked.
  3. Adds comprehensive test coverage in t/core/request.t (TEST 20).

Which issue(s) this PR fixes:

Fixes #14042

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
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

In `apisix/patch.lua`, monkey-patched `ngx.req.set_body_data` only cleared
`_request_body_table` and `_request_body_type`, leaving `api_ctx.req_post_args`
with stale cached arguments from the pre-rewrite body. Additionally,
clearing `post_arg.*` variables in `var._cache` was gated behind
`api_ctx._request_body_table`, skipping variable invalidation if
`_request_body_table` had not been populated or had already been cleared.

This commit updates `ngx.req.set_body_data` to always invalidate
`api_ctx.req_post_args` as well as any cached `post_arg.*` variables in
`api_ctx.var._cache` regardless of `_request_body_table` status.

Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
@hanzhenfang

Copy link
Copy Markdown
Contributor

Thanks for the PR, @bhuvan-somisetty!

Could we also invalidate the legacy post_arg_ variables here? core.ctx caches post_arg_foo in the same var._cache, but the invalidation loop only clears the post_arg. prefix.

After reading ctx.var.post_arg_foo and then calling ngx.req.set_body_data("foo=updated"), the cached variable can still return the original value even though req_post_args has been cleared. This appears to be another path of the existing cache-invalidation issue, rather than a regression introduced by this PR.

Please cover both prefixes and add a regression case that reads post_arg_foo before and after the rewrite. This observation is based on source review of f408d4a6; I have not run this case locally.

Extend the var._cache invalidation loop in patched ngx.req.set_body_data
to also match the legacy "post_arg_" prefix, preventing stale post_arg_foo
variables after a request body rewrite. Also update TEST 20 in
t/core/request.t to verify post_arg_ cache invalidation.

Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
@bhuvan-somisetty

Copy link
Copy Markdown
Author

Good point! I've updated the invalidation logic in apisix/patch.lua to clear both post_arg. and post_arg_ prefixes from var._cache, and extended the regression test in t/core/request.t to verify post_arg_foo before and after the rewrite.

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.

bug: ngx.req.set_body_data fails to invalidate req_post_args and post_arg.* cache

2 participants