Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

18.6 KB
Raw Blame
use super::*;

pub(super) struct WorkerImportScriptSource {
    pub(super) final_url: Url,
    pub(super) source: String,
    pub(super) muted_errors: bool,
    resource: Option<crate::worker::WorkerScriptResource>,
}

fn resolve_import_script_url(
    state: Rc<RefCell<WorkerGlobalState>>,
    input: &str,
) -> Result<Url, WorkerImportScriptError> {
    let base_url = state.borrow().current_script_url.clone();
    let mut url = Url::parse(input)
        .or_else(|_| {
            base_url
                .as_ref()
                .ok_or(url::ParseError::RelativeUrlWithoutBase)
                .and_then(|base| base.join(input))
        })
        .map_err(|_| {
            WorkerImportScriptError::syntax(format!(
                "Failed to execute 'importScripts': invalid URL `{input}`."
            ))
        })?;
    match url.scheme() {
        "http" | "https" | "data" | "blob" => {}
        scheme => {
            return Err(WorkerImportScriptError::network(format!(
                "Failed to execute 'importScripts': URL scheme `{scheme}` is not allowed."
            )));
        }
    }
    url.set_fragment(None);
    Ok(url)
}

fn materialize_worker_import_source(
    scope: &mut v8::PinScope<'_, '_>,
    state: &Rc<RefCell<WorkerGlobalState>>,
    script_url: &Url,
) -> Result<WorkerImportScriptSource, WorkerImportScriptError> {
    if let Some(violation) = {
        let state = state.borrow();
        state.current_script_url.as_ref().and_then(|protected_url| {
            worker_content_security_policy_report_only_violation(
                &state,
                protected_url,
                script_url,
                crate::content_security_policy::ContentSecurityPolicyResourceKind::WorkerScript,
            )
        })
    } {
        dispatch_worker_content_security_policy_violation_event_for_state(scope, state, &violation);
    }
    if let Some(violation) = {
        let state = state.borrow();
        state.current_script_url.as_ref().and_then(|protected_url| {
            worker_content_security_policy_violation(
                &state,
                protected_url,
                script_url,
                crate::content_security_policy::ContentSecurityPolicyResourceKind::WorkerScript,
            )
        })
    } {
        dispatch_worker_content_security_policy_violation_event_for_state(scope, state, &violation);
        let message = worker_content_security_policy_error_message(&violation, "importScripts");
        return Err(WorkerImportScriptError::network(message));
    }
    match script_url.scheme() {
        "data" => {
            let source =
                decode_data_url_script_source(script_url, "Failed to execute 'importScripts'")
                    .map_err(WorkerImportScriptError::network)?;
            let mime_type =
                moli_web_mime::data_url_mime_type(script_url.as_str()).ok_or_else(|| {
                    WorkerImportScriptError::network(format!(
                        "Failed to execute 'importScripts': invalid data URL `{script_url}`."
                    ))
                })?;
            ensure_worker_import_script_mime_acceptable(script_url, &mime_type, source.as_bytes())?;
            Ok(WorkerImportScriptSource {
                final_url: script_url.clone(),
                source,
                muted_errors: false,
                resource: None,
            })
        }
        "blob" => {
            let (body, mime_type) = crate::blob::object_url_body_and_type(script_url.as_str())
                .ok_or_else(|| {
                    WorkerImportScriptError::network(format!(
                        "Failed to execute 'importScripts': blob URL `{}` is unavailable.",
                        script_url
                    ))
                })?;
            ensure_worker_import_script_mime_acceptable(script_url, &mime_type, body.as_bytes())?;
            Ok(WorkerImportScriptSource {
                final_url: script_url.clone(),
                source: body,
                muted_errors: false,
                resource: None,
            })
        }
        "http" | "https" => {
            let (loader, initiator_url, referrer_policy, network_partition_key) = {
                let state = state.borrow();
                (
                    state.loader.clone(),
                    state.current_script_url.clone(),
                    state.referrer_policy.clone(),
                    state.network_partition_key.clone(),
                )
            };
            fetch_worker_import_source_blocking(
                loader,
                script_url.clone(),
                initiator_url,
                referrer_policy,
                network_partition_key,
            )
            .inspect(|source| {
                if let Some(resource) = source.resource.clone() {
                    report_service_worker_imported_script_loaded(state, resource);
                }
            })
            .map_err(WorkerImportScriptError::network)
        }
        scheme => Err(WorkerImportScriptError::network(format!(
            "Failed to execute 'importScripts': URL scheme `{scheme}` is not allowed."
        ))),
    }
}

fn ensure_worker_import_script_mime_acceptable(
    script_url: &Url,
    mime_type: &str,
    body: &[u8],
) -> Result<(), WorkerImportScriptError> {
    let headers = [(
        "Content-Type".to_owned(),
        moli_fetch::header_value_from_byte_string(mime_type)
            .expect("serialized MIME types contain ByteStrings"),
    )];
    crate::worker::ensure_worker_script_mime_acceptable(script_url, &headers, body)
        .map_err(WorkerImportScriptError::network)
}

pub(super) fn fetch_worker_import_source_blocking(
    loader: crate::network::context::WorkerResourceLoader,
    script_url: Url,
    initiator_url: Option<Url>,
    referrer_policy: Option<String>,
    network_partition_key: Option<String>,
) -> Result<WorkerImportScriptSource, String> {
    let request_url = script_url.clone();
    let mut request = moli_fetch::Request::new("GET", script_url.as_str(), None, vec![])
        .map_err(|error| error.to_string())?
        .with_page_network_policy()
        .with_network_partition_key(network_partition_key);
    let request_initiator_url = initiator_url.clone();
    if let Some(ref initiator_url) = request_initiator_url {
        request = request
            .with_initiator_url(initiator_url)
            .with_request_origin(moli_url::WebOrigin::from_url(initiator_url));
    }
    if let Some(referrer_policy) = referrer_policy {
        request = request.with_script_fetch_metadata(moli_fetch::ScriptFetchRequestMetadata {
            document_referrer_policy: Some(referrer_policy),
            ..moli_fetch::ScriptFetchRequestMetadata::default()
        });
    }
    let response_started_at = Instant::now();
    let cancel_handle = FetchCancelHandle::new();
    let load = loader
        .register_load(
            ResourceLoadKind::Script,
            ResourceLoadDisposition::Ordinary,
            Some(cancel_handle.clone()),
        )
        .ok_or_else(|| "worker is shutting down".to_owned())?;
    let response = loader
        .request_client()
        .fetch_text_for_worker_blocking_boundary_with_cancel(request, cancel_handle)
        .map_err(|error| format!("failed to fetch worker import `{script_url}`: {error}"));
    load.finish();
    let response = response?;
    let response_time_ms = response_started_at
        .elapsed()
        .as_millis()
        .min(u64::MAX as u128) as u64;
    moli_fetch::ensure_http_status_success(response.final_url.as_str(), response.status, false)
        .map_err(|error| error.to_string())?;
    crate::worker::ensure_worker_script_mime_acceptable(
        &response.final_url,
        &response.headers,
        response.body_bytes(),
    )?;
    if request_initiator_url.as_ref().is_some_and(|initiator_url| {
        matches!(initiator_url.scheme(), "http" | "https")
            && !moli_url::same_origin(initiator_url, &response.final_url)
    }) {
        return Err(format!(
            "Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at '{}' failed to load.",
            response.final_url
        ));
    }
    let (head, body, body_bytes) = response.into_parts();
    let resource = crate::worker::WorkerScriptResource::from_response_parts(
        request_url,
        &head,
        &body_bytes,
        response_time_ms,
    );
    Ok(WorkerImportScriptSource {
        final_url: head.final_url,
        source: body,
        muted_errors: false,
        resource: Some(resource),
    })
}

fn report_service_worker_imported_script_loaded(
    state: &Rc<RefCell<WorkerGlobalState>>,
    resource: crate::worker::WorkerScriptResource,
) {
    let state = state.borrow();
    let WorkerGlobalKind::Service {
        registration_id,
        version_id,
        ..
    } = &state.global_kind
    else {
        return;
    };
    let _ = state
        .parent_tx
        .send(WorkerToParentMessage::ServiceWorkerImportedScriptLoaded {
            registration_id: *registration_id,
            version_id: *version_id,
            resource,
        });
}

fn evaluate_worker_script(
    scope: &mut v8::PinScope<'_, '_>,
    state: Rc<RefCell<WorkerGlobalState>>,
    script_url: &Url,
    script_source: &str,
    muted_errors: bool,
) -> Result<(), WorkerImportScriptError> {
    let previous_url = {
        let mut state = state.borrow_mut();
        state.current_script_url.replace(script_url.clone())
    };
    let outcome = (|| {
        let source = v8::String::new(scope, script_source).ok_or_else(|| {
            WorkerImportScriptError::error(
                scope,
                format!("failed to allocate worker source for `{script_url}`"),
            )
        })?;
        let origin = create_script_origin(scope, script_url.as_str());
        let try_catch = std::pin::pin!(v8::TryCatch::new(scope));
        let mut scope = try_catch.init();
        let Some(script) = v8::Script::compile(&scope, source, Some(&origin)) else {
            if muted_errors {
                return Err(WorkerImportScriptError::network(format!(
                    "Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at '{script_url}' failed to load."
                )));
            }
            let error = scope
                .exception()
                .map(|value| {
                    let message = scope.message();
                    annotate_worker_exception_location(&mut scope, value, message);
                    WorkerImportScriptError::Exception(v8::Global::new(&scope, value))
                })
                .unwrap_or_else(|| {
                    WorkerImportScriptError::error(
                        &mut scope,
                        format!("failed to compile `{script_url}`"),
                    )
                });
            return Err(error);
        };
        let _ = crate::script_execution::execute_compiled_script(&mut scope, script);
        if scope.has_caught() {
            if muted_errors {
                return Err(WorkerImportScriptError::network(format!(
                    "Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at '{script_url}' failed to load."
                )));
            }
            let error = scope
                .exception()
                .map(|value| {
                    let message = scope.message();
                    annotate_worker_exception_location(&mut scope, value, message);
                    WorkerImportScriptError::Exception(v8::Global::new(&scope, value))
                })
                .unwrap_or_else(|| {
                    WorkerImportScriptError::error(
                        &mut scope,
                        format!("failed to execute `{script_url}`"),
                    )
                });
            return Err(error);
        }
        scope.perform_microtask_checkpoint();
        crate::context_bootstrap::run_end_of_microtask_checkpoint_tasks(&mut scope);
        Ok(())
    })();
    state.borrow_mut().current_script_url = previous_url;
    outcome
}

// ─── console ────────────────────────────────────────────────────────────────

pub(in crate::worker) const WORKER_EXCEPTION_SOURCE_SLOT: &str = "__moliWorkerExceptionSource";

pub(in crate::worker) const WORKER_EXCEPTION_LINE_SLOT: &str = "__moliWorkerExceptionLine";

pub(in crate::worker) const WORKER_EXCEPTION_COLUMN_SLOT: &str = "__moliWorkerExceptionColumn";

enum WorkerImportScriptError {
    DomException { name: &'static str, message: String },
    Exception(v8::Global<v8::Value>),
}

impl WorkerImportScriptError {
    fn syntax(message: impl Into<String>) -> Self {
        Self::DomException {
            name: "SyntaxError",
            message: message.into(),
        }
    }

    fn network(message: impl Into<String>) -> Self {
        Self::DomException {
            name: "NetworkError",
            message: message.into(),
        }
    }

    fn error<'s>(scope: &mut v8::PinScope<'s, '_>, message: impl Into<String>) -> Self {
        let message = message.into();
        let value = v8_string(scope, &message)
            .map(|value| v8::Exception::error(scope, value))
            .unwrap_or_else(|| v8::Exception::error(scope, v8::String::empty(scope)));
        Self::Exception(v8::Global::new(scope, value))
    }

    fn throw(self, scope: &mut v8::PinScope<'_, '_>) {
        match self {
            Self::DomException { name, message } => {
                let exception = worker_dom_exception_value(scope, &message, name);
                scope.throw_exception(exception);
            }
            Self::Exception(value) => {
                let value = v8::Local::new(scope, value);
                scope.throw_exception(value);
            }
        }
    }
}

struct PreparedWorkerImportScript {
    final_url: Url,
    source: Option<String>,
    muted_errors: bool,
}

fn annotate_worker_exception_location<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    exception: v8::Local<'s, v8::Value>,
    message: Option<v8::Local<'s, v8::Message>>,
) {
    let Ok(object) = v8::Local::<v8::Object>::try_from(exception) else {
        return;
    };
    let source = message
        .and_then(|message| message.get_script_resource_name(scope))
        .and_then(|value| value.to_string(scope))
        .map(|value| value.to_rust_string_lossy(scope))
        .filter(|value| !value.is_empty());
    if let Some(source) = source
        && let Some(value) = v8_string(scope, &source)
    {
        set_worker_exception_location_if_missing(
            scope,
            object,
            WORKER_EXCEPTION_SOURCE_SLOT,
            value.into(),
        );
    }
    if let Some(line) = message.and_then(|message| message.get_line_number(scope)) {
        let value = v8::Number::new(scope, line as f64);
        set_worker_exception_location_if_missing(
            scope,
            object,
            WORKER_EXCEPTION_LINE_SLOT,
            value.into(),
        );
    }
    if let Some(column) = message.and_then(|message| message.get_start_column().checked_add(1)) {
        let value = v8::Number::new(scope, column as f64);
        set_worker_exception_location_if_missing(
            scope,
            object,
            WORKER_EXCEPTION_COLUMN_SLOT,
            value.into(),
        );
    }
}

fn set_worker_exception_location_if_missing<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    object: v8::Local<'s, v8::Object>,
    slot: &str,
    value: v8::Local<'s, v8::Value>,
) {
    if get_private_value(scope, object, slot).is_none() {
        set_private_value(scope, object, slot, value);
    }
}

pub(super) fn worker_import_scripts_callback<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    _rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(state) = get_worker_state(scope) else {
        return;
    };
    if state.borrow().script_kind == crate::worker::thread::WorkerScriptKind::Module {
        throw_type_error(scope, "Module scripts don't support importScripts().");
        return;
    }
    let require_trusted_types_for_script =
        crate::content_security_policy::content_security_policy_requires_trusted_types_for_script(
            &state.borrow().content_security_policies,
        );
    let mut prepared = Vec::with_capacity(args.length() as usize);
    for i in 0..args.length() {
        let Some(specifier) = crate::context_bootstrap::trusted_script_url_string_or_throw(
            scope,
            args.get(i),
            crate::content_security_policy::TrustedTypesForScriptRequirements::enforced_only(
                require_trusted_types_for_script,
            ),
            "WorkerGlobalScope importScripts",
            "importScripts",
        ) else {
            return;
        };
        let resolved_url = match resolve_import_script_url(state.clone(), &specifier) {
            Ok(url) => url,
            Err(error) => {
                error.throw(scope);
                return;
            }
        };
        let source = if matches!(resolved_url.scheme(), "data" | "blob") {
            match materialize_worker_import_source(scope, &state, &resolved_url) {
                Ok(import_source) => Some(import_source.source),
                Err(error) => {
                    error.throw(scope);
                    return;
                }
            }
        } else {
            None
        };
        prepared.push(PreparedWorkerImportScript {
            final_url: resolved_url,
            source,
            muted_errors: false,
        });
    }
    for mut script in prepared {
        if script.source.is_none() {
            let import_source =
                match materialize_worker_import_source(scope, &state, &script.final_url) {
                    Ok(result) => result,
                    Err(error) => {
                        error.throw(scope);
                        return;
                    }
                };
            script.final_url = import_source.final_url;
            script.source = Some(import_source.source);
            script.muted_errors = import_source.muted_errors;
        }
        let source = script.source.as_deref().unwrap_or_default();
        if let Err(error) = evaluate_worker_script(
            scope,
            state.clone(),
            &script.final_url,
            source,
            script.muted_errors,
        ) {
            error.throw(scope);
            return;
        }
    }
}

fn create_script_origin<'s>(scope: &mut v8::PinScope<'s, '_>, url: &str) -> v8::ScriptOrigin<'s> {
    let name = v8::String::new(scope, url).expect("worker script origin");
    v8::ScriptOrigin::new(
        scope,
        name.into(),
        0,
        0,
        false,
        -1,
        None,
        false,
        false,
        false,
        None,
    )
}