script/dom/bindings/
principals.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::ptr::NonNull;
6
7use js::glue::{DestroyRustJSPrincipals, GetRustJSPrincipalsPrivate, JSPrincipalsCallbacks};
8use js::jsapi::{
9    JS_ReadUint32Pair, JSContext, JSPrincipals, JSStructuredCloneReader, JSStructuredCloneWriter,
10};
11use script_bindings::principals::{ServoJSPrincipals, ServoJSPrincipalsRef};
12use servo_url::MutableOrigin;
13
14use super::structuredclone::StructuredCloneTags;
15use crate::DomTypeHolder;
16
17#[allow(unused)]
18pub(crate) unsafe extern "C" fn destroy_servo_jsprincipal(principals: *mut JSPrincipals) {
19    unsafe {
20        Box::from_raw(GetRustJSPrincipalsPrivate(principals) as *mut MutableOrigin);
21        DestroyRustJSPrincipals(principals);
22    }
23}
24
25pub(crate) unsafe extern "C" fn write_jsprincipal(
26    principal: *mut JSPrincipals,
27    _cx: *mut JSContext,
28    writer: *mut JSStructuredCloneWriter,
29) -> bool {
30    let Some(principal) = NonNull::new(principal) else {
31        return false;
32    };
33    let obj = unsafe { ServoJSPrincipalsRef::from_raw_nonnull(principal) };
34    let origin = obj.origin();
35    let Ok(bytes_of_origin) = bincode::serialize(&origin) else {
36        return false;
37    };
38    let Ok(len) = bytes_of_origin.len().try_into() else {
39        return false;
40    };
41
42    unsafe {
43        if !js::jsapi::JS_WriteUint32Pair(writer, StructuredCloneTags::Principals as u32, len) {
44            return false;
45        }
46        if !js::jsapi::JS_WriteBytes(writer, bytes_of_origin.as_ptr() as _, len as usize) {
47            return false;
48        }
49    }
50
51    true
52}
53
54pub(crate) unsafe extern "C" fn read_jsprincipal(
55    _cx: *mut JSContext,
56    reader: *mut JSStructuredCloneReader,
57    principals: *mut *mut JSPrincipals,
58) -> bool {
59    let mut tag: u32 = 0;
60    let mut len: u32 = 0;
61
62    unsafe {
63        if !JS_ReadUint32Pair(reader, &mut tag as *mut u32, &mut len as *mut u32) {
64            return false;
65        }
66    }
67
68    if tag != StructuredCloneTags::Principals as u32 {
69        return false;
70    }
71    let mut bytes = vec![0u8; len as usize];
72
73    unsafe {
74        if !js::jsapi::JS_ReadBytes(reader, bytes.as_mut_ptr() as _, len as usize) {
75            return false;
76        }
77    }
78
79    let Ok(origin) = bincode::deserialize(&bytes[..]) else {
80        return false;
81    };
82    let principal = ServoJSPrincipals::new::<DomTypeHolder>(&origin);
83    unsafe { *principals = principal.as_raw() };
84    // we transferred ownership of principal to the caller
85    std::mem::forget(principal);
86    true
87}
88
89pub(crate) const PRINCIPALS_CALLBACKS: JSPrincipalsCallbacks = JSPrincipalsCallbacks {
90    write: Some(write_jsprincipal),
91    isSystemOrAddonPrincipal: Some(principals_is_system_or_addon_principal),
92};
93
94unsafe extern "C" fn principals_is_system_or_addon_principal(_: *mut JSPrincipals) -> bool {
95    false
96}
97
98// TODO is same_origin_domain equivalent to subsumes for our purposes
99pub(crate) unsafe extern "C" fn subsumes(obj: *mut JSPrincipals, other: *mut JSPrincipals) -> bool {
100    match (NonNull::new(obj), NonNull::new(other)) {
101        (Some(obj), Some(other)) => {
102            let obj = unsafe { ServoJSPrincipalsRef::from_raw_nonnull(obj) };
103            let other = unsafe { ServoJSPrincipalsRef::from_raw_nonnull(other) };
104            let obj_origin = obj.origin();
105            let other_origin = other.origin();
106            obj_origin.same_origin_domain(&other_origin)
107        },
108        (None, Some(_)) => {
109            // See https://github.com/servo/servo/issues/32999#issuecomment-2542522289 for why
110            // it's safe to consider the null principal here subsumes all others.
111            true
112        },
113        _ => {
114            warn!("Received null JSPrincipal argument.");
115            false
116        },
117    }
118}