Skip to content

Truly independent web browser

LibWeb: Don't crash on unknown EME session types

Shannon Bootha390d9f1a0c0
diff --git a/Libraries/LibWeb/Bindings/Rust/src/encrypted_media_extensions/algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/encrypted_media_extensions/algorithms.rs
index 0db163c97a9..7f2d80d0216 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/encrypted_media_extensions/algorithms.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/encrypted_media_extensions/algorithms.rs
@@ -12,7 +12,7 @@ use libwebcommon_rust::mime_sniff::mime_type::MimeType;
use super::encrypted_media_extensions::{CapabilitiesType, ConsentConfiguration, ConsentStatus, MediaKeyRestrictions};
use super::key_system::{ClearKeySystem, KeySystem};
use super::media_key_system_access::{
- MediaKeySystemConfiguration, MediaKeySystemMediaCapability, MediaKeysRequirement,
+ MediaKeySessionType, MediaKeySystemConfiguration, MediaKeySystemMediaCapability, MediaKeysRequirement,
};
pub(crate) fn supports_container(_container: &str) -> bool {
@@ -153,24 +153,22 @@ pub(crate) fn get_supported_capabilities_for_audio_video_type(
}
// https://w3c.github.io/encrypted-media/#dfn-is-persistent-session-type
-pub(crate) fn is_persistent_session_type(session_type: &Utf16String) -> bool {
+pub(crate) fn is_persistent_session_type(session_type: MediaKeySessionType) -> bool {
// 1. Let the session type be the specified MediaKeySessionType value.
// 2. Follow the steps for the value of session type from the following list:
-
- // * "temporary"
- if *session_type == "temporary" {
- // Return false.
- return false;
- }
-
- // * "persistent-license"
- if *session_type == "persistent-license" {
- // Return true.
- return true;
+ match session_type {
+ // * "temporary"
+ MediaKeySessionType::Temporary => {
+ // Return false.
+ false
+ }
+ // * "persistent-license"
+ MediaKeySessionType::PersistentLicense => {
+ // Return true.
+ true
+ }
}
-
- unreachable!("the session type is a MediaKeySessionType value")
}
// https://w3c.github.io/encrypted-media/#get-consent-status
@@ -295,6 +293,10 @@ pub(crate) fn get_supported_configuration_and_consent(
// 13. For each value in session types:
for session_type in &session_types {
// 1. Let session type be the value.
+ // AD-HOC: sessionTypes is a sequence of DOMString, but Is persistent session type? takes a MediaKeySessionType
+ // value. A session type that is not one is not supported, so the candidate configuration is not either.
+ // See https://github.com/w3c/encrypted-media/issues/447
+ let session_type = MediaKeySessionType::from_string(session_type)?;
// 2. If accumulated configuration's persistentState value is "not-allowed" and the Is persistent session type? algorithm
// returns true for session type return NotSupported.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/encrypted_media_extensions/media_key_system_access.rs b/Libraries/LibWeb/Bindings/Rust/src/encrypted_media_extensions/media_key_system_access.rs
index f9a786af1a0..3a8d66d9b31 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/encrypted_media_extensions/media_key_system_access.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/encrypted_media_extensions/media_key_system_access.rs
@@ -56,6 +56,25 @@ pub(crate) struct MediaKeySystemMediaCapability {
pub(crate) robustness: Utf16String,
}
+// https://w3c.github.io/encrypted-media/#dom-mediakeysessiontype
+#[derive(Clone, Copy)]
+pub(crate) enum MediaKeySessionType {
+ Temporary,
+ PersistentLicense,
+}
+
+impl MediaKeySessionType {
+ pub(crate) fn from_string(session_type: &Utf16String) -> Option<Self> {
+ if *session_type == "temporary" {
+ return Some(Self::Temporary);
+ }
+ if *session_type == "persistent-license" {
+ return Some(Self::PersistentLicense);
+ }
+ None
+ }
+}
+
// https://w3c.github.io/encrypted-media/#dom-mediakeysystemaccess
#[derive(Trace)]
#[gc(crate = libjs_rust)]
diff --git a/Tests/LibWeb/Text/expected/encrypted-media-requestMediaKeySystemAccess-unknown-session-type.txt b/Tests/LibWeb/Text/expected/encrypted-media-requestMediaKeySystemAccess-unknown-session-type.txt
new file mode 100644
index 00000000000..e2f9a746f26
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/encrypted-media-requestMediaKeySystemAccess-unknown-session-type.txt
@@ -0,0 +1,2 @@
+Unknown session type: NotSupportedError
+Configuration after an unknown session type: temporary
diff --git a/Tests/LibWeb/Text/input/encrypted-media-requestMediaKeySystemAccess-unknown-session-type.html b/Tests/LibWeb/Text/input/encrypted-media-requestMediaKeySystemAccess-unknown-session-type.html
new file mode 100644
index 00000000000..2021326c134
--- /dev/null
+++ b/Tests/LibWeb/Text/input/encrypted-media-requestMediaKeySystemAccess-unknown-session-type.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ promiseTest(async () => {
+ const videoCapabilities = [{ contentType: 'video/mp4; codecs="avc1.4d401e"' }];
+
+ try {
+ await navigator.requestMediaKeySystemAccess("org.w3.clearkey", [
+ { videoCapabilities, sessionTypes: ["persistent-licence"] },
+ ]);
+ println("FAIL: an unknown session type was supported");
+ } catch (error) {
+ println(`Unknown session type: ${error.name}`);
+ }
+
+ const access = await navigator.requestMediaKeySystemAccess("org.w3.clearkey", [
+ { label: "unknown", videoCapabilities, sessionTypes: ["persistent-licence"] },
+ { label: "temporary", videoCapabilities, sessionTypes: ["temporary"] },
+ ]);
+ println(`Configuration after an unknown session type: ${access.getConfiguration().label}`);
+ });
+</script>