Skip to content

fix(streaming): bind customer attribution map values - #4848

Merged
chrisgacsal merged 2 commits into
mainfrom
fix/ch-sqli
Aug 4, 2026
Merged

chrisgacsal merged 2 commits into
mainfrom
fix/ch-sqli

Conversation

@chrisgacsal

@chrisgacsal chrisgacsal commented Aug 4, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

Fixes a potential ClickHouse SQL injection vulnerability where customer keys and usage-attribution subject keys were interpolated directly into streaming queries. The attribution lookup now uses mapFromArrays with bound parameters, keeping untrusted values separate from the generated SQL.

Unit tests verify safe binding for multiple customers and quote-containing keys. A new end-to-end test confirms that events sent through every customer attribution key remain queryable by customer ID, customer key, and individual subject keys.

Fixes: #4847

Summary by CodeRabbit

  • New Features
    • Added end-to-end coverage for listing events by customer attribution and subject keys.
  • Bug Fixes
    • Improved customer attribution query handling by passing subject and customer mappings as bound parameters.
    • Prevented special characters in attribution keys from being embedded directly in generated queries.
  • Tests
    • Expanded query validation for customer and subject filtering, including malicious input scenarios.

Greptile Summary

The PR removes direct SQL interpolation from ClickHouse customer-attribution maps and binds subject keys and customer IDs as parallel arrays instead.

  • Replaces the literal WITH map(...) expression with mapFromArrays(?, ?) in event and meter queries.
  • Updates SQL-builder expectations for the new parameter ordering and array arguments.
  • Adds quote-containing-key coverage and an end-to-end attribution test across customer and subject keys.

Confidence Score: 5/5

The PR appears safe to merge, with no concrete changed-code failure identified.

The attribution keys and customer IDs are removed from generated SQL and supplied as aligned bound arrays, while the updated unit and end-to-end coverage preserves customer filtering and attribution behavior.

Important Files Changed

Filename Overview
openmeter/streaming/clickhouse/queryhelper.go Replaces SQL-literal attribution-map construction with parallel bound arrays while preserving empty-attribution handling.
openmeter/streaming/clickhouse/queryhelper_test.go Verifies that quote-containing attribution values remain outside generated SQL and appear only in bound arguments.
e2e/streaming_test.go Exercises ingestion and event lookup through customer ID, customer key, and each usage-attribution subject key.
openmeter/streaming/clickhouse/event_query_test.go Updates event-query SQL and argument expectations for the bound attribution arrays.
openmeter/streaming/clickhouse/event_query_v2_test.go Updates V2 event-query expectations for bound customer-attribution maps.
openmeter/streaming/clickhouse/meter_query_test.go Updates grouped meter-query expectations to account for the two new array parameters.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  C[Customer attribution records] --> K[Subject-key array]
  C --> I[Customer-ID array]
  K --> B[Bound ClickHouse parameters]
  I --> B
  B --> M["mapFromArrays(keys, IDs)"]
  E[Event subject] --> L[Map lookup]
  M --> L
  L --> R[Attributed customer_id]
Loading

Reviews (1): Last reviewed commit: "test(e2e): test meter query by customer" | Re-trigger Greptile

Context used:

@coderabbitai

coderabbitai Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change parameterizes ClickHouse customer attribution mappings with mapFromArrays, updates query expectations, adds injection-focused query-builder coverage, and adds an end-to-end test for customer and attribution-key event filtering.

Changes

Customer attribution filtering

Layer / File(s) Summary
Parameterized attribution mapping and query coverage
openmeter/streaming/clickhouse/queryhelper.go, openmeter/streaming/clickhouse/queryhelper_test.go, openmeter/streaming/clickhouse/*_query_test.go
Customer attribution lookup now uses bound subject and customer ID arrays with mapFromArrays. Query tests verify the generated SQL and argument order, including potentially injectable key values.
End-to-end event filtering
e2e/streaming_test.go
The new test ingests events for one customer and two subject keys, then verifies customer-ID and attribution-key filters with eventual-consistency polling.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

Possibly related PRs

Suggested labels: release-note/bug-fix

Suggested reviewers: tothandras, borbelyr-kong, turip

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: binding customer attribution map values in streaming queries.
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 💡 2
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/ch-sqli

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.

@chrisgacsal chrisgacsal added the release-note/bug-fix Release note: Bug Fixes label Aug 4, 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.

🧹 Nitpick comments (1)
openmeter/streaming/clickhouse/queryhelper.go (1)

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

Document the array-alignment invariant.

The added loop comments repeat visible append operations. Replace them with one comment that states both arrays must remain index-aligned for the mapping. Keep the empty-attribution explanation because it records intentional behavior.

Proposed cleanup
 var subjectKeys, customerIDs []string
 
-// For each customer, we map event subjects to customer ids
+// mapFromArrays pairs elements by index, so both slices must remain aligned.
 for _, customer := range customers {
 	usageAttribution := customer.GetUsageAttribution()
 
-	// We map the customer key to the customer id if it exists
 	if usageAttribution.Key != nil {
 		subjectKeys = append(subjectKeys, *usageAttribution.Key)
 		customerIDs = append(customerIDs, usageAttribution.ID)
 	}
 
-	// We map each subject key to the customer id
 	for _, subjectKey := range usageAttribution.SubjectKeys {
 		subjectKeys = append(subjectKeys, subjectKey)
 		customerIDs = append(customerIDs, usageAttribution.ID)
 	}
 }
 
-// Select the customer id column
 query = query.SelectMore(fmt.Sprintf(

As per coding guidelines, comments must explain non-obvious intent and must not narrate visible conditions.

🤖 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 `@openmeter/streaming/clickhouse/queryhelper.go` around lines 23 - 54, Replace
the two comments describing the customer-key and subject-key append loops with
one comment stating that subjectKeys and customerIDs must remain index-aligned
for mapFromArrays. Keep the existing comment explaining the intentional
empty-attribution behavior, and leave the append logic unchanged.

Source: Coding guidelines

🤖 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 `@openmeter/streaming/clickhouse/queryhelper.go`:
- Around line 23-54: Replace the two comments describing the customer-key and
subject-key append loops with one comment stating that subjectKeys and
customerIDs must remain index-aligned for mapFromArrays. Keep the existing
comment explaining the intentional empty-attribution behavior, and leave the
append logic unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 3b9c835a-b2aa-4d09-8cb6-fbd9b1024d8f

📥 Commits

Reviewing files that changed from the base of the PR and between 0ba1299 and a1facf9.

📒 Files selected for processing (6)
  • e2e/streaming_test.go
  • openmeter/streaming/clickhouse/event_query_test.go
  • openmeter/streaming/clickhouse/event_query_v2_test.go
  • openmeter/streaming/clickhouse/meter_query_test.go
  • openmeter/streaming/clickhouse/queryhelper.go
  • openmeter/streaming/clickhouse/queryhelper_test.go

@chrisgacsal
chrisgacsal merged commit ed04696 into main Aug 4, 2026
28 of 31 checks passed
@chrisgacsal
chrisgacsal deleted the fix/ch-sqli branch August 4, 2026 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL injection through customer usageAttribution subjectKeys (and key) into the shared ClickHouse meter/event query

2 participants