fix: preserve value in t:base64Decode when input is not valid base64#3533
fix: preserve value in t:base64Decode when input is not valid base64#35330xst3m wants to merge 1 commit intoowasp-modsecurity:v3/masterfrom
Conversation
Add Base64::tryDecode() that checks mbedtls return value before replacing the variable. On invalid input, the original value is now preserved and transform returns false.
|
|
Thanks for the fix — the core logic (checking the mbedtls return code) is correct and the bug analysis is solid. One suggestion: rather than adding a new The only other caller is Leaving the broken
This keeps the API surface smaller and ensures all base64 decoding in the project handles errors properly. |



What
When
t:base64Decodeencounters non-base64 input, the transformation now preserves the original value unchanged and returnsfalse(no change), instead of silently replacing it with an empty string and returningtrue.Why
The
base64Helper()function insrc/utils/base64.cccallsmbedtls_base64_decodebut ignores its error code return value. It only checks theout_lenoutput parameter, which stays0when mbedtls returnsMBEDTLS_ERR_BASE64_INVALID_CHARACTER(-0x002C). This causesBase64Decode::transform()to unconditionally replace the variable's value with an empty string for any non-base64 input.Impact:
t:base64Decodein a pipeline run on an empty string — effectively dead code for non-base64 inputt:base64Decodefrom rules 934130/934131 as a workaround (coreruleset/coreruleset#3376)How
Added
Base64::tryDecode()which checks thembedtls_base64_decodereturn value before proceeding:MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL→ valid base64, proceed with decodefalse(value preserved)This is a whitelist approach — only the known-good return code proceeds. Any current or future error codes from mbedtls (including the new
MBEDTLS_ERR_ERROR_CORRUPTION_DETECTEDin mbedtls v4.x) are automatically handled.Files changed:
src/utils/base64.htryDecode()method declarationsrc/utils/base64.cctryDecode()implementationsrc/actions/transformations/base64_decode.cctryDecode(), preserve value on failureBackward compatibility:
Base64::decode()API is unchanged —remote_user.ccis not affectedReferences
Proposed test additions
For secrules-language-tests
transformations/base64Decode.json:{ "ret" : 0, "input" : "not;valid;base64", "type" : "tfn", "name" : "base64Decode", "output" : "not;valid;base64" }, { "ret" : 0, "input" : "hello world!", "type" : "tfn", "name" : "base64Decode", "output" : "hello world!" }