Skip to content

fix: prevent SQL injection bypass in escapeIdentifiers and protectIdentifiers#10283

Open
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/escape-identifiers-bypass
Open

fix: prevent SQL injection bypass in escapeIdentifiers and protectIdentifiers#10283
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/escape-identifiers-bypass

Conversation

@gr8man
Copy link
Copy Markdown

@gr8man gr8man commented Jun 6, 2026

Description
This PR fixes a critical SQL injection bypass vulnerability within BaseConnection::escapeIdentifiers() and BaseConnection::protectIdentifiers().

Previously, the framework blindly bypassed identifier escaping if it encountered a parenthesis ( or a single quote ' (relying on strcspn and str_contains), assuming it was a valid SQL function or subquery. This allowed attackers to inject malformed SQL strings (e.g., COUNT(id) OR 1=1 or unbalanced parentheses) that evaded escaping entirely.

Changes made:

  • Removed the unsafe str_contains and strcspn checks.
  • Introduced a strict isSafeToBypassEscape() regex validation that ensures an identifier is either a valid balanced function call (with optional safe aliases) or a proper string literal before bypassing the escape logic.
  • Updated database tests to verify that malformed and malicious SQL injections (e.g., trailing operators, unbalanced parentheses) are now correctly caught and safely escaped as standard identifiers.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated

Implemented strict regex validation for SQL function calls and string literals in BaseConnection::isSafeToBypassEscape() to prevent SQL injection vulnerabilities. Updated tests to match the new secure behavior and added explicit SQL injection test cases.
@mergeable
Copy link
Copy Markdown

mergeable Bot commented Jun 6, 2026

Hi there, gr8man! 👋

Thank you for sending this PR!

We expect the following in all Pull Requests (PRs).

Important

We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works.

If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work
on the framework than you do. Please make it as painless for your contributions to be included as possible.

See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md

Sincerely, the mergeable bot 🤖

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant