[2.x] fix: require a scheme when declining remote LESS imports - #5065
Merged
Merged
Conversation
containImports() declines imports it treats as remote so less.php emits
them for the browser. The check matched `#^(https?:)?//#i`, whose
optional scheme also matches a bare leading `//` -- and a `//`-prefixed
string is a valid absolute path, so a local path satisfied a check meant
only for remote URLs. Declining does not end the import, so an inline
import would then be read from that path.
Require a real scheme. An `@import url('https://...')` webfont still
works, and theme/extension imports resolved through Flarum's import
directories are unaffected.
Protocol-relative imports (`@import url("//host/x")`) are now refused at
save: they cannot be told apart from a local path, and the callback only
receives the filename, not the import's inline flag, so the decision has
to be made from the path string alone.
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.
Fixes #0000
Changes proposed in this pull request:
LessCompiler::containImports()declines imports it treats as remote, so less.php emits them for the browser rather than trying to resolve them locally. The check matched#^(https?:)?//#i, where the optional scheme also matches a bare leading//. A path beginning//is a valid absolute path, so a local path could satisfy a check meant only for remote URLs — and declining doesn't end the import, so an inline import would then be read from that path.This requires a real scheme:
An
@import url('https://…')webfont still works, which is what the guard is there for, and theme/extension imports (@import "mixins.less", resolved through Flarum's import directories) are unaffected.Behaviour change: a protocol-relative import (
@import url("//host/x")) is now refused at save. It can't be distinguished from a local path from the string alone — and the string is all the callback gets (less.php passes it the filename, not the import's inline flag) — so it has to go the safe way. Nothing in core or the bundled extensions uses one; write the scheme out in full instead. Worth a changelog line.Reviewers should focus on:
Whether requiring a scheme is the right call versus keeping protocol-relative working. Distinguishing a remote host from a local path heuristically doesn't hold up — a hostname-shaped first segment is forgeable, and keying on whether the file exists makes behaviour depend on what's on disk — so the scheme is the one property decidable from the path alone.
Tests
The existing tests covered
//with non-inline imports and(inline)with single-slash paths, but not both together, which was the gap. Added:custom_less_cannot_read_a_file_via_a_double_slash_path(data provider:@import (inline),@impor (inline), three-slash) — fails on the old guard, passes on this one.a_theme_colour_cannot_read_a_file_via_a_double_slash_path— a guard on the config-variable path (which already handled this, via its own check).a_protocol_relative_import_is_refused— records the behaviour change above as intended.A docblock on an earlier test claimed the callback wouldn't be consulted for an inline import. It is consulted for every import, so the claim was wrong and it's removed.
Screenshot
N/A — no visual change. Confirmed on a local install that an
@import url('https://fonts.googleapis.com/…')webfont compiles intoforum.cssand the font loads.Necessity
LessCompileris core.)Confirmed
yarn testinjs/). (N/A.)composer test).Required changes: