Skip to content

Commit

Permalink
Fix cssrewrite filter dealing with hostnames.
Browse files Browse the repository at this point in the history
  • Loading branch information
miracle2k committed Dec 15, 2013
1 parent d6dbc92 commit 7d76ec6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/webassets/filter/cssrewrite/__init__.py
Expand Up @@ -92,9 +92,14 @@ def replace_url(self, url):
# If path is an absolute one, keep it
parsed = urlparse.urlparse(url)
if not parsed.scheme and not parsed.path.startswith('/'):
abs_source_url = urlparse.urljoin(self.source_url, url)

# relpath() will not detect this case
if urlparse.urlparse(abs_source_url).scheme:
return abs_source_url

# rewritten url: relative path from new location (output)
# to location of referenced file (source + current url)
url = urlpath.relpath(self.output_url,
urlparse.urljoin(self.source_url, url))
url = urlpath.relpath(self.output_url, abs_source_url)

return url
13 changes: 13 additions & 0 deletions tests/test_filters.py
Expand Up @@ -682,6 +682,19 @@ def test_change_folder(self):
self.mkbundle('in.css', filters=cssrewrite, output='out.css').build()
assert self.get('out.css') == '''h1 { background: url(/new/sub/icon.png) }'''

def test_hostnames(self):
"""[Regression] Properly deal with full urls.
"""
self.env.append_path(self.path('g'), 'http://input.com/')
self.env.url = 'http://output.com/'

self.create_directories('g')
self.create_files({'g/in.css': '''h1 { background: url(sub/icon.png) }'''})

self.mkbundle('in.css', filters='cssrewrite', output='out.css').build()
self.p('out.css')
assert self.get('out.css') == '''h1 { background: url(http://input.com/sub/icon.png) }'''

def test_replace_with_cache(self):
"""[Regression] Test replace mode while cache is active.
Expand Down

3 comments on commit 7d76ec6

@karlwnw
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain why having the hostname in the urls inside css files is better ? Is it for security reason ?

@miracle2k
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I've made a mistake, this should only apply in cases where there is no other option. If file A is on http://foo.org/A.css and file B is on http://bar.org/B.css, then before this fix, it would generate a link like "../bar.org/B.css", which is broken.

@karlwnw
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I get it. My bad for not properly reading the doc. I didn't specify the Environment.url property, so with django-assets the settings.PARENT_DOMAIN is used by default. Setting the settings.ASSETS_URL value fixed it.

Please sign in to comment.