Skip to content

feat(go): add configurable size guardrails#3475

Open
ayush00git wants to merge 6 commits intoapache:mainfrom
ayush00git:fix/size-guardrails
Open

feat(go): add configurable size guardrails#3475
ayush00git wants to merge 6 commits intoapache:mainfrom
ayush00git:fix/size-guardrails

Conversation

@ayush00git
Copy link
Contributor

@ayush00git ayush00git commented Mar 13, 2026

Why?

go deserialization didn't have any configured guardrails for untrusted paylaods which leads to high memory pressure while allocation and Out of memory attacks.

What does this PR do?

Added two configurable guardrails MaxCollectionSize and MaxBinarySize, and implement size guardrails across the fory codegen.

  1. Configuration: Added MaxCollectionSize and MaxBinarySize to Config struct with corresponding options WithMaxCollectionSize and WithMaxBinarySize.
  2. Added ReadCollectionLength and ReadBinaryLength to ByteBuffer and ReadContext.
  3. These methods enforce configured limits and return specialized errors: ErrKindMaxCollectionSizeExceeded and ErrKindMaxBinarySizeExceeded.
  4. Updated the Go code generator (codegen/decoder.go) to use these guarded length methods in generated serializers.
  5. Removed the generic ReadLength method to ensure all length-reading paths in the codebase are subject to guardrails.

Related issues

Closes #3419

AI Contribution Checklist

  • Substantial AI assistance was used in this PR: yes

  • If yes, I included a completed AI Contribution Checklist in this PR description and the required AI Usage Disclosure.

  • If yes, I included the standardized AI Usage Disclosure block below.

  • If yes, I can explain and defend all important changes without AI help.

  • If yes, I reviewed AI-assisted code changes line by line before submission.

  • If yes, I ran adequate human verification and recorded evidence (checks run locally or in CI, pass/fail summary, and confirmation I reviewed results).

  • If yes, I added/updated tests and specs where required.

  • If yes, I validated protocol/performance impacts with evidence when applicable.

  • If yes, I verified licensing and provenance compliance.

AI Usage Disclosure
I used AI to find and replace the multiple iterations of `ReadLength` by the specific `ReadCollectionSize` / `ReadBinarySize` across the go runtime.
Also I used it to fix some errors during running tests.
I can still explain all of my work, as everything is tested by me.

Does this PR introduce any user-facing change?

  • Does this PR introduce any public API change?
  • Does this PR introduce any binary protocol compatibility change?

Benchmark

@ayush00git ayush00git requested a review from chaokunyang as a code owner March 13, 2026 17:23
@ayush00git ayush00git changed the title added guardrails and their type Errors feat(go): add configurable size guardrails Mar 13, 2026
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.

[Go] configurable size guardrails for untrusted payloads

1 participant