Skip to content

Commit a77bc66

Browse files
committed
Merge pull request #241 from rails/detect-asset-alias
Prevent alias logical paths passed to asset_path
2 parents 6f5a37c + 25dd706 commit a77bc66

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

lib/sprockets/rails/helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ def initialize(source)
1515
end
1616
end
1717

18+
class AssetAliasUsed < StandardError
19+
def initialize(actual, expected)
20+
msg = "Asset was linked to from an alias rather than its exact path. " +
21+
"Alias resolving may not be available in production.\n" +
22+
"Use #{expected.inspect} instead of #{actual.inspect}"
23+
super(msg)
24+
end
25+
end
26+
1827
include ActionView::Helpers::AssetUrlHelper
1928
include ActionView::Helpers::AssetTagHelper
2029

@@ -80,6 +89,10 @@ def asset_digest_path(path, options = {})
8089
if environment = assets_environment
8190
if asset = environment[path]
8291
unless options[:debug]
92+
if path != asset.logical_path
93+
raise AssetAliasUsed.new(path, asset.logical_path)
94+
end
95+
8396
if !precompiled_assets.include?(asset)
8497
raise AssetNotPrecompiled.new(asset.logical_path)
8598
end

test/fixtures/bundle/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var jQuery;

test/fixtures/jquery/bower.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "jquery",
3+
"main": "./jquery.js"
4+
}

test/fixtures/jquery/jquery.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var jQuery;

test/test_helper.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,26 @@ def test_precompile_allows_links
664664
end
665665
end
666666

667+
def test_asset_path_with_index_requires_exact_logical_path
668+
@view.assets_precompile = ["bundle.js"]
669+
670+
assert @view.asset_path("bundle.js")
671+
672+
assert_raises(Sprockets::Rails::Helper::AssetAliasUsed) do
673+
assert @view.asset_path("bundle/index.js")
674+
end
675+
end
676+
677+
def test_asset_path_with_bower_requires_exact_logical_path
678+
@view.assets_precompile = ["jquery/jquery.js"]
679+
680+
assert @view.asset_path("jquery/jquery.js")
681+
682+
assert_raises(Sprockets::Rails::Helper::AssetAliasUsed) do
683+
assert @view.asset_path("jquery.js")
684+
end
685+
end
686+
667687
def test_links_image_target
668688
assert_match "logo.png", @assets['url.css'].links.to_a[0]
669689
end

0 commit comments

Comments
 (0)