Commit 3543d97c authored by rijubrata.bhaumik's avatar rijubrata.bhaumik Committed by Commit Bot

[Battery] Allow usage from SecureContext or top-level browsing context only.

Make the Battery Status API available only within a secure context that is
also a top-level browsing context. This disallows the use of the API within
framed content, as well as from any content that is not a secure context.

Details: https://github.com/w3c/battery/issues/10

WPT updated in https://github.com/w3c/web-platform-tests/pull/5871

BUG=661792

Review-Url: https://codereview.chromium.org/2880763002
Cr-Commit-Position: refs/heads/master@{#476263}
parent 5d0edd94
...@@ -4,7 +4,7 @@ On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE ...@@ -4,7 +4,7 @@ On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE
PASS typeof(nav.getBattery()) == 'object' is true PASS typeof(nav.getBattery()) == 'object' is true
PASS nav.getBattery() is undefined. Error Code is 18
PASS successfullyParsed is true PASS successfullyParsed is true
TEST COMPLETE TEST COMPLETE
......
...@@ -18,8 +18,15 @@ function processMessage(event) { ...@@ -18,8 +18,15 @@ function processMessage(event) {
w.close(); w.close();
w = null; w = null;
} else if (event.data == "closed") { } else if (event.data == "closed") {
shouldBeUndefined("nav.getBattery()"); nav.getBattery().then(battery => {
finishJSTest(); assert_unreachable('getBattery should reject on a closed window');
})
.catch(error => {
// DOMException.SECURITY_ERR = 18.
debug('Error Code is ' + error.code);
assert_equals(error.code, DOMException.SECURITY_ERR);
});
setTimeout(finishJSTest, 0);
} }
} }
......
This is a testharness.js-based test.
FAIL throw a 'SecurityError' when invoking navigator.getBattery() within iframe assert_unreached: Should have rejected: undefined Reached unreachable code
Harness: the test ran to completion.
This is a testharness.js-based test.
FAIL navigator.getBattery() shall throw a 'SecurityError' in an insecure context assert_unreached: Should have rejected: undefined Reached unreachable code
Harness: the test ran to completion.
...@@ -4,8 +4,10 @@ ...@@ -4,8 +4,10 @@
#include "modules/battery/NavigatorBattery.h" #include "modules/battery/NavigatorBattery.h"
#include "core/dom/ExecutionContext.h" #include "core/dom/DOMException.h"
#include "core/frame/LocalFrame.h" #include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/frame/LocalDOMWindow.h"
#include "modules/battery/BatteryManager.h" #include "modules/battery/BatteryManager.h"
namespace blink { namespace blink {
...@@ -19,10 +21,26 @@ ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state, ...@@ -19,10 +21,26 @@ ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state,
} }
ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state) { ScriptPromise NavigatorBattery::getBattery(ScriptState* script_state) {
if (!battery_manager_) { ExecutionContext* execution_context = ExecutionContext::From(script_state);
battery_manager_ =
BatteryManager::Create(ExecutionContext::From(script_state)); // Check secure context.
String error_message;
if (!execution_context->IsSecureContext(error_message)) {
return ScriptPromise::RejectWithDOMException(
script_state, DOMException::Create(kSecurityError, error_message));
}
// Check top-level browsing context.
if (!ToDocument(execution_context)->domWindow()->GetFrame() ||
!ToDocument(execution_context)->GetFrame()->IsMainFrame()) {
return ScriptPromise::RejectWithDOMException(
script_state, DOMException::Create(
kSecurityError, "Not a top-level browsing context."));
} }
if (!battery_manager_)
battery_manager_ = BatteryManager::Create(execution_context);
return battery_manager_->StartRequest(script_state); return battery_manager_->StartRequest(script_state);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment