Skip to content

Commit

Permalink
Drop integrity attribute in HTML <script> and <link> tags
Browse files Browse the repository at this point in the history
Since the JS and CSS scripts are rewritten, integrity checks are failing
on purpose. Since we do not really need these integrity checks, we just
drop the attribute when rewritting the HTML
  • Loading branch information
benoit74 committed Jun 4, 2024
1 parent 1e9045c commit ed0f53e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Drop `integrity` attribute in HTML `<script>` and `<link>` tags (#298)

## [2.0.0] - 2024-06-04

### Added
Expand Down
3 changes: 3 additions & 0 deletions src/warc2zim/content_rewriting/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def handle_starttag(self, tag: str, attrs: AttrsList, *, auto_close: bool = Fals
self.transform_attrs(
attrs,
self.url_rewriter_existing if tag == "a" else self.url_rewriter_all,
["integrity"] if tag == "script" or "link" else [],
)
)

Expand Down Expand Up @@ -226,10 +227,12 @@ def transform_attrs(
self,
attrs: AttrsList,
url_rewriter: UrlRewriterProto,
exclude_attrs: list[str],
) -> str:
processed_attrs = (
self.process_attr(attr_name, attr_value, url_rewriter)
for attr_name, attr_value in attrs
if attr_name not in exclude_attrs
)
return " ".join(self.format_attr(*attr) for attr in processed_attrs)

Expand Down
14 changes: 14 additions & 0 deletions tests/test_html_rewriting.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ def test_escaped_content(escaped_content, no_js_notify):
"}</script>"
),
),
ContentForTests(
'<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"'
' integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm'
'8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw=="'
' crossorigin="anonymous" referrerpolicy="no-referrer"></script>',
'<script src="../cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"'
' crossorigin="anonymous" referrerpolicy="no-referrer"></script>',
),
ContentForTests(
'<link rel="preload" src="https://cdnjs.cloudflare.com/jquery.min.js"'
' integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm'
'8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw=="></link>',
'<link rel="preload" src="../cdnjs.cloudflare.com/jquery.min.js"></link>',
),
]
)
def js_rewrites(request):
Expand Down

0 comments on commit ed0f53e

Please sign in to comment.