Repository navigation
fix(streaming): bind customer attribution map values - #4848
Conversation
📝 WalkthroughWalkthroughThe change parameterizes ClickHouse customer attribution mappings with ChangesCustomer attribution filtering
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related issues
Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 2📝 Generate docstrings 💡
🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
openmeter/streaming/clickhouse/queryhelper.go (1)
23-54: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDocument the array-alignment invariant.
The added loop comments repeat visible
appendoperations. 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
📒 Files selected for processing (6)
e2e/streaming_test.goopenmeter/streaming/clickhouse/event_query_test.goopenmeter/streaming/clickhouse/event_query_v2_test.goopenmeter/streaming/clickhouse/meter_query_test.goopenmeter/streaming/clickhouse/queryhelper.goopenmeter/streaming/clickhouse/queryhelper_test.go
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
mapFromArrayswith 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
Greptile Summary
The PR removes direct SQL interpolation from ClickHouse customer-attribution maps and binds subject keys and customer IDs as parallel arrays instead.
WITH map(...)expression withmapFromArrays(?, ?)in event and meter queries.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
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]Reviews (1): Last reviewed commit: "test(e2e): test meter query by customer" | Re-trigger Greptile
Context used: