Skip to content

upb: guard against NULL symtab in upb_JsonDecode for extensions and Any - #31878

Open
hyc-alt wants to merge 1 commit into
protocolbuffers:mainfrom
hyc-alt:fix/upb-json-decode-null-symtab
Open

hyc-alt wants to merge 1 commit into
protocolbuffers:mainfrom
hyc-alt:fix/upb-json-decode-null-symtab

Conversation

@hyc-alt

@hyc-alt hyc-alt commented Nov 27, 2026

Copy link
Copy Markdown

Summary

Fixes two NULL-pointer dereferences (SIGSEGV) in upb_JsonDecode() (upb/json/decode.c) triggered by untrusted JSON inputs when symtab is NULL.

Root Cause

Passing symtab == NULL is supported for decoding messages that do not require extension or google.protobuf.Any resolution (matching upb/json/encode.c:370, which explicitly checks if (!e->ext_pool) before encoding Any).

However, two call sites in upb/json/decode.c unconditionally dereference d->symtab:

  1. jsondec_field() (upb/json/decode.c:966-969): Any JSON object with a bracketed key such as {"[foo.bar]": 1} unconditionally calls upb_DefPool_FindExtensionByNameWithSize(d->symtab, ...), crashing with SIGSEGV inside upb_strtable_lookup2 (upb/hash/common.c:133), even when upb_JsonDecode_IgnoreUnknown is enabled.
  2. jsondec_typeurl() (upb/json/decode.c:1433): Decoding a google.protobuf.Any @type URL unconditionally calls upb_DefPool_FindMessageByNameWithSize(d->symtab, ...), crashing with SIGSEGV instead of returning an error status.

Fix

  • In jsondec_field(), guard d->symtab so bracketed extension keys resolve to NULL (handled by the existing unknown-field / IgnoreUnknown path) when d->symtab == NULL.
  • In jsondec_typeurl(), check if (!d->symtab) and report "Tried to decode Any, but no symtab was provided" (mirroring upb/json/encode.c:371).

Testing

  • Added regression test JsonTest.NullSymtabExtensionAndAny in upb/json/decode_test.cc covering bracketed extension keys (default and upb_JsonDecode_IgnoreUnknown) and google.protobuf.Any with symtab == nullptr.
  • Verified before/after under ASan+UBSan in both Release (-DNDEBUG) and Debug builds as well as -std=c99 -Wall -Wextra -Werror.

Passing a NULL symtab to upb_JsonDecode() is supported for messages that do
not require extension or Any type resolution (matching upb_JsonEncode(),
which explicitly checks `if (!e->ext_pool)` before encoding Any).

However, two sites in upb/json/decode.c unconditionally pass d->symtab into
upb_DefPool lookup functions:
1. jsondec_field() passes d->symtab to upb_DefPool_FindExtensionByNameWithSize()
   whenever an input JSON object contains a bracketed key such as `"[foo]"`,
   causing an immediate NULL-pointer dereference even when
   upb_JsonDecode_IgnoreUnknown is enabled.
2. jsondec_typeurl() passes d->symtab to upb_DefPool_FindMessageByNameWithSize()
   when decoding a google.protobuf.Any `@type` URL, causing a NULL-pointer
   dereference instead of returning an error status.

Guard d->symtab at both sites: treat bracketed extension keys as unknown
fields when d->symtab is NULL, and return an error message matching
upb/json/encode.c when decoding Any without a symtab.

This branch has not been deployed

No deployments
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.

1 participant