fix: prevent PromQL injection in chaos_recommender prometheus queries#1221
fix: prevent PromQL injection in chaos_recommender prometheus queries#12211PoPTRoN wants to merge 1 commit intokrkn-chaos:mainfrom
Conversation
Review Summary by QodoPrevent PromQL injection in chaos_recommender prometheus queries
WalkthroughsDescription• Prevents PromQL injection attacks by validating namespace, pod name, and scrape duration inputs • Adds three validation functions with regex-based allowlisting for Kubernetes and Prometheus naming rules • Integrates validation checks into query functions before string interpolation • Includes comprehensive unit test suite with 26 test cases covering valid inputs and injection attempts Diagramflowchart LR
Input["User Input<br/>namespace/pod/duration"] --> Validate["Validation Functions<br/>_validate_namespace<br/>_validate_pod_name<br/>_validate_scrape_duration"]
Validate --> Check{"Regex Match<br/>Valid?"}
Check -->|No| Reject["Raise ValueError"]
Check -->|Yes| Query["Safe PromQL Query<br/>Interpolation"]
Query --> Result["Query Execution"]
File Changes1. krkn/chaos_recommender/prometheus.py
|
Code Review by Qodo
1. Global sys.modules pollution
|
c76d4c2 to
2a33533
Compare
There was a problem hiding this comment.
Pull request overview
This PR mitigates PromQL injection risk in the chaos recommender by validating user-provided inputs before interpolating them into Prometheus query strings.
Changes:
- Added allowlist-based validators for namespace, pod name, and scrape duration inputs.
- Integrated validation into
chaos_recommender/prometheus.pybefore constructing PromQL queries. - Added unit tests covering accepted and rejected inputs (including injection-shaped strings).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
krkn/chaos_recommender/prometheus.py |
Calls new validators prior to building PromQL queries (but currently introduces a runtime NameError due to a removed constant). |
krkn/chaos_recommender/validators.py |
Introduces regex-based input validators used to block unsafe PromQL interpolation. |
tests/test_chaos_recommender_prometheus.py |
Adds unit tests to verify validators reject malformed/injection-like values. |
Signed-off-by: 1PoPTRoN <vrxn.arp1traj@gmail.com>
2a33533 to
3b6a479
Compare
|
Hey @paigerube14 @chaitanyaenr, |
Type of change
Description
Namespace,
scrape_duration, and pod name values are interpolated directly into PromQL query strings via%sinchaos_recommender/prometheus.py. A malicious or malformed value likedefault"}[1m]) + sum(something_else{x="could break query syntax or probe data from other namespaces.This PR adds input validation using regex allowlisting based on Kubernetes and Prometheus naming rules:
_validate_namespace()— enforces K8s namespace format_validate_scrape_duration()— enforces Prometheus duration format (e.g.5m,1h)_validate_pod_name()— enforces K8s pod name formatInvalid inputs are rejected with a
ValueErrorbefore reaching any PromQL query.Documentation
Checklist before requesting a review
REQUIRED:
python -m unittest tests.test_chaos_recommender_prometheus -v test_dots_rejected (tests.test_chaos_recommender_prometheus.TestValidateNamespace.test_dots_rejected) ... ok test_empty_string (tests.test_chaos_recommender_prometheus.TestValidateNamespace.test_empty_string) ... ok test_ends_with_hyphen_rejected (tests.test_chaos_recommender_prometheus.TestValidateNamespace.test_ends_with_hyphen_rejected) ... ok test_injection_closing_brace (tests.test_chaos_recommender_prometheus.TestValidateNamespace.test_injection_closing_brace) ... ok test_injection_special_chars (tests.test_chaos_recommender_prometheus.TestValidateNamespace.test_injection_special_chars) ... ok test_max_length_accepted (tests.test_chaos_recommender_prometheus.TestValidateNamespace.test_max_length_accepted) ... ok test_starts_with_hyphen_rejected (tests.test_chaos_recommender_prometheus.TestValidateNamespace.test_starts_with_hyphen_rejected) ... ok test_too_long_rejected (tests.test_chaos_recommender_prometheus.TestValidateNamespace.test_too_long_rejected) ... ok test_underscores_rejected (tests.test_chaos_recommender_prometheus.TestValidateNamespace.test_underscores_rejected) ... ok test_uppercase_rejected (tests.test_chaos_recommender_prometheus.TestValidateNamespace.test_uppercase_rejected) ... ok test_valid_namespaces (tests.test_chaos_recommender_prometheus.TestValidateNamespace.test_valid_namespaces) ... ok test_empty_string (tests.test_chaos_recommender_prometheus.TestValidatePodName.test_empty_string) ... ok test_ends_with_hyphen_rejected (tests.test_chaos_recommender_prometheus.TestValidatePodName.test_ends_with_hyphen_rejected) ... ok test_injection_attempt (tests.test_chaos_recommender_prometheus.TestValidatePodName.test_injection_attempt) ... ok test_starts_with_hyphen_rejected (tests.test_chaos_recommender_prometheus.TestValidatePodName.test_starts_with_hyphen_rejected) ... ok test_uppercase_rejected (tests.test_chaos_recommender_prometheus.TestValidatePodName.test_uppercase_rejected) ... ok test_valid_pod_names (tests.test_chaos_recommender_prometheus.TestValidatePodName.test_valid_pod_names) ... ok test_empty_string (tests.test_chaos_recommender_prometheus.TestValidateScrapeDuration.test_empty_string) ... ok test_float_rejected (tests.test_chaos_recommender_prometheus.TestValidateScrapeDuration.test_float_rejected) ... ok test_injection_attempt (tests.test_chaos_recommender_prometheus.TestValidateScrapeDuration.test_injection_attempt) ... ok test_invalid_unit (tests.test_chaos_recommender_prometheus.TestValidateScrapeDuration.test_invalid_unit) ... ok test_missing_number (tests.test_chaos_recommender_prometheus.TestValidateScrapeDuration.test_missing_number) ... ok test_missing_unit (tests.test_chaos_recommender_prometheus.TestValidateScrapeDuration.test_missing_unit) ... ok test_valid_durations (tests.test_chaos_recommender_prometheus.TestValidateScrapeDuration.test_valid_durations) ... ok ---------------------------------------------------------------------- Ran 24 tests in 0.000s OK