Skip to content

fix(query): tighten function name and cast type parsing in the AST - #14866

Merged
abnegate merged 3 commits into
mainfrom
fix/query-ast-identifiers
Oct 23, 2026
Merged

abnegate merged 3 commits into
mainfrom
fix/query-ast-identifiers

Conversation

@abnegate

Copy link
Copy Markdown
Member

What does this PR do?

Func and Cast nodes now only accept names and types that match the identifier and type grammar the builders already enforce for selectCast, so the parser, builder and hand-built trees agree. toAst() now throws UnsupportedException for filter methods it does not model, matching build(), instead of emitting a Raw node.

Test Plan

composer test in packages/query (5578 tests, 8 new regression tests). bin/monorepo check query (Rector, Pint, PHPStan) is clean.

Related PRs and Issues

  • None

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

🤖 Generated with Claude Code

Func and Cast nodes now only accept names and types that match the identifier and type grammar the builders already enforce for selectCast, so the parser, builder and hand-built trees agree. toAst() now throws UnsupportedException for filter methods it does not model, matching build(), instead of emitting a Raw node.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Anchor identifier regexes with \z instead of $ (Func.php:19) — The function-name check uses $ as the end anchor, which in PCRE also matches before a trailing newline, so a name like NOW passes validation (packages/query/src/AST/Call/Func.php:19).

This PR hardens the query DSL against injection through function names and cast types: AST constructors now validate identifiers before serialization, and unsupported filter expressions are rejected instead of silently emitting raw SQL.

Files Change
packages/query/src/AST/Call/Func.php Adds a regex allowlist check on the function name at construction.
packages/query/src/AST/Expression/Cast.php Adds a regex allowlist check on the cast type at construction.
packages/query/src/Builder.php Replaces the raw fallback for unsupported filter methods with an explicit UnsupportedException.
packages/query/tests/Regression/SecurityRegressionTest.php Adds regression tests covering injection payloads and validation of legitimate names/types.

One low-severity regex-anchoring nit was found; otherwise the fix is sound and all legitimate call paths were traced as unaffected.

Reviewed commit: e33df0b

@ghost

ghost commented Oct 23, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The PR hardens AST function-name and cast-type validation, rejects unsupported filter methods during AST conversion, and adds regression coverage for these cases. The latest changes centralize identifier/type checks in a shared grammar utility and cover whitespace edge cases and multi-word cast types.

Latest changes: The newest commits replace the AST nodes’ duplicated regex checks with shared Grammar validators and add cases for trailing whitespace and valid multi-word cast types.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 5
File Change
packages/query/src/AST/Call/Func.php Uses the shared grammar utility to validate AST function names.
packages/query/src/AST/Expression/Cast.php Uses the shared grammar utility to validate AST cast types.
packages/query/src/AST/Grammar.php Adds reusable identifier and type validators without regex.
packages/query/src/Builder.php Throws UnsupportedException when AST conversion encounters an unsupported filter method.
packages/query/tests/Regression/SecurityRegressionTest.php Adds regression tests for unsafe names and types, unsupported filters, whitespace boundaries, and multi-word types.
🔇 Filtered out · 1

Findings Hansi considered but did not post.

Finding Why
Align final-newline type handling with selectCast Verifier: The validator mismatch exists, but no concrete failure follows from it. selectCast() stores a raw SQL Condition rather than constructing Cast; the final LF is harmless SQL whitespace. When that SQL is parsed, Tokenizer::filter() removes whitespace before Parser::parseCastExpression() constructs Cast, so it receives 'INT', not "INT\n". Hand-built Cast nodes intentionally reject trailing whitespace, with explicit regression tests. Sharing the validator would be consistency cleanup, not a demonstrated bug or required gap in this AST change.

Merged main (d63021b); no changes to this PR's code since 02672c8 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Tier A · Looks good to merge. Summary

Comment thread packages/query/src/AST/Call/Func.php Resolved
@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/query-ast-identifiers (after).

Metric Before After Change
🚀 Requests/sec 215.34 219.07 ⚪ +1.7%
⏱️ Latency P50 78.89 ms 77.37 ms ⚪ -1.9%
⏱️ Latency P95 192.9 ms 190.1 ms ⚪ -1.5%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 77.37 190.1 13,680 219.07 -2.8
Account 158.08 307.9 720 12.14 +17.22
TablesDB 74.39 143.68 7,440 120.8 +3.81
Storage 72.6 168.56 3,600 60.54 -12.04
Functions 115.5 229.93 1,920 33.03 -4.36

Top API waits (after)

API request Max wait (ms)
account.name.update 470.72
tablesdb.rows.create 415.75
account.prefs.update 381.09
functions.variables.update 367.99
functions.delete 366.17

AGENTS.md disallows regex validation, so Func and Cast now check names and
types with strspn and string functions through a shared Grammar class. The
accepted grammar is unchanged except that a trailing newline is now
rejected: the previous pattern's `$` anchor matched before a final "\n", so
"NOW\n" was accepted as a function name and "INT\n" as a cast type.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Tier S · Looks good to merge. Summary

@abnegate
abnegate merged commit f7f087f into main Oct 23, 2026
233 of 234 checks passed
@abnegate
abnegate deleted the fix/query-ast-identifiers branch October 23, 2026 04:04
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…iers

fix(query): tighten function name and cast type parsing in the AST

(cherry picked from commit f7f087f)
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