From 0bdad32b66c6ddf339aaa51caf8f1e3110262033 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Mon, 2 Feb 2015 08:57:53 -0700 Subject: [PATCH] servo: Merge #4793 - Added error checking on XMLHttpRequest::setWithCredentials (from KiChjang:xhr-cred-check); r=Manishearth Fixes #4665 Source-Repo: https://github.com/servo/servo Source-Revision: 755adf0ddefb060007c0319655f994445aea4709 --- .../script/dom/webidls/XMLHttpRequest.webidl | 1 + servo/components/script/dom/xmlhttprequest.rs | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/servo/components/script/dom/webidls/XMLHttpRequest.webidl b/servo/components/script/dom/webidls/XMLHttpRequest.webidl index ba100ca23ad1..6066ba9c17bd 100644 --- a/servo/components/script/dom/webidls/XMLHttpRequest.webidl +++ b/servo/components/script/dom/webidls/XMLHttpRequest.webidl @@ -50,6 +50,7 @@ interface XMLHttpRequest : XMLHttpRequestEventTarget { void setRequestHeader(ByteString name, ByteString value); [SetterThrows] attribute unsigned long timeout; + [SetterThrows] attribute boolean withCredentials; readonly attribute XMLHttpRequestUpload upload; [Throws] diff --git a/servo/components/script/dom/xmlhttprequest.rs b/servo/components/script/dom/xmlhttprequest.rs index 0f6e95512722..961125bf25b7 100644 --- a/servo/components/script/dom/xmlhttprequest.rs +++ b/servo/components/script/dom/xmlhttprequest.rs @@ -489,8 +489,21 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> { fn WithCredentials(self) -> bool { self.with_credentials.get() } - fn SetWithCredentials(self, with_credentials: bool) { - self.with_credentials.set(with_credentials); + // Spec for SetWithCredentials: https://xhr.spec.whatwg.org/#dom-xmlhttprequest-withcredentials + fn SetWithCredentials(self, with_credentials: bool) -> ErrorResult { + match self.ready_state.get() { + XMLHttpRequestState::HeadersReceived | + XMLHttpRequestState::Loading | + XMLHttpRequestState::XHRDone => Err(InvalidState), + _ if self.send_flag.get() => Err(InvalidState), + _ => match self.global.root() { + GlobalRoot::Window(_) if self.sync.get() => Err(InvalidAccess), + _ => { + self.with_credentials.set(with_credentials); + Ok(()) + }, + }, + } } fn Upload(self) -> Temporary { Temporary::new(self.upload)