Skip to content

fix(ask): sanitize the echoed question against quick-action execution (#2629) - #2630

Closed
IsmaelMartinez wants to merge 1 commit into
The-PR-Agent:mainfrom
IsmaelMartinez:fix/ask-question-echo-sanitization
Closed

IsmaelMartinez wants to merge 1 commit into
The-PR-Agent:mainfrom
IsmaelMartinez:fix/ask-question-echo-sanitization

Conversation

@IsmaelMartinez

Copy link
Copy Markdown
Collaborator

The /ask tool sanitized the model answer against line-leading slash commands but echoed the user's raw question into the same bot comment, so GitLab quick actions in the question ran under the bot's account. This extracts the rewrites (including the \r/ case GitLab normalizes to line start) into _sanitize_slash_commands, applies it to both the echoed question and the answer, and reuses it in pr_line_questions.py where the \r/ rewrite was missing. Fixes #2629.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the bug label Aug 11, 2026
@qodo-free-for-open-source-projects

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

Sanitize /ask question echo to prevent GitLab quick-action execution

🐞 Bug fix 🧪 Tests 🕐 10-20 Minutes

Grey Divider

AI Description

• Extract slash-command neutralization into a shared helper.
• Sanitize both the model answer and the echoed user question in /ask comments.
• Reuse the helper in line-question comments and add regression tests for \r/ cases.
Diagram

graph TD
  U([User question]) --> PQ["PRQuestions"] --> S["_sanitize_slash_commands"] --> C["Git provider comment"]
  PQ --> GA["Model answer"] --> S
  PLQ["PR_LineQuestions"] --> S
  C --> GL{{"GitLab quick actions"}}
  subgraph Legend
    direction LR
    _usr([Input]) ~~~ _mod["Module"] ~~~ _ext{{External}}
  end
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Wrap echoed question/answer in fenced code blocks
  • ➕ Prevents GitLab from interpreting quick actions in most markdown renderers
  • ➕ Preserves original text without character-level rewrites
  • ➖ Changes comment formatting and readability (especially for natural language answers)
  • ➖ May not fully prevent all platform-specific command parsing edge cases
2. Centralize sanitization in the Git provider comment-posting layer
  • ➕ Guarantees all outgoing bot comments are protected, not just /ask paths
  • ➕ Avoids relying on each tool to remember sanitization
  • ➖ Harder to scope exceptions where slash-leading text is desired
  • ➖ Requires broader refactor and careful auditing of all comment entry points

Recommendation: The PR’s approach (a shared helper used by both PRQuestions and PR_LineQuestions) is the right minimal fix: it directly addresses the injection vector (line-leading '/' including GitLab’s \r normalization) while keeping behavior stable. Consider a future follow-up to apply similar sanitization centrally at the comment publishing boundary if other tools also echo user-provided text.

Files changed (3) +65 / -11

Bug fix (2) +12 / -10
pr_line_questions.pyReuse shared slash-command sanitization for line answers +2/-4

Reuse shared slash-command sanitization for line answers

• Imports and applies _sanitize_slash_commands to the generated model answer before posting line-level comments. This replaces the previous newline-only sanitization logic to also cover \r/ cases consistently.

pr_agent/tools/pr_line_questions.py

pr_questions.pySanitize echoed /ask question and extract sanitization helper +10/-6

Sanitize echoed /ask question and extract sanitization helper

• Introduces _sanitize_slash_commands to neutralize line-leading slash commands (including \r/). Uses the helper for both the model answer and the echoed question in the generated /ask comment body.

pr_agent/tools/pr_questions.py

Tests (1) +53 / -1
test_pr_questions_helpers.pyAdd regression tests for slash-command sanitization and question echo +53/-1

Add regression tests for slash-command sanitization and question echo

• Adds unit tests for _sanitize_slash_commands across leading '/', newline '/', and carriage-return '/' inputs. Adds tests asserting that the echoed question in /ask output is sanitized while mid-line '/merge' mentions remain unchanged.

tests/unittest/test_pr_questions_helpers.py

@qodo-free-for-open-source-projects

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.
Tip of the day
💡 Did you know, you can group findings by type and pick your Finding display, from Minimal to Full

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@IsmaelMartinez

Copy link
Copy Markdown
Collaborator Author

Note: this overlaps #2628 in _prepare_pr_answer and the test file — whichever lands second, I'll rebase the other promptly.

@qodo-code-review

qodo-code-review Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Informational

1. Private helper shared cross-module 🐞 Bug ⚙ Maintainability
Description
PR_LineQuestions imports and depends on _sanitize_slash_commands from pr_questions.py, but the
leading underscore indicates a private API and makes future refactors more likely to silently break
this integration. This is now a de-facto shared utility and should be made an explicit public helper
(or moved to a shared utils module) to reduce maintenance risk.
Code

pr_agent/tools/pr_line_questions.py[20]

+from pr_agent.tools.pr_questions import _sanitize_slash_commands
Relevance

●● Moderate

No direct precedent on private-helper API exposure; team accepts naming/refactor nits but this is
subjective architecture choice.

PR-#2231
PR-#2598

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The PR introduces _sanitize_slash_commands as a top-level helper in pr_questions.py and then
imports it from pr_line_questions.py, meaning other code now depends on a symbol explicitly named
as private.

pr_agent/tools/pr_questions.py[18-23]
pr_agent/tools/pr_line_questions.py[1-21]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`_sanitize_slash_commands` is imported by another module (`pr_line_questions.py`) but is named like a private function (leading underscore). This creates an implicit, fragile API surface.

## Issue Context
The sanitizer is now used across multiple tools to prevent provider quick actions from being triggered in published comments, so it’s a shared, security-relevant helper.

## Fix Focus Areas
- pr_agent/tools/pr_questions.py[18-23]
- pr_agent/tools/pr_line_questions.py[20-20]

## Suggested change
- Rename `_sanitize_slash_commands` to `sanitize_slash_commands` (public), and update imports/call sites.
- (Optional) Move it to a small shared module (e.g., `pr_agent/tools/sanitize.py` or `pr_agent/algo/text_safety.py`) to avoid tool-to-tool coupling.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Grey Divider

Context sources
✅ Compliance rules (platform): 34 rules
Review mode: 🧠 Deep: This push contains substantial, non-mechanical logic changes across AI handlers, run details, providers, code suggestions, and question sanitization, creating many independent paths where redundant review can catch subtle regressions.

Grey Divider

Tip of the day
💡 Did you know, you can ask Qodo to dismiss a finding you disagree with, with your reason on record

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Previous reviews

Review updated until commit 077f832 🧠 Deep

Results up to commit fa9d103 ⚖️ Balanced


🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 📜 Skill insights (0)


Informational
1. Private helper shared cross-module 🐞 Bug ⚙ Maintainability
Description
PR_LineQuestions imports and depends on _sanitize_slash_commands from pr_questions.py, but the
leading underscore indicates a private API and makes future refactors more likely to silently break
this integration. This is now a de-facto shared utility and should be made an explicit public helper
(or moved to a shared utils module) to reduce maintenance risk.
Code

pr_agent/tools/pr_line_questions.py[20]

+from pr_agent.tools.pr_questions import _sanitize_slash_commands
Relevance

●● Moderate

No direct precedent on private-helper API exposure; team accepts naming/refactor nits but this is
subjective architecture choice.

PR-#2231
PR-#2598

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The PR introduces _sanitize_slash_commands as a top-level helper in pr_questions.py and then
imports it from pr_line_questions.py, meaning other code now depends on a symbol explicitly named
as private.

pr_agent/tools/pr_questions.py[18-23]
pr_agent/tools/pr_line_questions.py[1-21]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`_sanitize_slash_commands` is imported by another module (`pr_line_questions.py`) but is named like a private function (leading underscore). This creates an implicit, fragile API surface.

## Issue Context
The sanitizer is now used across multiple tools to prevent provider quick actions from being triggered in published comments, so it’s a shared, security-relevant helper.

## Fix Focus Areas
- pr_agent/tools/pr_questions.py[18-23]
- pr_agent/tools/pr_line_questions.py[20-20]

## Suggested change
- Rename `_sanitize_slash_commands` to `sanitize_slash_commands` (public), and update imports/call sites.
- (Optional) Move it to a small shared module (e.g., `pr_agent/tools/sanitize.py` or `pr_agent/algo/text_safety.py`) to avoid tool-to-tool coupling.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Results up to commit 240350f 🧠 Deep


No changes from previous review

Grey Divider

Qodo Logo

@IsmaelMartinez
IsmaelMartinez force-pushed the fix/ask-question-echo-sanitization branch from fa9d103 to 240350f Compare August 26, 2026 14:40
@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit 240350f

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit 077f832

@IsmaelMartinez

Copy link
Copy Markdown
Collaborator Author

Hi @naorpeled, ready and freshly rebased onto today's main. /ask echoes the question back verbatim, so a line-leading /improve in it runs as a quick action under the bot's account; this neutralises that. Closes #2629.

@IsmaelMartinez
IsmaelMartinez force-pushed the fix/ask-question-echo-sanitization branch from 077f832 to 4cf5b82 Compare August 30, 2026 08:58
@qodo-code-review

qodo-code-review Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Code Review by Qodo

Grey Divider

Sorry, something went wrong

We weren't able to complete the code review on our side. Please try again manually by commenting /agentic_review on this PR.

Grey Divider

Qodo Logo

@IsmaelMartinez

Copy link
Copy Markdown
Collaborator Author

Closing here: this is tracked in private advisory GHSA-wwwp-394c-5c69, where the same fix is staged for the maintainers to merge, so a public PR is no longer the route. Thanks for the patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/ask republishes the raw question, letting line-leading quick actions run under the bot's account

2 participants