Skip to content

fix: narrow the language code rule to what escapes the directory - #9

Merged
muhammetsafak merged 2 commits into
mainfrom
fix/narrow-language-code-rule
Nov 25, 2026
Merged

muhammetsafak merged 2 commits into
mainfrom
fix/narrow-language-code-rule

Conversation

@muhammetsafak

Copy link
Copy Markdown
Contributor

Summary

  • The [A-Za-z0-9_-] rule from fix: reject language codes outside [A-Za-z0-9_-] #7 rejected valid codes such as sr@latin, zh.Hant and türkçe. setDefault() / change() now throw TranslatorException only for an empty code, /, \, a NUL byte, or a code made only of dots and spaces (., .., ..., .. , . .). Every other code works as in 2.0.0.
  • In directory mode the path is escaped for glob(), so a directory literally named .* no longer matches .. and loads the parent's PHP files.
  • Tests cover the rejected set, the accepted set in file and directory mode, wildcard codes, and : / wrapper-like names; docs and CHANGELOG.md (Unreleased) updated.

Why the path cannot escape

The identifier is appended to the developer's setDir() path as one component. Without a separator it cannot add a component; ./.. (and their Windows-normalised forms) are the only single components that select the directory itself or its parent. A drive letter (C:), a stream-wrapper prefix (phar:, php:) or \\?\ only has meaning at the start of a path, and that start is the developer's directory. : mid-path can only address an alternate data stream of a file inside the language directory.

Test plan

  • vendor/bin/phpunit (142 tests; 85 of them fail against 2.0.0's source: the rejections and the glob case)
  • vendor/bin/phpstan analyse, vendor/bin/php-cs-fixer fix --dry-run --diff, composer validate --strict, wiki-sync.sh --check docs

The strict [A-Za-z0-9_-] rule broke valid codes such as sr@latin, zh.Hant and
türkçe. setDefault() and change() now reject only what can leave the language
directory or select its parent: an empty code, "/", "\", a NUL byte, and codes
made only of dots and spaces ("." ".." and the forms Windows normalises to
them). Everything else loads as it did in 2.0.0.

In directory mode the path is also escaped before it reaches glob(), so a
directory literally named like a wildcard (".*") cannot match ".." and pull in
the parent's PHP files.
Translator: reject ":" on Windows (not a valid file name character there),
assert the "language code is invalid" message in the rejection tests so a
missing-file error cannot pass for them, and cover every glob escape ("*",
"?", "[") with a literal wildcard-named directory. The changelog now says that
codes pointing into a sub-directory (sub/en) are rejected, and the exception
table shows the current message.
@muhammetsafak
muhammetsafak merged commit 34dcbf6 into main Nov 25, 2026
18 checks passed
@muhammetsafak
muhammetsafak deleted the fix/narrow-language-code-rule branch November 25, 2026 16:45
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