Commit 183ce10c authored by Jinho Bang's avatar Jinho Bang Committed by Commit Bot

PaymentRequest: Use [SecureContext] instead of manual check

Until now, we had to use IsSecureContext() to check whether an execution
context is a secure context manually. But we can use [SecureContext]
instead of the manual check now. After this change, all related APIs
including PaymentRequest will not be exposed in non-secure context.

Bug: none
Change-Id: Ie5b3fc6641e882cc2844595fea350b6adb3425db
Reviewed-on: https://chromium-review.googlesource.com/818594Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Commit-Queue: Jinho Bang <jinho.bang@samsung.com>
Cr-Commit-Position: refs/heads/master@{#548425}
parent 88321d60
...@@ -33,7 +33,6 @@ public class PaymentRequestBlobUrlTest { ...@@ -33,7 +33,6 @@ public class PaymentRequestBlobUrlTest {
mPaymentRequestTestRule.openPageAndClickNode("buy"); mPaymentRequestTestRule.openPageAndClickNode("buy");
mPaymentRequestTestRule.assertWaitForPageScaleFactorMatch(2); mPaymentRequestTestRule.assertWaitForPageScaleFactorMatch(2);
mPaymentRequestTestRule.expectResultContains( mPaymentRequestTestRule.expectResultContains(
new String[] {"SecurityError: Failed to construct 'PaymentRequest': " new String[] {"PaymentRequest is not defined"});
+ "Must be in a secure context"});
} }
} }
...@@ -46,7 +46,6 @@ public class PaymentRequestDataUrlTest implements MainActivityStartCallback { ...@@ -46,7 +46,6 @@ public class PaymentRequestDataUrlTest implements MainActivityStartCallback {
public void test() throws InterruptedException, ExecutionException, TimeoutException { public void test() throws InterruptedException, ExecutionException, TimeoutException {
mPaymentRequestTestRule.openPageAndClickNode("buy"); mPaymentRequestTestRule.openPageAndClickNode("buy");
mPaymentRequestTestRule.expectResultContains( mPaymentRequestTestRule.expectResultContains(
new String[] {"SecurityError: Failed to construct 'PaymentRequest': " new String[] {"PaymentRequest is not defined"});
+ "Must be in a secure context"});
} }
} }
...@@ -24,12 +24,19 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestDataUrlTest, SecurityError) { ...@@ -24,12 +24,19 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestDataUrlTest, SecurityError) {
"'1.00'}}})).show(); } catch(e) { " "'1.00'}}})).show(); } catch(e) { "
"document.getElementById('result').innerHTML = e; }\">Data URL " "document.getElementById('result').innerHTML = e; }\">Data URL "
"Test</button><div id='result'></div></body></html>"); "Test</button><div id='result'></div></body></html>");
// PaymentRequest should not be defined in non-secure context.
bool result = true;
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
GetActiveWebContents(),
"window.domAutomationController.send('PaymentRequest' in window);",
&result));
ASSERT_FALSE(result);
ASSERT_TRUE(content::ExecuteScript( ASSERT_TRUE(content::ExecuteScript(
GetActiveWebContents(), GetActiveWebContents(),
"(function() { document.getElementById('buy').click(); })();")); "(function() { document.getElementById('buy').click(); })();"));
ExpectBodyContains( ExpectBodyContains({"PaymentRequest is not defined"});
{"SecurityError: Failed to construct 'PaymentRequest': Must be in a "
"secure context"});
} }
} // namespace payments } // namespace payments
<!DOCTYPE html> <!DOCTYPE html>
<!-- Copyright © 2017 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<meta charset="utf-8"> <meta charset="utf-8">
<title>Test for PaymentRequest Constructor (insecure)</title> <title>Test for PaymentRequest Constructor (insecure)</title>
<link rel="help" href="https://w3c.github.io/payment-request/#paymentrequest-interface"> <link rel="help" href="https://w3c.github.io/payment-request/#paymentrequest-interface">
...@@ -8,6 +7,8 @@ ...@@ -8,6 +7,8 @@
<script> <script>
test(() => { test(() => {
assert_false(isSecureContext); assert_false(isSecureContext);
assert_false("PaymentRequest" in window); assert_false('PaymentRequest' in window);
assert_false('PaymentResponse' in window);
assert_false('PaymentRequestUpdateEvent' in window);
}, "PaymentRequest constructor must not be exposed in insecure context"); }, "PaymentRequest constructor must not be exposed in insecure context");
</script> </script>
...@@ -2,22 +2,23 @@ ...@@ -2,22 +2,23 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// https://w3c.github.io/browser-payment-api/#paymentaddress-interface // https://w3c.github.io/payment-request/#paymentaddress-interface
[ [
RuntimeEnabled=PaymentRequest, RuntimeEnabled=PaymentRequest,
SecureContext,
Exposed=Window Exposed=Window
] interface PaymentAddress { ] interface PaymentAddress {
serializer = {attribute}; serializer = {attribute};
readonly attribute DOMString country;
readonly attribute FrozenArray<DOMString> addressLine;
readonly attribute DOMString region;
readonly attribute DOMString city; readonly attribute DOMString city;
readonly attribute DOMString country;
readonly attribute DOMString dependentLocality; readonly attribute DOMString dependentLocality;
readonly attribute DOMString postalCode;
readonly attribute DOMString sortingCode;
readonly attribute DOMString languageCode; readonly attribute DOMString languageCode;
readonly attribute DOMString organization; readonly attribute DOMString organization;
readonly attribute DOMString recipient;
readonly attribute DOMString phone; readonly attribute DOMString phone;
readonly attribute DOMString postalCode;
readonly attribute DOMString recipient;
readonly attribute DOMString region;
readonly attribute DOMString sortingCode;
readonly attribute FrozenArray<DOMString> addressLine;
}; };
...@@ -1005,10 +1005,7 @@ PaymentRequest::PaymentRequest(ExecutionContext* execution_context, ...@@ -1005,10 +1005,7 @@ PaymentRequest::PaymentRequest(ExecutionContext* execution_context,
execution_context->GetTaskRunner(TaskType::kMiscPlatformAPI), execution_context->GetTaskRunner(TaskType::kMiscPlatformAPI),
this, this,
&PaymentRequest::OnCompleteTimeout) { &PaymentRequest::OnCompleteTimeout) {
if (!GetExecutionContext()->IsSecureContext()) { DCHECK(GetExecutionContext()->IsSecureContext());
exception_state.ThrowSecurityError("Must be in a secure context");
return;
}
if (!AllowedToUsePaymentRequest(GetFrame())) { if (!AllowedToUsePaymentRequest(GetFrame())) {
exception_state.ThrowSecurityError( exception_state.ThrowSecurityError(
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// https://w3c.github.io/browser-payment-api/#paymentrequest-interface // https://w3c.github.io/payment-request/#paymentrequest-interface
// http://crbug.com/587995 // http://crbug.com/587995
[ [
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
Constructor(sequence<PaymentMethodData> methodData, PaymentDetailsInit details, optional PaymentOptions options), Constructor(sequence<PaymentMethodData> methodData, PaymentDetailsInit details, optional PaymentOptions options),
ConstructorCallWith=ExecutionContext, ConstructorCallWith=ExecutionContext,
RaisesException=Constructor, RaisesException=Constructor,
SecureContext,
Exposed=Window, Exposed=Window,
ActiveScriptWrappable ActiveScriptWrappable
] interface PaymentRequest : EventTarget { ] interface PaymentRequest : EventTarget {
......
...@@ -14,19 +14,6 @@ ...@@ -14,19 +14,6 @@
namespace blink { namespace blink {
namespace { namespace {
TEST(PaymentRequestTest, SecureContextRequired) {
V8TestingScope scope;
scope.GetDocument().SetSecurityOrigin(
SecurityOrigin::Create(KURL("http://www.example.com/")));
PaymentRequest::Create(
scope.GetExecutionContext(), BuildPaymentMethodDataForTest(),
BuildPaymentDetailsInitForTest(), scope.GetExceptionState());
EXPECT_TRUE(scope.GetExceptionState().HadException());
EXPECT_EQ(kSecurityError, scope.GetExceptionState().Code());
}
TEST(PaymentRequestTest, NoExceptionWithValidData) { TEST(PaymentRequestTest, NoExceptionWithValidData) {
V8TestingScope scope; V8TestingScope scope;
MakePaymentRequestOriginSecure(scope.GetDocument()); MakePaymentRequestOriginSecure(scope.GetDocument());
......
...@@ -10,22 +10,23 @@ enum PaymentComplete { ...@@ -10,22 +10,23 @@ enum PaymentComplete {
"unknown" "unknown"
}; };
// https://w3c.github.io/browser-payment-api/#paymentresponse-interface // https://w3c.github.io/payment-request/#dom-paymentresponse
[ [
RuntimeEnabled=PaymentRequest, RuntimeEnabled=PaymentRequest,
SecureContext,
Exposed=Window Exposed=Window
] interface PaymentResponse { ] interface PaymentResponse {
serializer = {attribute}; serializer = {attribute};
readonly attribute DOMString requestId; readonly attribute DOMString requestId;
readonly attribute DOMString methodName; readonly attribute DOMString methodName;
readonly attribute DOMString? payerName;
readonly attribute DOMString? payerEmail;
readonly attribute DOMString? payerPhone;
[CallWith=ScriptState, RaisesException] readonly attribute object details; [CallWith=ScriptState, RaisesException] readonly attribute object details;
readonly attribute PaymentAddress? shippingAddress; readonly attribute PaymentAddress? shippingAddress;
readonly attribute DOMString? shippingOption; readonly attribute DOMString? shippingOption;
readonly attribute DOMString? payerName;
readonly attribute DOMString? payerEmail;
readonly attribute DOMString? payerPhone;
[CallWith=ScriptState] Promise<void> complete(optional PaymentComplete paymentResult = "unknown"); [CallWith=ScriptState] Promise<void> complete(optional PaymentComplete paymentResult = "unknown");
}; };
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