Skip to content

Commit

Permalink
Implement WASM_BINDGEN_TEST_NO_ORIGIN_ISOLATION (#3807)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Jan 23, 2024
1 parent d4cc943 commit aa65ab5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* Accept the `--skip` flag with `wasm-bindgen-test-runner`.
[#3803](https://github.com/rustwasm/wasm-bindgen/pull/3803)

* Introduce environment variable `WASM_BINDGEN_TEST_NO_ORIGIN_ISOLATION` to disable origin isolation for `wasm-bindgen-test-runner`.
[#3807](https://github.com/rustwasm/wasm-bindgen/pull/3807)

### Changed

* Stabilize `ClipboardEvent`.
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ fn main() -> anyhow::Result<()> {
&args,
&tests,
test_mode,
std::env::var("WASM_BINDGEN_TEST_NO_ORIGIN_ISOLATION").is_err(),
)
.context("failed to spawn server")?;
let addr = srv.server_addr();
Expand Down
19 changes: 14 additions & 5 deletions crates/cli/src/bin/wasm-bindgen-test-runner/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) fn spawn(
args: &[OsString],
tests: &[String],
test_mode: TestMode,
isolate_origin: bool,
) -> Result<Server<impl Fn(&Request) -> Response + Send + Sync>, Error> {
let mut js_to_execute = String::new();

Expand Down Expand Up @@ -291,7 +292,14 @@ pub(crate) fn spawn(
"<script src='run.js' type=module></script>",
)
};
return set_isolate_origin_headers(Response::from_data("text/html", s));

let mut response = Response::from_data("text/html", s);

if isolate_origin {
set_isolate_origin_headers(&mut response)
}

return response;
}

// Otherwise we need to find the asset here. It may either be in our
Expand All @@ -304,7 +312,10 @@ pub(crate) fn spawn(
// Make sure browsers don't cache anything (Chrome appeared to with this
// header?)
response.headers.retain(|(k, _)| k != "Cache-Control");
set_isolate_origin_headers(response)
if isolate_origin {
set_isolate_origin_headers(&mut response)
}
response
})
.map_err(|e| anyhow!("{}", e))?;
return Ok(srv);
Expand Down Expand Up @@ -346,7 +357,7 @@ pub(crate) fn spawn(
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy#certain_features_depend_on_cross-origin_isolation
* https://security.googleblog.com/2018/07/mitigating-spectre-with-site-isolation.html
*/
fn set_isolate_origin_headers(mut response: Response) -> Response {
fn set_isolate_origin_headers(response: &mut Response) {
response.headers.push((
Cow::Borrowed("Cross-Origin-Opener-Policy"),
Cow::Borrowed("same-origin"),
Expand All @@ -355,6 +366,4 @@ fn set_isolate_origin_headers(mut response: Response) -> Response {
Cow::Borrowed("Cross-Origin-Embedder-Policy"),
Cow::Borrowed("require-corp"),
));

response
}

0 comments on commit aa65ab5

Please sign in to comment.