Skip to content

fix(csrf): safely validate decoded token table and fields - #14025

Open
bhuvan-somisetty wants to merge 1 commit into
apache:masterfrom
bhuvan-somisetty:fix-csrf-token-decode
Open

bhuvan-somisetty wants to merge 1 commit into
apache:masterfrom
bhuvan-somisetty:fix-csrf-token-decode

Conversation

@bhuvan-somisetty

Copy link
Copy Markdown

Description

In apisix/plugins/csrf.lua, check_csrf_token decodes the base64 token into JSON without verifying that the result is a table. When a client supplies a token decoding to a JSON primitive (e.g. number, boolean, null), indexing fields like token_table["random"] raises an unhandled Lua runtime error (e.g. attempt to index a number value), causing an HTTP 500 error instead of cleanly rejecting the request.

This PR:

  • Verifies type(token_table) == "table" after JSON decoding.
  • Validates that random, expires, and sign are present with valid scalar types.
  • Adds comprehensive unit test cases in t/plugin/csrf.t verifying that non-table JSON tokens and invalid fields are rejected with HTTP 401 without crashing.

Which issue(s) this PR fixes:

Fixes #14024

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/plugins/csrf.lua`, `check_csrf_token` decodes the base64 token
into JSON without verifying that the result is a table. When a client
supplies a token decoding to a JSON primitive (e.g. number, boolean, null),
indexing fields like `token_table["random"]` raises an unhandled Lua runtime
error (e.g. `attempt to index a number value`), causing a 500 error.

This commit verifies `type(token_table) == "table"` and checks that `random`,
`expires`, and `sign` have valid types, safely returning `false` on invalid
tokens.

Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
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: csrf plugin crashes with runtime error on malformed or non-table base64 token

1 participant