Skip to content

Commit

Permalink
avm2: Implement flash.system.Security pageDomain
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpie authored and Dinnerbone committed Nov 23, 2023
1 parent d3489ef commit 96059a8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/avm2/globals/flash/system/Security.as
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package flash.system {
public final class Security {
public static native function get pageDomain():String;
public static native function get sandboxType():String;

public static native function allowDomain(... domains):void;
public static native function allowInsecureDomain(... domains):void;
public static native function loadPolicyFile(url: String):void;
Expand Down
26 changes: 26 additions & 0 deletions core/src/avm2/globals/flash/system/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@ use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::avm2_stub_method;
use crate::string::AvmString;
use url::Url;

pub fn get_page_domain<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(url) = activation
.context
.page_url
.as_ref()
.and_then(|page_url| Url::parse(page_url).ok())
{
if !url.origin().is_tuple() {
tracing::warn!("flash.system.Security.pageDomain: Returning null for opaque origin");
return Ok(Value::Null);
}

let mut domain = url.origin().ascii_serialization();
domain.push('/'); // Add trailing slash that is used by Flash, but isn't part of a standard origin.
Ok(AvmString::new_utf8(activation.context.gc_context, domain).into())
} else {
tracing::warn!("flash.system.Security.pageDomain: No page-url available");
Ok(Value::Null)
}
}

pub fn get_sandbox_type<'gc>(
activation: &mut Activation<'_, 'gc>,
Expand Down
3 changes: 3 additions & 0 deletions core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ pub struct UpdateContext<'a, 'gc> {
/// The system properties
pub system: &'a mut SystemProperties,

pub page_url: &'a mut Option<String>,

/// The current instance ID. Used to generate default `instanceN` names.
pub instance_counter: &'a mut i32,

Expand Down Expand Up @@ -377,6 +379,7 @@ impl<'a, 'gc> UpdateContext<'a, 'gc> {
player: self.player.clone(),
load_manager: self.load_manager,
system: self.system,
page_url: self.page_url,
instance_counter: self.instance_counter,
avm1_shared_objects: self.avm1_shared_objects,
avm2_shared_objects: self.avm2_shared_objects,
Expand Down
12 changes: 12 additions & 0 deletions core/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ pub struct Player {

system: SystemProperties,

page_url: Option<String>,

/// The current instance ID. Used to generate default `instanceN` names.
instance_counter: i32,

Expand Down Expand Up @@ -1927,6 +1929,7 @@ impl Player {
player: self.self_reference.clone(),
load_manager,
system: &mut self.system,
page_url: &mut self.page_url,
instance_counter: &mut self.instance_counter,
storage: self.storage.deref_mut(),
log: self.log.deref_mut(),
Expand Down Expand Up @@ -2184,6 +2187,7 @@ pub struct PlayerBuilder {
player_version: Option<u8>,
quality: StageQuality,
sandbox_type: SandboxType,
page_url: Option<String>,
frame_rate: Option<f64>,
external_interface_providers: Vec<Box<dyn ExternalInterfaceProvider>>,
fs_command_provider: Box<dyn FsCommandProvider>,
Expand Down Expand Up @@ -2229,6 +2233,7 @@ impl PlayerBuilder {
player_version: None,
quality: StageQuality::High,
sandbox_type: SandboxType::LocalTrusted,
page_url: None,
frame_rate: None,
external_interface_providers: vec![],
fs_command_provider: Box::new(NullFsCommandProvider),
Expand Down Expand Up @@ -2391,6 +2396,12 @@ impl PlayerBuilder {
self
}

// Configure the embedding page's URL (if applicable)
pub fn with_page_url(mut self, page_url: Option<String>) -> Self {
self.page_url = page_url;
self
}

/// Sets and locks the player's frame rate. If None is provided, this has no effect.
pub fn with_frame_rate(mut self, frame_rate: Option<f64>) -> Self {
self.frame_rate = frame_rate;
Expand Down Expand Up @@ -2532,6 +2543,7 @@ impl PlayerBuilder {
// Misc. state
rng: SmallRng::seed_from_u64(get_current_date_time().timestamp_millis() as u64),
system: SystemProperties::new(self.sandbox_type),
page_url: self.page_url.clone(),
transform_stack: TransformStack::new(),
instance_counter: 0,
player_version,
Expand Down
1 change: 1 addition & 0 deletions desktop/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ impl ActivePlayer {
.with_fullscreen(opt.fullscreen)
.with_load_behavior(opt.load_behavior)
.with_spoofed_url(opt.spoof_url.clone().map(|url| url.to_string()))
.with_page_url(opt.spoof_url.clone().map(|url| url.to_string()))
.with_player_version(Some(opt.player_version))
.with_frame_rate(opt.frame_rate);
let player = builder.build();
Expand Down
1 change: 1 addition & 0 deletions web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ impl Ruffle {
.with_frame_rate(config.frame_rate)
// FIXME - should this be configurable?
.with_sandbox_type(SandboxType::Remote)
.with_page_url(window.location().href().ok())
.build();

let mut callstack = None;
Expand Down

0 comments on commit 96059a8

Please sign in to comment.