Skip to content
Permalink
Browse files

Support module script credentials mode

  • Loading branch information
CYBAI committed Dec 16, 2019
1 parent cfad32c commit 5c9536acdc8aed056fa91e0e3cffd3e3e0b0ad03
Showing with 37 additions and 6 deletions.
  1. +15 −2 components/script/dom/htmlscriptelement.rs
  2. +22 −4 components/script/script_module.rs
@@ -38,7 +38,7 @@ use ipc_channel::ipc;
use ipc_channel::router::ROUTER;
use js::jsval::UndefinedValue;
use msg::constellation_msg::PipelineId;
use net_traits::request::{CorsSettings, Destination, Referrer, RequestBuilder};
use net_traits::request::{CorsSettings, CredentialsMode, Destination, Referrer, RequestBuilder};
use net_traits::ReferrerPolicy;
use net_traits::{FetchMetadata, FetchResponseListener, Metadata, NetworkError};
use net_traits::{ResourceFetchTiming, ResourceTimingType};
@@ -491,7 +491,18 @@ impl HTMLScriptElement {
// Step 16.
let cors_setting = cors_setting_for_element(element);

// TODO: Step 17: Module script credentials mode.
// Step 17.
let credentials_mode = match script_type {
ScriptType::Classic => None,
ScriptType::Module => Some(reflect_cross_origin_attribute(element).map_or(
CredentialsMode::CredentialsSameOrigin,
|attr| match &*attr {
"use-credentials" => CredentialsMode::Include,
"anonymous" => CredentialsMode::CredentialsSameOrigin,
_ => CredentialsMode::CredentialsSameOrigin,
},
)),
};

// TODO: Step 18: Nonce.

@@ -583,6 +594,7 @@ impl HTMLScriptElement {
url.clone(),
Destination::Script,
integrity_metadata.to_owned(),
credentials_mode.unwrap(),
);

if !r#async && was_parser_inserted {
@@ -637,6 +649,7 @@ impl HTMLScriptElement {
text.clone(),
base_url.clone(),
self.id.clone(),
credentials_mode.unwrap(),
);
},
}
@@ -55,7 +55,8 @@ use js::rust::wrappers::JS_SetPendingException;
use js::rust::CompileOptionsWrapper;
use js::rust::IntoHandle;
use js::rust::{Handle, HandleValue};
use net_traits::request::{Destination, ParserMetadata, Referrer, RequestBuilder, RequestMode};
use net_traits::request::{CredentialsMode, Destination, ParserMetadata};
use net_traits::request::{Referrer, RequestBuilder, RequestMode};
use net_traits::{FetchMetadata, Metadata};
use net_traits::{FetchResponseListener, NetworkError};
use net_traits::{ResourceFetchTiming, ResourceTimingType};
@@ -856,6 +857,8 @@ struct ModuleContext {
url: ServoUrl,
/// Destination of current module context
destination: Destination,
/// Credentials Mode of current module context
credentials_mode: CredentialsMode,
/// Indicates whether the request failed, and why
status: Result<(), NetworkError>,
/// Timing object for this resource
@@ -986,6 +989,7 @@ impl FetchResponseListener for ModuleContext {
&self.owner,
&module_tree,
self.destination.clone(),
self.credentials_mode.clone(),
);

// Resolve the request of this module tree promise directly
@@ -1096,6 +1100,7 @@ pub fn fetch_external_module_script(
url: ServoUrl,
destination: Destination,
integrity_metadata: String,
credentials_mode: CredentialsMode,
) -> Rc<Promise> {
// Step 1.
fetch_single_module_script(
@@ -1105,6 +1110,7 @@ pub fn fetch_external_module_script(
Referrer::Client,
ParserMetadata::NotParserInserted,
integrity_metadata,
credentials_mode,
None,
true,
)
@@ -1118,6 +1124,7 @@ pub fn fetch_single_module_script(
referrer: Referrer,
parser_metadata: ParserMetadata,
integrity_metadata: String,
credentials_mode: CredentialsMode,
parent_url: Option<ServoUrl>,
top_level_module_fetch: bool,
) -> Rc<Promise> {
@@ -1213,6 +1220,7 @@ pub fn fetch_single_module_script(
.referrer(Some(referrer))
.parser_metadata(parser_metadata)
.integrity_metadata(integrity_metadata.clone())
.credentials_mode(credentials_mode)
.mode(mode);

let context = Arc::new(Mutex::new(ModuleContext {
@@ -1221,6 +1229,7 @@ pub fn fetch_single_module_script(
metadata: None,
url: url.clone(),
destination: destination.clone(),
credentials_mode: credentials_mode.clone(),
status: Ok(()),
resource_timing: ResourceFetchTiming::new(ResourceTimingType::Resource),
}));
@@ -1254,6 +1263,7 @@ pub fn fetch_inline_module_script(
module_script_text: DOMString,
url: ServoUrl,
script_id: ScriptId,
credentials_mode: CredentialsMode,
) {
let global = owner.global();

@@ -1270,8 +1280,12 @@ pub fn fetch_inline_module_script(
Ok(record) => {
module_tree.set_record(record);

let descendant_results =
fetch_module_descendants_and_link(&owner, &module_tree, Destination::Script);
let descendant_results = fetch_module_descendants_and_link(
&owner,
&module_tree,
Destination::Script,
credentials_mode,
);

global.set_inline_module_map(script_id, module_tree);

@@ -1295,8 +1309,10 @@ fn fetch_module_descendants_and_link(
owner: &ModuleOwner,
module_tree: &ModuleTree,
destination: Destination,
credentials_mode: CredentialsMode,
) -> Option<Rc<Promise>> {
let descendant_results = fetch_module_descendants(owner, module_tree, destination);
let descendant_results =
fetch_module_descendants(owner, module_tree, destination, credentials_mode);

match descendant_results {
Ok(descendants) => {
@@ -1364,6 +1380,7 @@ fn fetch_module_descendants(
owner: &ModuleOwner,
module_tree: &ModuleTree,
destination: Destination,
credentials_mode: CredentialsMode,
) -> Result<Vec<Rc<Promise>>, ModuleError> {
debug!("Start to load dependencies of {}", module_tree.url.clone());

@@ -1400,6 +1417,7 @@ fn fetch_module_descendants(
Referrer::Client,
ParserMetadata::NotParserInserted,
"".to_owned(),
credentials_mode.clone(),
Some(module_tree.url.clone()),
false,
)

0 comments on commit 5c9536a

Please sign in to comment.
You can’t perform that action at this time.