Skip to content

Reject custom In specifications that cannot be expanded safely - #247

Closed
tyrsson wants to merge 1 commit into
upd/240from
fix/in-custom-specification-tokenizer
Closed

tyrsson wants to merge 1 commit into
upd/240from
fix/in-custom-specification-tokenizer

Conversation

@tyrsson

@tyrsson tyrsson commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

The prototype above confirms one tokenizer covers both defects. Recommendation:

Replace the narrow split with a full format-string tokenizer and fail closed.

private const PLACEHOLDER = "/%%(*SKIP)(*FAIL)|%(?:(\d+)\$)?[-+ 0']*\d*(?:\.\d+)?([a-zA-Z])/";

// private static function tokenize(string $spec): Generator<int, array{literal?: string, conversion?: string, index?: int}>

Walk preg_match_all(..., PREG_OFFSET_CAPTURE | PREG_SET_ORDER), emit the literal run between matches, then the placeholder. Then:

  1. Conversion must be s (case-insensitive). Anything else throws a new forUnsupportedSpecificationConversion($spec, $conversion). That answers item 2: %d no longer reaches vsprintf to eat a value.
  2. Accept and ignore width/precision modifiers (%1$05s, %.2s, %10s). The modifier applied to a rendered value, which is now a placeholder name, so honoring it would pad :abc into garbage. Ignoring is the only correct reading; document it in the docblock.
  3. After removing %% pairs, any residual % in a literal run (including a trailing %) throws the same exception. The message must name %% as the escape, or specs containing a literal percent become unusable and the change reads as a regression.
  4. Leave %% intact for processExpression()'s vsprintf to unescape exactly once. Do not convert it inside expand(), or you double-unescape.

This deletes the substr($text, offset: 1, length: -2) surgery and gives one place to also reject mixed %s/%N$s styles (item 3), since the tokenizer already knows which style each placeholder used. Keep that as a separate exception, forMixedSpecificationPlaceholders, so the message stops blaming the argument.

Tests worth adding: %1$05s with a two-item list renders both values (proved above); %.2s and %10s behave like %s; %d throws; trailing % throws; 100%% = ANY (ARRAY[%s]) still renders one literal percent; '%s = ANY (ARRAY[%1$s])' still throws not-referenced-once.

Blast radius is nil: expand() runs only when setSpecification() was called, the class is @internal, and the default In path is untouched. These paths currently emit wrong SQL silently, so throwing is strictly better.

CustomSpecification::expand() split the specification on the two placeholder
forms it knew and passed every other character through to vsprintf. Two
consequences:

- A sprintf modifier inside a placeholder, as in %1$05s, was not recognised,
  so the placeholder reached the rendered SQL intact and vsprintf then
  formatted the bound parameter name rather than the value.
- A conversion other than %s, such as %d or %f, was left for vsprintf to
  consume, which shifted every later parameter by one.

Both now go through one tokenizer that matches a placeholder carrying any
sprintf modifier. A conversion other than the string conversion, plain and
positional placeholders mixed in one specification, and an unescaped percent
sign are all rejected with a named constructor. Modifiers are accepted and
ignored, because the value they would format is a placeholder name rather
than a value. Escaped percents are skipped by the tokenizer and left for
AbstractSql to unescape exactly once.

ArgumentPlaceholders is extracted from CustomSpecification so that the
validation does not push either class past the complexity gate.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@codecov

codecov Bot commented Nov 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

simon-mundy added a commit to simon-mundy/phpdb that referenced this pull request Nov 4, 2026
Capture a positional placeholder's argument number in the placeholder
pattern rather than slicing it out with substr(), and require a
specification to use plain (%s) or positional (%N$s) placeholders
throughout. Mixing them now throws InvalidArgumentException with its own
message. Adopted from the review of php-db#247.
@tyrsson tyrsson closed this Nov 4, 2026
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