← Back to Pull requests

TRT-3122: Sippy security presubmit fails on Snyk Code SQL findings #4234

Closed
unknown closed this pull request without merging on Nov 25, 2026 17:05 UTC
View on GitHub
unknown unknown commented on Nov 24, 2026 15:52 UTC
Author

Summary

Fixes TRT-3122. The ci/prow/security presubmit was failing on Snyk Code go/Sqli findings. I went through each finding in the snyk.sarif.json from the PR #4229 security run (7 findings: 6 HIGH, 1 LOW). One root cause turned out to be a real problem and is fixed here. The rest were checked by hand and confirmed as false positives. Where practical, the code was changed so request input cannot reach the SQL text. Two reviewed false positives that restructuring could not clear are moved into their own files, which are excluded in .snyk with the reason. SQL injection checks are not disabled for the rest of the code.

Triage

| Reported location | Sink (from SARIF code flow) | Verdict | Resolution |
|---|---|---|---|
| server.go jsonJobBugsFromDB (ListFilteredJobIDs) | Filter.ToSQL OR Where | Real defect | Column validation and quoting fix (below) |
| server.go jsonJobsAnalysisFromDB (ListFilteredJobIDs) | Filter.ToSQL OR Where | Real defect | Column validation and quoting fix |
| server.go jsonJobRunsReportFromDB | applyJobRunFilters -> FilterFieldToSQL -> Where | Real defect | Column validation and quoting fix |
| server.go jsonJobRunsReportFromDB (alias path) | applyJobRunFilters -> FilterItemToSQL(alias) -> Where | False positive | Alias columns come from the constant columnAliases map |
| server.go jsonJobRunsReportFromDB (server.go:1642) | applyJobRunFilters -> FilterFieldToSQL / FilterItemToSQL / testNameFilterSQL -> Where | False positive | Fields are validated and quoted or come from the constant columnAliases map, operators from fixed switches, and all values are bound. Restructuring did not clear it, so the builders moved to pkg/api/job_runs_filters.go, which is excluded in .snyk |
| server.go jsonFeatureGateDetail (GetPromotionStatus) | UncollapsedTestReportWithStats Raw | False positive | Builder moved to its own file, which is excluded in .snyk |
| server.go jsonTestsReportFromDB | UncollapsedTestReportWithStats Raw | False positive | Same |
| pkg/api/tests.go (go/Sqli/test, LOW) | UncollapsedTestReportWithStats Raw | False positive | Same |
| server.go jsonUpdateTriage | UpdateTriage First(&t, id) (both lookups) | False positive | Explicit Where("id = ?") |

The real defect

FilterItem.FilterFieldToSQL quoted the filter column taken from the request with Go's %q. That is not SQL identifier quoting. PostgreSQL does not treat \" as an escape inside a double-quoted identifier, so a " in columnField ends the identifier early and the rest of the text is parsed as SQL. I checked this against the dev database: "x\" = 1 OR 1=1 --" parses as the identifier x\ followed by = 1 OR 1=1. The BigQuery builder (toBQStr) was worse: it stripped the quotes entirely, so the column text went into the GoogleSQL query unescaped (for example name) OR (TRUE).

Fix:

  • New filter.ValidateField / filter.ErrInvalidField: a filter column must be a simple identifier (^[A-Za-z_][A-Za-z0-9_]$). Every field the frontend and backend use already matches this pattern.

  • PostgreSQL: FilterFieldToSQL validates the column, quotes it with pq.QuoteIdentifier, and now returns an error. Filter.ToSQL returns (gorm.DB, error) like ToBQStr, so the request fails instead of running without that filter.

  • BigQuery: ToBQStr validates every column and returns (BQFilterResult, error). The tests report also calls the new Filter.ValidateFields before splitting the filter, because the collapsed query never renders the processed part.

  • ApplyFilters, FilterableDBResult, /api/tests and job analysis return the ToSQL error before building or embedding the query.

  • IsBadRequestError treats ErrInvalidField and ErrUnsupportedOperator as client errors (HTTP 400). FilterItemToSQL wraps unsupported operators with ErrUnsupportedOperator. applyJobRunFilters and the BigQuery tests report return a ValidationError.

  • Filter values were already bound parameters and still are.
  • False positives

  • processedFilterConditions already limited pushdown to allowlisted columns, took operators from a fixed map, and bound values. It now looks up the quoted column SQL in a constant table (pushdownColumns), so text from the request is used only as a lookup key and never becomes SQL.

  • lifecycleWhereClause uses a constant column reference, an operator chosen from a switch, and bound values.

  • UpdateTriage looked up a uint primary key, which GORM binds. Both lookups now use an explicit Where("id = ?", triage.ID).

  • applyJobRunFilters: alias fields resolve through the constant columnAliases map and render with FilterItemToSQL, other fields go through the validated and quoted FilterFieldToSQL, and the test name fields use testNameFilterSQL, whose status comes from the fixed testNameStatuses map. Snyk taints SQL built through the request FilterItem and does not accept ValidateField or pq.QuoteIdentifier as sanitizers, so restructuring did not clear the finding. applyJobRunFilters, testNameFilterSQL and their supporting columnAliases, testNameStatuses and isTestNameField now live in pkg/api/job_runs_filters.go. That file has a header warning that it is not scanned and must not gain new code, and .snyk excludes it with the reason. Which fields can be filtered is unchanged.

  • Snyk taints the whole TestResultsSpec from the validated release and period params, so it flags the uncollapsed report's Raw call regardless of how the fragments are built. UncollapsedTestReportWithStats now lives alone in pkg/db/query/uncollapsed_test_report.go, and .snyk excludes only that file, with the reason. Its condition builders (processedFilterConditions, lifecycleWhereClause) stay in scanned files.

  • Inline deepcode ignore comments are not used. The CI scan runs without --remote-repo-url and applies only .snyk path excludes (an earlier run on this PR reported Ignored issues: 0 with inline comments present).
  • Docs

  • pkg/api/README.md: documents the identifier rule for filter field and the HTTP 400 responses for invalid fields and unsupported operators.

  • .snyk: documents which suppression mechanism the scan applies (path excludes only) and adds the two reviewed files.
  • Validation

  • make lint (sippy-dev MCP run_lint) on the final code: passed (golangci-lint 0 issues, ESLint and npm audit passed).

  • make test (sippy-dev MCP run_tests) on the final code: passed. BDD 126/126, Go 16674 tests (32 skipped), Vitest 202/202, MCP pytest 289/289.

  • make e2e (sippy-dev MCP run_e2e, BigQuery coverage off) on the final code: passed, DONE 160 tests, 2 skipped.

  • make integration was not run locally (no container socket available); ci/prow/integration covers it.

  • New table-driven unit tests:

  • - pkg/filter/filterable_sql_test.go: ValidateField, Filter.ValidateFields; PostgreSQL rendering and field rejection in FilterFieldToSQL; rendering and the ErrUnsupportedOperator wrap in FilterItemToSQL; Filter.ToSQL AND/OR rendering through GORM dry-run with the real PostgreSQL dialect (no connection), including that an invalid column or unsupported operator returns an error; ToBQStr parameters and rejection; ApplyFilters and FilterableDBResult return ErrInvalidField.
    - pkg/db/query/cumulative_query_test.go: processedFilterConditions pushdown and allowlist behavior, including a quote-breakout field that never reaches the SQL text.
    - pkg/api/job_runs_filter_test.go: applyJobRunFilters rendering and binding for alias and base columns (including failed, which is not an alias) and a negated alias, and the ValidationError for an invalid field or an unsupported operator.
    - pkg/api/tests_test.go: the BigQuery tests report rejects an invalid field with a ValidationError for collapsed and uncollapsed queries (fails without the fix).
    - pkg/api/api_test.go: IsBadRequestError classifies ErrInvalidField as a bad request.
  • New e2e tests test/e2e/filters_test.go: on /api/tests (collapsed and uncollapsed), /api/jobs/runs and /api/jobs/analysis, a valid filter column returns 200 and a quote-breakout column returns 400. An unsupported operator returns 400 on /api/tests, a /api/jobs/runs alias column, and the job analysis job runs subquery.
  • Snyk

    The Snyk CLI is not available locally. ci/prow/security passed once pkg/api/job_runs_filters.go was excluded in .snyk. The job runs filter code in that file now matches what was there before the Snyk-only restructuring, and pkg/filter is back to its earlier shape. The next ci/prow/security run will confirm that the scan still passes.

    🤖 Generated with Claude Code

    ---
    Generated with claude-opus-5-5

    <!-- coderabbit-review -->

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    Bug Fixes
    Invalid filter fields and unsupported operators now return 400 Bad Request responses across supported API endpoints.
    Job-run filters accept only supported fields, including the documented test-name filter.
    Filter errors are reported instead of silently producing unusable queries.
    Filter values are passed safely as query parameters, while valid filters continue to support column aliases and supported operators.

    Documentation
    * Updated API guidance to explain valid filter fields, supported job-run filters, error responses, and filter value handling.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: sosiouxme Review duration: 1513m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.75) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.75 · 650 lines diff across 8 files
    0.75
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.075
    Real PR Conversation Timeline
    1 comments
    "Review active on #4234"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 75.2% (HIGH) · Higher = larger blast radius
    75.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 29.7% · Safe to merge
    29.7%