Repository navigation
Confine asset compiler imports consistently across LESS and SCSS - #245
Merged
Merged
Conversation
LessImportResolver confines @import resolution by colliding with the path-form import dir less.php auto-adds for the current file's directory. That entry is re-created for every file less.php parses, keyed by that file's own directory, and it is also consulted by data-uri() and image-size(). Claiming only the entry asset's directory therefore left anything imported from another directory resolving unconfined. makeResolver() now registers a resolver for each directory it admits, so the collision follows the import graph. The key-normalisation logic moves into importDirKey() so registration and the initial build share it.
The allowed-root policy applied to the LESS, CSS and JavaScript compilers was never wired into ScssCompiler, so scssphp resolved @import against the importing file's own directory with `..` traversal allowed and resolution was not bounded to the asset tree. ScssCompiler now uses HasAllowedImportRoots and installs a validator via ScssphpFilter::setImportValidator(), which requires assetic/framework ^3.2.3. Import paths configured on the filter are mirrored so they count as allowed roots, preserving legitimate cross-tree imports. Adds ScssCompilerTest, mirroring the existing LessCompilerTest coverage.
This comment was marked as resolved.
This comment was marked as resolved.
LukeTowers
force-pushed
the
fix/asset-import-confinement
branch
from
September 15, 2026 06:09
c7c92ac to
e1077a1
Compare
SCSS string literals treat a backslash as an escape, so embedding a raw Windows path in the @import statement mangled it -- scssphp never saw a resolvable target and raised a compile error instead of the import being refused by the validator. The path is now written with forward slashes. A refused import also differs by platform: scssphp either emits the statement verbatim or raises a compile error. Both are refusals, so the test accepts either and asserts only that the file is not inlined.
LESS: the resolver registered for an admitted file's directory kept only the configured roots and dropped the importing resolver's own context directory. A file imported from a subdirectory could therefore no longer import back up into the entry asset's tree (e.g. sub/child.less importing ../variables.less), which the base branch allowed. The context directory is now carried forward. SCSS: ScssphpFilter::getChildren() recurses through the override for each child, and each nested call reset the allowed root to the child's directory without restoring it. Imports following a subdirectory import were then validated against the wrong root and dropped from dependency extraction and hashAsset(). Only the outermost call now sets the root, matching filterLoad(). Also drops the redundant setAllowedImportRoots() call from the SCSS cross-tree test so it actually covers the configured-import-path mirroring.
Member
Author
|
Re the CodeRabbit nitpick on |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two gaps in how
@importresolution is bounded to the asset tree.LESS — confinement didn't follow the import graph
LessImportResolverbounds resolution by colliding with the path-form import dir less.php auto-adds for the current file's directory. That entry is re-created for every file less.php parses, keyed by that file's own directory — and it's also consulted bydata-uri()/image-size(). We only claimed the entry asset's directory, so anything imported from another directory resolved unconfined.makeResolver()now registers a resolver for each directory it admits, so the collision follows the import graph. The key-normalisation moves intoimportDirKey()so registration and the initial build share one implementation — worth keeping in one place, since getting it wrong doesn't fail loudly, it just silently stops colliding (and differs on Windows).SCSS — never wired up at all
LESS, CSS and JavaScript all got the allowed-root policy;
ScssCompilernever did. scssphp resolves@importagainst the configured import paths and the importing file's own directory, both with..traversal allowed.ScssCompilernow usesHasAllowedImportRootsand installs a validator through the new assetic hook. Import paths configured on the filter are mirrored into the allowed roots so legitimate cross-tree imports still resolve — the parent stores them privately, hence the local mirror.Tests
+9, all verified in both directions by reverting each fix and re-running:
LessCompilerTest—testBlocksTraversalFromAnImportedSubdirectoryFileandtestBlocksDataUriFileReadFromAnImportedSubdirectoryFileboth fail without the resolver change.testAllowsNestedPartialChainguards the legitimate case.ScssCompilerTest(new, mirrorsLessCompilerTest) — the three confinement tests fail without the compiler change;testAllowsLegitimateSameTreePartial,testAllowsNestedPartialChainandtestAllowsCrossTreeImportWhenRootIsWhitelistedguard normal use.Full suite: 837 tests, 3324 assertions, 0 failures (828 before).
phpcsclean — includes removing one stray blank line inLessCompilerTestthat pre-dates this branch but would now be linted as a changed file.Summary by CodeRabbit
Bug Fixes
Security
Tests