Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove upgrade_insecure_requests header in PocketIC gateway #2617

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions rs/pocket_ic_server/src/state_api/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use futures::future::Shared;
use http::{
header::{
ACCEPT_RANGES, CACHE_CONTROL, CONTENT_LENGTH, CONTENT_RANGE, CONTENT_TYPE, COOKIE, DNT,
IF_MODIFIED_SINCE, IF_NONE_MATCH, RANGE, USER_AGENT,
IF_MODIFIED_SINCE, IF_NONE_MATCH, RANGE, UPGRADE_INSECURE_REQUESTS, USER_AGENT,
},
HeaderName, Method, StatusCode, Uri,
};
Expand Down Expand Up @@ -506,6 +506,7 @@ pub(crate) struct HandlerState {
backend_client: Client<HttpConnector, Body>,
resolver: DomainResolver,
replica_url: String,
is_https: bool,
}

impl HandlerState {
Expand All @@ -514,12 +515,14 @@ impl HandlerState {
backend_client: Client<HttpConnector, Body>,
resolver: DomainResolver,
replica_url: String,
is_https: bool,
) -> Self {
Self {
http_gateway_client,
backend_client,
resolver,
replica_url,
is_https,
}
}

Expand Down Expand Up @@ -614,8 +617,15 @@ async fn handler(
req.send().await
};

let mut http_response = resp.canister_response;
if !state.is_https {
http_response
.headers_mut()
.remove(UPGRADE_INSECURE_REQUESTS);
Comment on lines +622 to +624
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamspofford-dfinity Could you please suggest how the headers should be modified here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Upgrade-Insecure-Requests should not be blocked (or rather since it's a request header it shouldn't appear in the first place)
  • Strict-Transport-Security should be blocked
  • Content-Security-Policy, if it exists, should be modified to remove the upgrade-insecure-requests; directive, if it exists

Copy link
Contributor Author

@mraszyk mraszyk Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifying the content of the CSP header looks involved to me. I see the following alternatives:

  • block CSP entirely for localhost (this would make local testing of CSP impossible though);
  • modify the content of the CSP header when deploying an asset canister to the local network.

The latter seems to be the most robust option in my opinion.

}

// Convert it into Axum response
let response = resp.canister_response.into_response();
let response = http_response.into_response();

Ok(HandlerResponse::ResponseBody(response))
}
Expand Down Expand Up @@ -968,6 +978,7 @@ impl ApiState {
backend_client,
domain_resolver,
replica_url.clone(),
http_gateway_config.https_config.is_some(),
));

let router_api_v2 = Router::new()
Expand Down