Skip to content

Commit 0b81102

Browse files
author
unknown
committed
fix: sri remote url relative protocol
1 parent 4a456ec commit 0b81102

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

crates/rspack_plugin_sri/src/html.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::sync::Arc;
22

3-
use cow_utils::CowUtils;
43
use futures::future::join_all;
54
use once_cell::sync::Lazy;
65
use regex::Regex;
@@ -173,12 +172,12 @@ async fn process_tag(
173172
return Ok(None);
174173
}
175174

176-
let Some(mut tag_src) = get_tag_src(tag) else {
175+
let Some(tag_src) = get_tag_src(tag) else {
177176
return Ok(None);
178177
};
179178

180179
// A tag which is not generated by chunks should be skipped
181-
if match Url::parse(&tag_src) {
180+
let src = if match Url::parse(&tag_src) {
182181
Ok(url) => url.scheme() == "http" || url.scheme() == "https",
183182
Err(_) => tag_src.starts_with("//"),
184183
} {
@@ -188,15 +187,20 @@ async fn process_tag(
188187
let protocol_relative_public_path = HTTP_PROTOCOL_REGEX.replace(public_path, "").to_string();
189188
let protocol_relative_tag_src = HTTP_PROTOCOL_REGEX.replace(&tag_src, "").to_string();
190189
if protocol_relative_tag_src.starts_with(&protocol_relative_public_path) {
191-
tag_src = protocol_relative_tag_src
192-
.cow_replace(&protocol_relative_public_path, public_path)
193-
.into_owned();
190+
let tag_src_with_scheme = format!("http:{}", protocol_relative_tag_src);
191+
let public_path_with_scheme = if protocol_relative_public_path.starts_with("//") {
192+
format!("http:{}", protocol_relative_public_path)
193+
} else {
194+
protocol_relative_public_path.to_string()
195+
};
196+
get_asset_path(&tag_src_with_scheme, &public_path_with_scheme)
194197
} else {
195198
return Ok(None);
196199
}
197-
}
200+
} else {
201+
get_asset_path(&tag_src, public_path)
202+
};
198203

199-
let src = get_asset_path(&tag_src, public_path);
200204
if let Some(integrity) =
201205
get_integrity_checksum_for_asset(&src, integrities, normalized_integrities).await
202206
{

packages/rspack/src/builtin-plugin/SubresourceIntegrityPlugin.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export class SubresourceIntegrityPlugin extends NativeSubresourceIntegrityPlugin
200200
isUrlSrc = tagSrc.startsWith("//");
201201
}
202202

203+
let src = "";
203204
if (isUrlSrc) {
204205
if (!publicPath) {
205206
return;
@@ -210,16 +211,21 @@ export class SubresourceIntegrityPlugin extends NativeSubresourceIntegrityPlugin
210211
);
211212
const protocolRelativeTagSrc = tagSrc.replace(HTTP_PROTOCOL_REGEX, "");
212213
if (protocolRelativeTagSrc.startsWith(protocolRelativePublicPath)) {
213-
tagSrc = protocolRelativeTagSrc.replace(
214-
protocolRelativePublicPath,
215-
publicPath
214+
let tagSrcWithScheme = `http:${protocolRelativeTagSrc}`;
215+
let publicPathWithScheme = protocolRelativePublicPath.startsWith("//")
216+
? `http:${protocolRelativePublicPath}`
217+
: protocolRelativePublicPath;
218+
src = relative(
219+
publicPathWithScheme,
220+
decodeURIComponent(tagSrcWithScheme)
216221
);
217222
} else {
218223
return;
219224
}
225+
} else {
226+
src = relative(publicPath, decodeURIComponent(tagSrc));
220227
}
221228

222-
const src = relative(publicPath, decodeURIComponent(tagSrc));
223229
tag.attributes.integrity =
224230
this.getIntegrityChecksumForAsset(src) ||
225231
computeIntegrity(

0 commit comments

Comments
 (0)