Skip to content

Improvements to decompression logic#2878

Open
VirajSalaka wants to merge 3 commits into
wso2:mainfrom
VirajSalaka:decompress-fix
Open

Improvements to decompression logic#2878
VirajSalaka wants to merge 3 commits into
wso2:mainfrom
VirajSalaka:decompress-fix

Conversation

@VirajSalaka

Copy link
Copy Markdown
Contributor

Purpose

Improvements request response decompression

Goals

Describe what solutions this feature or fix introduces to address the problems outlined above.

Approach

Describe how you are implementing the solutions. Include an animated GIF or screenshot if the change affects the UI. Include a link to a Markdown file or Google doc if the feature write-up is too long to paste here.

User stories

Summary of user stories addressed by this change>

Documentation

Link(s) to product documentation that addresses the changes of this PR. If no doc impact, enter “N/A” plus brief explanation of why there’s no doc impact

Automation tests

  • Unit tests

    Code coverage information

  • Integration tests

    Details about the test cases and coverage

Security checks

Samples

Provide high-level details about the samples related to this feature

Related PRs

List any other related PRs

Test environment

List all JDK versions, operating systems, databases, and browser/versions on which this feature/fix was tested

@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@VirajSalaka, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 30 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 8b98a451-5277-4dd8-954b-46219d17c5ff

📥 Commits

Reviewing files that changed from the base of the PR and between 94ff883 and ed056b2.

📒 Files selected for processing (9)
  • gateway/configs/config-template.toml
  • gateway/gateway-runtime/policy-engine/cmd/policy-engine/main.go
  • gateway/gateway-runtime/policy-engine/internal/analytics/analytics_test.go
  • gateway/gateway-runtime/policy-engine/internal/config/config.go
  • gateway/gateway-runtime/policy-engine/internal/config/config_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/decompression.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/decompression_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go
📝 Walkthrough

Walkthrough

Adds configurable per-direction decompressed-body limits for the policy engine, bounded buffered and streaming decompression, overflow handling, stream cleanup, and configuration and kernel tests.

Changes

Decompression limits

Layer / File(s) Summary
Configuration contract
gateway/configs/config-template.toml, gateway/gateway-runtime/policy-engine/internal/config/..., gateway/gateway-runtime/policy-engine/internal/analytics/analytics_test.go
Adds request and response max_decompressed_bytes settings, 10 MiB defaults, positive-value validation, and supporting fixtures.
Bounded decompression engine
gateway/gateway-runtime/policy-engine/internal/kernel/decompression.go, decompression_test.go
Adds bounded buffered decompression, deterministic streaming decoder coordination, overflow errors, cleanup behavior, and decompression-bomb coverage.
Kernel enforcement and cleanup
gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go, execution_context_test.go
Applies request/response limits, returns 413 for oversized buffered requests, fails closed for response and streaming failures, and cleans up decoders.
Server wiring and compatibility updates
gateway/gateway-runtime/policy-engine/cmd/policy-engine/main.go, internal/kernel/extproc.go, internal/kernel/*_test.go
Passes configured limits through server construction, defers stream decompressor cleanup, and updates affected tests and benchmarks.

Estimated code review effort: 4 (Complex) | ~60 minutes

Possibly related PRs

Suggested reviewers: krishanx92, renuka-fernando, pubudu538, ashera96, chamilaadhi

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is mostly a pasted template with only a brief Purpose line; the remaining required sections are unfilled or generic. Fill in Purpose, Goals, Approach, User stories, Documentation, tests, Security checks, Samples, Related PRs, and Test environment with PR-specific details.
Docstring Coverage ⚠️ Warning Docstring coverage is 54.81% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and clearly points to the decompression logic changes in this PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (4)
gateway/gateway-runtime/policy-engine/internal/kernel/extproc_test.go (1)

101-106: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Test the request and response limits independently.

Both constructor arguments currently use testMaxDecompressedBytes, so this test cannot catch request/response assignment swaps or dropped values.

Proposed test adjustment
-	server := NewExternalProcessorServer(kernel, chainExecutor, tracingConfig, "test-service", testMaxDecompressedBytes, testMaxDecompressedBytes)
+	const requestLimit int64 = 1024
+	const responseLimit int64 = 2048
+	server := NewExternalProcessorServer(kernel, chainExecutor, tracingConfig, "test-service", requestLimit, responseLimit)

 	require.NotNil(t, server)
 	assert.Equal(t, kernel, server.kernel)
 	assert.Equal(t, chainExecutor, server.executor)
 	assert.NotNil(t, server.tracer)
+	assert.Equal(t, requestLimit, server.maxRequestDecompressedBytes)
+	assert.Equal(t, responseLimit, server.maxResponseDecompressedBytes)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gateway/gateway-runtime/policy-engine/internal/kernel/extproc_test.go` around
lines 101 - 106, Update the NewExternalProcessorServer test setup to pass
distinct request and response decompression-limit values, then assert the
corresponding server fields retain each value independently. Keep the existing
kernel, executor, and tracer assertions unchanged, and use the server’s
request/response limit symbols to verify constructor assignment order.
gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go (1)

103-107: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Add a regression test for deferred decompressor cleanup.

Exercise Process with active request/response stream decompressors and verify they are closed and cleared on EOF, receive errors, send errors, and processing errors.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go` around
lines 103 - 107, Add a regression test covering Process with active request and
response stream decompressors, verifying deferred cleanup closes and clears both
decompressors on EOF, receive errors, send errors, and processing errors.
Exercise each outcome through the existing Process test setup and assert cleanup
occurs after the call returns.
gateway/gateway-runtime/policy-engine/internal/config/config.go (1)

171-176: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Tracked TODO in a new public config surface.

Request/Response are exported koanf-bound fields; renaming later is a breaking config change for operators. Worth resolving the naming before this ships rather than after [policy_engine.request] is documented in the template.

Want me to open an issue to track the rename?

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gateway/gateway-runtime/policy-engine/internal/config/config.go` around lines
171 - 176, Resolve the naming TODO in the configuration struct before exposing
the public koanf keys: replace the generic Request and Response BodyConfig
fields with names that clearly represent their body-processing scope, and update
all references and configuration documentation/templates consistently. Preserve
separate request- and response-direction limits while avoiding a later breaking
rename of the policy_engine configuration surface.
gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go (1)

519-534: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Buffered paths still fall back to raw compressed bytes on non-limit decompression errors.

Streaming request/response now fail closed for malformed encodings (Lines 593-598, 919-929), but the buffered request/response paths still clear the encoding and hand raw compressed bytes to policies. That means policies inspect ciphertext-like bytes and body-driven decisions (auth, guardrails, content checks) silently degrade for a malformed content-encoding — the same failure mode the streaming change deliberately closed. Consider aligning buffered handling with the streaming behavior in a follow-up.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go`
around lines 519 - 534, Update the buffered request decompression handling
around decompressBody so every non-limit decompression error fails closed like
the streaming paths, rather than clearing ec.requestContentEncoding and
forwarding raw compressed bytes to policies. Preserve the existing
ErrDecompressedTooLarge response handling, and apply the same behavior to the
corresponding buffered response path if it has the same fallback.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@gateway/gateway-runtime/policy-engine/internal/config/config.go`:
- Around line 171-176: Resolve the naming TODO in the configuration struct
before exposing the public koanf keys: replace the generic Request and Response
BodyConfig fields with names that clearly represent their body-processing scope,
and update all references and configuration documentation/templates
consistently. Preserve separate request- and response-direction limits while
avoiding a later breaking rename of the policy_engine configuration surface.

In `@gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go`:
- Around line 519-534: Update the buffered request decompression handling around
decompressBody so every non-limit decompression error fails closed like the
streaming paths, rather than clearing ec.requestContentEncoding and forwarding
raw compressed bytes to policies. Preserve the existing ErrDecompressedTooLarge
response handling, and apply the same behavior to the corresponding buffered
response path if it has the same fallback.

In `@gateway/gateway-runtime/policy-engine/internal/kernel/extproc_test.go`:
- Around line 101-106: Update the NewExternalProcessorServer test setup to pass
distinct request and response decompression-limit values, then assert the
corresponding server fields retain each value independently. Keep the existing
kernel, executor, and tracer assertions unchanged, and use the server’s
request/response limit symbols to verify constructor assignment order.

In `@gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go`:
- Around line 103-107: Add a regression test covering Process with active
request and response stream decompressors, verifying deferred cleanup closes and
clears both decompressors on EOF, receive errors, send errors, and processing
errors. Exercise each outcome through the existing Process test setup and assert
cleanup occurs after the call returns.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: e7ef5f0a-1127-4feb-8c65-668b800db23e

📥 Commits

Reviewing files that changed from the base of the PR and between 71fd31a and bec0062.

📒 Files selected for processing (13)
  • gateway/configs/config-template.toml
  • gateway/gateway-runtime/policy-engine/cmd/policy-engine/main.go
  • gateway/gateway-runtime/policy-engine/internal/analytics/analytics_test.go
  • gateway/gateway-runtime/policy-engine/internal/config/config.go
  • gateway/gateway-runtime/policy-engine/internal/config/config_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/decompression.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/decompression_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/execution_context_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc_bench_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/translator_test.go

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 26, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
gateway/gateway-runtime/policy-engine/internal/kernel/execution_context_test.go (1)

53-53: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Optional: extract a test-server helper.

The constructor call is repeated ~25 times with identical trailing limit args; a small helper (newTestServer(kernel, exec) plus a limits variant) would keep the next signature change to one line.

Also applies to: 101-101, 131-131, 642-642, 693-693, 808-808, 891-891

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@gateway/gateway-runtime/policy-engine/internal/kernel/execution_context_test.go`
at line 53, Extract a test helper for constructing the external processor server
with the repeated tracing and decompression-limit arguments, such as a default
helper accepting kernel and executor plus a limits variant for differing limits.
Replace the repeated NewExternalProcessorServer calls in
execution_context_test.go, including the referenced locations, while preserving
existing argument values and behavior.
gateway/gateway-runtime/policy-engine/internal/config/config.go (1)

171-191: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Tracked TODO on BodyConfig naming.

The struct is already exported and keyed as [policy_engine.request]/[policy_engine.response], so a later rename is a breaking config/API change. Worth resolving now or tracking explicitly.

Want me to open an issue for the rename?

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gateway/gateway-runtime/policy-engine/internal/config/config.go` around lines
171 - 191, The TODO indicates BodyConfig has a misleading generic name despite
containing direction-specific request/response settings. Rename BodyConfig and
update the Request and Response field types, comments, and all references to use
a clearer direction-agnostic configuration name; ensure the change is
consistently applied across the config API and parsing paths.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@gateway/gateway-runtime/policy-engine/internal/kernel/execution_context_test.go`:
- Around line 82-91: Replace the non-blocking decoderDone checks in the
decompressor cleanup test with bounded waits, allowing each decoder goroutine
time to finish and signal completion while still failing on timeout. Preserve
the separate validation and failure messages for requestDecomp and
responseDecomp.

---

Nitpick comments:
In `@gateway/gateway-runtime/policy-engine/internal/config/config.go`:
- Around line 171-191: The TODO indicates BodyConfig has a misleading generic
name despite containing direction-specific request/response settings. Rename
BodyConfig and update the Request and Response field types, comments, and all
references to use a clearer direction-agnostic configuration name; ensure the
change is consistently applied across the config API and parsing paths.

In
`@gateway/gateway-runtime/policy-engine/internal/kernel/execution_context_test.go`:
- Line 53: Extract a test helper for constructing the external processor server
with the repeated tracing and decompression-limit arguments, such as a default
helper accepting kernel and executor plus a limits variant for differing limits.
Replace the repeated NewExternalProcessorServer calls in
execution_context_test.go, including the referenced locations, while preserving
existing argument values and behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 9b40221f-a01b-4160-b0bd-248fdd93c370

📥 Commits

Reviewing files that changed from the base of the PR and between bec0062 and 8b5496d.

📒 Files selected for processing (14)
  • gateway/configs/config-template.toml
  • gateway/gateway-runtime/policy-engine/cmd/policy-engine/main.go
  • gateway/gateway-runtime/policy-engine/internal/analytics/analytics_test.go
  • gateway/gateway-runtime/policy-engine/internal/config/config.go
  • gateway/gateway-runtime/policy-engine/internal/config/config_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/decompression.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/decompression_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/downstream_upstream_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/execution_context_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc_bench_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/translator_test.go
🚧 Files skipped from review as they are similar to previous changes (9)
  • gateway/gateway-runtime/policy-engine/cmd/policy-engine/main.go
  • gateway/gateway-runtime/policy-engine/internal/analytics/analytics_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc_bench_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/translator_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go
  • gateway/gateway-runtime/policy-engine/internal/config/config_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/decompression.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/extproc.go

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 27, 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.

1 participant