diff --git a/dom/html/HTMLMediaElement.cpp b/dom/html/HTMLMediaElement.cpp
index 5a4377d14053..9644b24e4cce 100644
--- a/dom/html/HTMLMediaElement.cpp
+++ b/dom/html/HTMLMediaElement.cpp
@@ -3205,7 +3205,13 @@ nsresult HTMLMediaElement::FinishDecoderSetup(MediaDecoder* aDecoder,
#ifdef MOZ_EME
if (mMediaKeys) {
- mDecoder->SetCDMProxy(mMediaKeys->GetCDMProxy());
+ if (mMediaKeys->GetCDMProxy()) {
+ mDecoder->SetCDMProxy(mMediaKeys->GetCDMProxy());
+ } else {
+ // CDM must have crashed.
+ ShutdownDecoder();
+ return NS_ERROR_FAILURE;
+ }
}
#endif
@@ -5496,6 +5502,12 @@ HTMLMediaElement::SetMediaKeys(mozilla::dom::MediaKeys* aMediaKeys,
// 5.3. If mediaKeys is not null, run the following steps:
if (aMediaKeys) {
+ if (!aMediaKeys->GetCDMProxy()) {
+ promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR,
+ NS_LITERAL_CSTRING("CDM crashed before binding MediaKeys object to HTMLMediaElement"));
+ return promise.forget();
+ }
+
// 5.3.1 Associate the CDM instance represented by mediaKeys with the
// media element for decrypting media data.
if (NS_FAILED(aMediaKeys->Bind(this))) {
diff --git a/dom/media/MediaDecoder.cpp b/dom/media/MediaDecoder.cpp
index 52150986eb96..61d74ce6a9c2 100644
--- a/dom/media/MediaDecoder.cpp
+++ b/dom/media/MediaDecoder.cpp
@@ -1713,6 +1713,7 @@ void
MediaDecoder::SetCDMProxy(CDMProxy* aProxy)
{
MOZ_ASSERT(NS_IsMainThread());
+ MOZ_ASSERT(aProxy);
mCDMProxyPromiseHolder.ResolveIfExists(aProxy, __func__);
}