Skip to content

GH#3706: Fix critical security findings in code-audit-helper.sh#4069

Merged
marcusquinn merged 1 commit intomainfrom
bugfix/t3706-code-audit-security-fixes
Mar 10, 2026
Merged

GH#3706: Fix critical security findings in code-audit-helper.sh#4069
marcusquinn merged 1 commit intomainfrom
bugfix/t3706-code-audit-security-fixes

Conversation

@marcusquinn
Copy link
Owner

@marcusquinn marcusquinn commented Mar 10, 2026

Summary

Details

Fix 1 — SQL injection via unvalidated auto-detected PR number (HIGH)

Line 623 calls gh pr view --json number -q .number and assigns the result directly to pr_number. While the --pr CLI argument was already validated (line 611), the auto-detected path bypassed validation entirely. If gh returned unexpected output (error string leaking to stdout), it would flow directly into SQL interpolation on line 638.

Added numeric regex validation (^[0-9]+$) immediately after the gh pr view assignment, defaulting to 0 on failure.

Fix 2 — SQL file corruption from multi-line descriptions (MEDIUM)

sql_escape() only escaped single quotes for SQLite. Multi-line descriptions (from API responses or code comments) containing literal newlines would split INSERT statements across multiple lines in the SQL file generated by import_coderabbit_findings, causing parse errors.

Added $'\n' and $'\r' replacement with spaces before the quote escaping step.

Verification

  • bash -n: PASS
  • shellcheck -x: zero violations
  • Both fixes are minimal, targeted, and consistent with existing validation patterns in the file

Closes #3706

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved data handling to prevent corruption from multiline input
    • Enhanced PR auto-detection with validation and fallback handling for edge cases

…ewlines (GH#3706)

- Add numeric validation for auto-detected pr_number from gh pr view,
  preventing SQL injection when gh returns unexpected non-numeric output
- Add newline/CR stripping in sql_escape() to prevent multi-line SQL
  corruption in import_coderabbit_findings line-by-line INSERT generation

Closes #3706
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 10, 2026

Walkthrough

The pull request addresses SQL injection vulnerabilities in the code-audit helper script by implementing input normalization in sql_escape() to handle multiline SQL safely and adding numeric validation guards for auto-detected PR numbers to prevent non-numeric values from corrupting the audit workflow.

Changes

Cohort / File(s) Summary
SQL Injection & Input Validation
.agents/scripts/code-audit-helper.sh
Enhanced sql_escape() to normalize multiline input by replacing newlines with spaces and removing carriage returns. Added PR auto-detection fallback: validates detected PR number is numeric, logs warning if invalid, defaults to 0 to prevent non-numeric values from altering control flow.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

bug, code-reviews-actioned

Poem

🛡️ Newlines now tamed with spaces and care,
SQL injection schemes vanish in air,
Numeric guards stand at the gate,
Invalid inputs meet their fate,
Zero debt maintained, quality stays A-grade! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR addresses only 2 of 8 documented coding requirements from #3706: SQL injection fix for auto-detected pr_number and sql_escape() newline handling. Missing: backslash un-escape removal, explicit db() return, array-based service iteration, character-safe truncation, generic API collector refactoring, and input validation at all flagged locations. Complete remaining critical security fixes: implement backslash un-escape removal in sql_escape(), add explicit db() return, refactor service iteration to use arrays, replace head -c with cut -c, consolidate duplicate API collectors, and validate all user-provided IDs (run_id, limit) per issue requirements.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically identifies the main change: fixing critical security findings in code-audit-helper.sh, directly referenced by issue #3706.
Out of Scope Changes check ✅ Passed All changes are directly within scope of issue #3706: PR auto-detection numeric validation and sql_escape() newline/carriage-return handling align with documented security requirements.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bugfix/t3706-code-audit-security-fixes

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

🔍 Code Quality Report

�[0;35m[MONITOR]�[0m Code Review Monitoring Report

�[0;34m[INFO]�[0m Latest Quality Status:
SonarCloud: 0 bugs, 0 vulnerabilities, 396 code smells

�[0;34m[INFO]�[0m Recent monitoring activity:
Tue Mar 10 12:00:31 UTC 2026: Code review monitoring started
Tue Mar 10 12:00:32 UTC 2026: SonarCloud - Bugs: 0, Vulnerabilities: 0, Code Smells: 396

📈 Current Quality Metrics

  • BUGS: 0
  • CODE SMELLS: 396
  • VULNERABILITIES: 0

Generated on: Tue Mar 10 12:00:34 UTC 2026


Generated by AI DevOps Framework Code Review Monitoring

@sonarqubecloud
Copy link

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
.agents/scripts/code-audit-helper.sh (1)

260-534: Note: API collector deduplication remains as future refactor opportunity.

Issue #3706 also mentioned reducing duplicated code across collect_sonarcloud, collect_codacy, and collect_codefactor by introducing a generic collect_from_api helper. This is correctly deferred from this security-focused PR but worth tracking.

The three collectors share a common pattern:

  • Token presence check → API call → jq parsing → SQL file generation → batch import

A future refactor could extract this into a reusable helper accepting service name, URL builder, token variable, and jq filter. Not blocking for this PR.

Would you like me to open a follow-up issue to track the API collector deduplication refactor?

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.agents/scripts/code-audit-helper.sh around lines 260 - 534, The collectors
collect_sonarcloud, collect_codacy, and collect_codefactor duplicate the same
token-check → API call → jq parsing → SQL generation → db import flow; create a
follow-up issue (tracking to `#3706`) that proposes a generic collect_from_api
helper to accept service name, token/env var, URL builder, request headers/body,
and jq filter file to centralize token validation, curl invocation, jq-to-SQL
templating, tempfile cleanup (mktemp + _save_cleanup_scope/push_cleanup), and
batch db import so these three functions can be refactored to call the shared
helper.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In @.agents/scripts/code-audit-helper.sh:
- Around line 260-534: The collectors collect_sonarcloud, collect_codacy, and
collect_codefactor duplicate the same token-check → API call → jq parsing → SQL
generation → db import flow; create a follow-up issue (tracking to `#3706`) that
proposes a generic collect_from_api helper to accept service name, token/env
var, URL builder, request headers/body, and jq filter file to centralize token
validation, curl invocation, jq-to-SQL templating, tempfile cleanup (mktemp +
_save_cleanup_scope/push_cleanup), and batch db import so these three functions
can be refactored to call the shared helper.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: be52573b-8cbf-4c35-8b5e-d91aac391df2

📥 Commits

Reviewing files that changed from the base of the PR and between d8c06bb and 4704765.

📒 Files selected for processing (1)
  • .agents/scripts/code-audit-helper.sh

@marcusquinn marcusquinn merged commit 7cc12b0 into main Mar 10, 2026
28 checks passed
@marcusquinn marcusquinn deleted the bugfix/t3706-code-audit-security-fixes branch March 10, 2026 12:11
@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request implements crucial fixes to the code-audit-helper.sh script, enhancing its security and data integrity. The changes prevent potential SQL injection vulnerabilities arising from unvalidated auto-detected pull request numbers and resolve issues with SQL file corruption caused by multi-line input in descriptions.

Highlights

  • SQL Injection Prevention: Validated auto-detected PR numbers from gh pr view to prevent SQL injection if gh returns unexpected non-numeric output, addressing a high-severity finding.
  • SQL Data Corruption Fix: Hardened the sql_escape() function by stripping newline and carriage return characters, preventing multi-line SQL corruption during line-by-line INSERT generation.
Changelog
  • .agents/scripts/code-audit-helper.sh
    • Improved sql_escape function to replace newlines and carriage returns with spaces to prevent multi-line SQL corruption.
    • Added numeric regex validation for auto-detected pull request numbers from gh pr view, defaulting to 0 on failure to prevent SQL injection.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses two security vulnerabilities in code-audit-helper.sh. The validation added for the auto-detected pull request number is a crucial fix to prevent potential SQL injection. The change to the sql_escape function to handle newlines and carriage returns hardens the script against malformed SQL generation. I have one minor suggestion to improve the handling of carriage returns for better consistency.

# Replace newlines and carriage returns with spaces to prevent
# multi-line SQL corruption in line-by-line INSERT generation
val="${val//$'\n'/ }"
val="${val//$'\r'/}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency and to correctly handle all types of line endings (like classic Mac OS \r), it's better to replace carriage returns with a space, just like you do for newlines. Currently, \r is removed, which could cause words to be merged together (e.g., word1\rword2 becomes word1word2).

Suggested change
val="${val//$'\r'/}"
val="${val//$'\r'/ }"

@github-actions
Copy link

Completed via PR #4094. Task t1429 merged to main.

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

Labels

bug Auto-created from TODO.md tag status:done Task is complete

Projects

None yet

Development

Successfully merging this pull request may close these issues.

quality-debt: .agents/scripts/code-audit-helper.sh — PR #1376 review feedback (critical)

1 participant