← Back to Pull requests

Logs: Only query Tempo with a validated trace ID in log line details #876

Open
unknown opened this pull request with 603 lines diff across 7 files
View on GitHub
unknown unknown commented on Nov 22, 2026 19:53 UTC
Author

<!-- CURSOR_AGENT_PR_BODY_BEGIN -->
What is this feature?

A fix for the Trace section in log line details. That section now sends a Tempo lookup only when the Tempo data link's query is a bare hex trace ID (abcd1234) or an exact trace-id lookup ({ trace:id = "abcd1234" }). It sends only that hex ID. For any other link query it shows the existing "Could not retrieve trace." message and does not call the datasource.

Why do we need this feature?

LogLineDetailsTrace used getTraceIdFromTraceQlQuery(traceRef.query) ?? traceRef.query. When no trace ID could be extracted, it sent the whole interpolated link query to the linked Tempo datasource with queryType: 'traceql'. Data links and correlations put log field values into that query without escaping. So anyone who can write a log line could choose the TraceQL that runs under the viewer's Tempo credentials when the viewer expanded the Trace section, or when it reopened on its own from local storage.

The change:

  • links.ts adds getTraceIdFromLinkQuery. It returns a trimmed hex ID only when the whole query is a bare hex ID or an exact { trace:id = "<hex>" } lookup. It returns undefined for anything else, including values that are not strings.

  • LogLineDetailsTrace.tsx computes the trace ID once from the link query, and that ID is the only value used to build the request. If there is no ID, it sets the unavailable state and returns before querying. The raw link query never reaches dataSource.query.
  • Who is this feature for?

    Anyone who views logs that have Tempo data links or correlations.

    Which issue(s) does this PR fix?:

    The tests in https://github.com/shaw-andy/grafana/pull/874 prove this fix. They are left out of this PR.

    Special notes for your reviewer:

    Tests run:

  • Tests from https://github.com/shaw-andy/grafana/pull/874 (yarn jest --watchAll=false public/app/features/logs/components/panel/LogLineDetails.test.tsx -t "TraceQL link injection"):

  • - Unchanged code: 5 failed, 1 passed. Each failure shows the attacker's selector sent to Tempo as TraceQL.
    - With this fix: 6 passed.
  • yarn jest --watchAll=false public/app/features/logs/components/panel/:

  • - On the #874 branch with the fix applied: 20 suites, 476 tests passed.
    - On this branch: 20 suites, 470 tests passed.
  • New getTraceIdFromLinkQuery cases in links.test.ts try the same technique in other forms. They cover a bare ID followed by a newline and a selector, || breakouts, { true }, 0x prefixes, an empty string, and inputs that are not strings. All are rejected. Bare hex IDs and exact lookups are still accepted.

  • eslint, prettier --check and tsc --noEmit pass for the changed files.
  • Other call sites I checked: this is the only frontend code that runs a log-derived link query against Tempo automatically. explore/TraceView/.../SpanDetail/LogsLink.tsx (useHasLogs) also runs a link query automatically, but in the other direction. It probes for logs using Loki queries that the trace-to-logs settings build from span tags, behind the dynamic trace-to-logs flag. That is a different operation, and this PR does not change it.

    Please check that:

  • It works as expected from a user's perspective.

  • If this is a pre-GA feature, it is behind a feature toggle. (N/A)

  • The docs are updated. (N/A)
  • <!-- CURSOR_AGENT_PR_BODY_END -->

    <div><a href="https://cursor.com/agents/bc-5f7dd46a-00d7-4845-90b8-d292bdba38ae?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-web-light.png"><img alt="Open in Web" width="114" height="28" src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a>&nbsp;<a href="https://cursor.com/background-agent?bcId=bc-5f7dd46a-00d7-4845-90b8-d292bdba38ae&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img alt="Open in Cursor" width="131" height="28" src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a>&nbsp;</div>

    <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    High Risk

    This is a security fix: it stops untrusted log-derived link queries from being executed as TraceQL against Tempo with the viewer's credentials.

    > <sup>Reviewed by Cursor Bugbot for commit 5911913d076fa494e6d683aaf96ee21f1a68cfa0. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

    Diff: 603 lines (+452 / -151) across 7 files Reviewer: cursor[bot] Review duration: 19m
    All Vouch review confidence checks passed
    Residual risk 0.29 = Change Risk (0.55) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.55 · 603 lines diff across 7 files
    0.55
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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.383
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #876"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    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: 55.1% (MEDIUM) · Higher = larger blast radius
    55.1%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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.4% · Safe to merge
    29.4%