Skip to content

Commit 44a7820

Browse files
committed
Raise an error when local asset files contain a ?
1 parent 7758e01 commit 44a7820

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

sphinx/builders/html/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,9 +1255,17 @@ def js_tag(js: JavaScript) -> str:
12551255

12561256

12571257
def _file_checksum(outdir: str | os.PathLike[str], filename: str | os.PathLike[str]) -> str:
1258+
filename = os.fspath(filename)
12581259
# Don't generate checksums for HTTP URIs
1259-
if '://' in str(filename):
1260+
if '://' in filename:
12601261
return ''
1262+
# Some themes and extensions have used query strings
1263+
# for a similar asset checksum feature.
1264+
# As we cannot safely strip the query string,
1265+
# raise an error to the user.
1266+
if '?' in filename:
1267+
msg = f'Local asset file paths must not contain query strings: {filename!r}'
1268+
raise ThemeError(msg)
12611269
try:
12621270
# Ensure universal newline mode is used to avoid checksum differences
12631271
with open(path.join(outdir, filename), encoding='utf-8') as f:

tests/test_build_html.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
from html5lib import HTMLParser
1212

1313
import sphinx.builders.html
14-
from sphinx.builders.html import validate_html_extra_path, validate_html_static_path
15-
from sphinx.errors import ConfigError
14+
from sphinx.builders.html import (
15+
_file_checksum,
16+
validate_html_extra_path,
17+
validate_html_static_path,
18+
)
19+
from sphinx.errors import ConfigError, ThemeError
1620
from sphinx.testing.util import strip_escseq
1721
from sphinx.util.inventory import InventoryFile
1822

@@ -1242,6 +1246,20 @@ def test_file_checksum(app):
12421246
assert '<script src="https://example.com/script.js"></script>' in content
12431247

12441248

1249+
def test_file_checksum_query_string():
1250+
with pytest.raises(ThemeError, match='Local asset file paths must not contain query strings'):
1251+
_file_checksum('', 'with_query_string.css?dead_parrots=1')
1252+
1253+
with pytest.raises(ThemeError, match='Local asset file paths must not contain query strings'):
1254+
_file_checksum('', 'with_query_string.js?dead_parrots=1')
1255+
1256+
with pytest.raises(ThemeError, match='Local asset file paths must not contain query strings'):
1257+
_file_checksum(Path.cwd(), '_static/with_query_string.css?dead_parrots=1')
1258+
1259+
with pytest.raises(ThemeError, match='Local asset file paths must not contain query strings'):
1260+
_file_checksum(Path.cwd(), '_static/with_query_string.js?dead_parrots=1')
1261+
1262+
12451263
@pytest.mark.sphinx('html', testroot='html_assets')
12461264
def test_javscript_loading_method(app):
12471265
app.add_js_file('normal.js')

0 commit comments

Comments
 (0)