Skip to content

Commit

Permalink
add headers for cross-origin-isolated to enable SharedArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick99e99 committed Aug 13, 2024
1 parent 55821ae commit 7a10447
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use {
axum::{
body,
extract::{DefaultBodyLimit, Extension, Json, Path, Query},
http::{header, HeaderValue, StatusCode, Uri},
http::{header, HeaderName, HeaderValue, StatusCode, Uri},
response::{IntoResponse, Redirect, Response},
routing::{get, post},
Router,
Expand Down Expand Up @@ -84,6 +84,8 @@ pub struct Server {
help = "Decompress encoded content. Currently only supports brotli. Be careful using this on production instances. A decompressed inscription may be arbitrarily large, making decompression a DoS vector."
)]
pub(crate) decompress: bool,
#[arg(long, help = "Disable Cross Origin Isolated Environment. [default: false]")]
pub(crate) disable_cross_origin_isolated: bool,
#[arg(long, help = "Disable JSON API.")]
pub(crate) disable_json_api: bool,
#[arg(
Expand Down Expand Up @@ -159,6 +161,7 @@ impl Server {
domain: acme_domains.first().cloned(),
index_sats: index.has_sat_index(),
json_api_enabled: !self.disable_json_api,
cross_origin_isolated: !self.disable_cross_origin_isolated,
});

let router = Router::new()
Expand Down Expand Up @@ -285,6 +288,23 @@ impl Server {
.layer(CompressionLayer::new())
.with_state(server_config.clone());

let router = if server_config.cross_origin_isolated {
router.layer(SetResponseHeaderLayer::overriding(
HeaderName::from_static("cross-origin-opener-policy"),
HeaderValue::from_static("same-origin"),
))
.layer(SetResponseHeaderLayer::overriding(
HeaderName::from_static("cross-origin-embedder-policy"),
HeaderValue::from_static("require-corp"),
))
.layer(SetResponseHeaderLayer::overriding(
HeaderName::from_static("cross-origin-resource-policy"),
HeaderValue::from_static("same-site"),
))
} else {
router
};

let router = if server_config.json_api_enabled {
router.layer(DefaultBodyLimit::disable())
} else {
Expand Down
1 change: 1 addition & 0 deletions src/subcommand/server/server_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) struct ServerConfig {
pub(crate) domain: Option<String>,
pub(crate) index_sats: bool,
pub(crate) json_api_enabled: bool,
pub(crate) cross_origin_isolated: bool,
}

impl ServerConfig {
Expand Down

0 comments on commit 7a10447

Please sign in to comment.