Skip to content

Commit

Permalink
Update clipboard API calls to use clipboard permissions.
Browse files Browse the repository at this point in the history
This adds permission checks to make sure that the user has granted
appropriate permission for the clipboard API methods.

Bug: 677564
Change-Id: I5af221b0e18d45509ff2a8c3856680e598a51dcb
Reviewed-on: https://chromium-review.googlesource.com/804973
Reviewed-by: Antoine Labour <piman@chromium.org>
Reviewed-by: Raymes Khoury <raymes@chromium.org>
Reviewed-by: Philip Jägenstedt <foolip@chromium.org>
Commit-Queue: Gary Kacmarcik <garykac@chromium.org>
Cr-Commit-Position: refs/heads/master@{#525516}
  • Loading branch information
garykac authored and Commit Bot committed Dec 20, 2017
1 parent ea3e79e commit ba383e4
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 15 deletions.
12 changes: 9 additions & 3 deletions chrome/browser/permissions/permission_uma_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ std::string GetPermissionRequestString(PermissionRequestType type) {
return "AudioCapture";
case PermissionRequestType::PERMISSION_MEDIASTREAM_CAMERA:
return "VideoCapture";
case PermissionRequestType::PERMISSION_CLIPBOARD_READ:
return "ClipboardRead";
default:
NOTREACHED();
return "";
Expand Down Expand Up @@ -500,9 +502,9 @@ void PermissionUmaUtil::RecordPermissionAction(
bool secure_origin = content::IsOriginSecure(requesting_origin);

switch (permission) {
// Geolocation, MidiSysEx, Push, and Media permissions are disabled on
// insecure origins, so there's no need to record separate metrics for
// secure/insecure.
// Geolocation, MidiSysEx, Push, Media and Clipboard permissions are
// disabled on insecure origins, so there's no need to record separate
// metrics for secure/insecure.
case CONTENT_SETTINGS_TYPE_GEOLOCATION:
UMA_HISTOGRAM_ENUMERATION("Permissions.Action.Geolocation", action,
PermissionAction::NUM);
Expand Down Expand Up @@ -536,6 +538,10 @@ void PermissionUmaUtil::RecordPermissionAction(
"Permissions.Action.SecureOrigin.Flash",
"Permissions.Action.InsecureOrigin.Flash", action);
break;
case CONTENT_SETTINGS_TYPE_CLIPBOARD_READ:
UMA_HISTOGRAM_ENUMERATION("Permissions.Action.ClipboardRead", action,
PermissionAction::NUM);
break;
// The user is not prompted for these permissions, thus there is no
// permission action recorded for them.
default:
Expand Down
2 changes: 2 additions & 0 deletions chrome/browser/permissions/permission_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ std::string PermissionUtil::GetPermissionString(
return "AccessibilityEvents";
case CONTENT_SETTINGS_TYPE_CLIPBOARD_READ:
return "ClipboardRead";
case CONTENT_SETTINGS_TYPE_CLIPBOARD_WRITE:
return "ClipboardWrite";
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ void LayoutTestMessageFilter::OnSetPermission(
type = PermissionType::BACKGROUND_SYNC;
} else if (name == "accessibility-events") {
type = PermissionType::ACCESSIBILITY_EVENTS;
} else if (name == "clipboard-read") {
type = PermissionType::CLIPBOARD_READ;
} else if (name == "clipboard-write") {
type = PermissionType::CLIPBOARD_WRITE;
} else {
NOTREACHED();
type = PermissionType::NOTIFICATIONS;
Expand Down
5 changes: 5 additions & 0 deletions third_party/WebKit/LayoutTests/NeverFixTests
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ external/wpt/audio-output/setSinkId-manual.https.html [ WontFix ]
external/wpt/battery-status/battery-charging-manual.https.html [ WontFix ]
external/wpt/battery-status/battery-plugging-in-manual.https.html [ WontFix ]
external/wpt/battery-status/battery-unplugging-manual.https.html [ WontFix ]
external/wpt/clipboard-apis/async-navigator-clipboard-basics.https.html [ WontFix ]
external/wpt/clipboard-apis/async-write-dttext-read-dttext-manual.https.html [ WontFix ]
external/wpt/clipboard-apis/async-write-dttext-read-text-manual.https.html [ WontFix ]
external/wpt/clipboard-apis/async-write-text-read-dttext-manual.https.html [ WontFix ]
external/wpt/clipboard-apis/async-write-text-read-text-manual.https.html [ WontFix ]
external/wpt/console/console-count-logging-manual.html [ WontFix ]
external/wpt/css/CSS2/linebox/inline-formatting-context-010b.xht [ WontFix ]
external/wpt/css/CSS2/normal-flow/inline-block-replaced-height-008.xht [ WontFix ]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Tests navigator.clipboard.readText() permission failure.

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


PASS PermissionsHelper.setPermission is defined.
PASS navigator.clipboard is non-null.
PASS clipboard.readText() fail (as expected).
PASS successfullyParsed is true

TEST COMPLETE

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="../http/tests/resources/permissions-helper.js"></script>
</head>
<body>
<script>
description("Tests navigator.clipboard.readText() permission failure.");

shouldBeDefined("PermissionsHelper.setPermission");

function successCallback(access) {
testFailed(
"clipboard.readText() does not fail (even though it really should have).");
finishJSTest();
}

function errorCallback(error) {
testPassed("clipboard.readText() fail (as expected).");
finishJSTest();
}

window.jsTestIsAsync = true;

PermissionsHelper.setPermission('clipboard-read', 'denied').then(function() {
shouldBeNonNull("navigator.clipboard");
navigator.clipboard.readText().then(successCallback, errorCallback);
});


</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Tests navigator.clipboard.readText() permission success.

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


PASS PermissionsHelper.setPermission is defined.
PASS navigator.clipboard is non-null.
PASS clipboard.readText() success (as expected).
PASS successfullyParsed is true

TEST COMPLETE

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="../http/tests/resources/permissions-helper.js"></script>
</head>
<body>
<script>
description("Tests navigator.clipboard.readText() permission success.");

shouldBeDefined("PermissionsHelper.setPermission");

function successCallback(access) {
testPassed("clipboard.readText() success (as expected).");
finishJSTest();
}

function errorCallback(error) {
testFailed(
"clipboard.readText() does not fail (even though we hoped it would).");
finishJSTest();
}

window.jsTestIsAsync = true;

PermissionsHelper.setPermission('clipboard-read', 'granted').then(function() {
shouldBeNonNull("navigator.clipboard");
navigator.clipboard.readText().then(successCallback, errorCallback);
});


</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Tests navigator.clipboard.writeText() permission failure.

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


PASS PermissionsHelper.setPermission is defined.
PASS navigator.clipboard is non-null.
PASS clipboard.writeText() fail (as expected).
PASS successfullyParsed is true

TEST COMPLETE

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="../http/tests/resources/permissions-helper.js"></script>
</head>
<body>
<script>
description("Tests navigator.clipboard.writeText() permission failure.");

shouldBeDefined("PermissionsHelper.setPermission");

function successCallback(access) {
testFailed(
"clipboard.writeText() does not fail (even though it really should have).");
finishJSTest();
}

function errorCallback(error) {
testPassed("clipboard.writeText() fail (as expected).");
finishJSTest();
}

window.jsTestIsAsync = true;

PermissionsHelper.setPermission('clipboard-write', 'denied').then(function() {
shouldBeNonNull("navigator.clipboard");
navigator.clipboard.writeText("xyz").then(successCallback, errorCallback);
});


</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Tests navigator.clipboard.writeText() permission success.

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


PASS PermissionsHelper.setPermission is defined.
PASS navigator.clipboard is non-null.
PASS clipboard.writeText() success (as expected).
PASS successfullyParsed is true

TEST COMPLETE

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="../http/tests/resources/permissions-helper.js"></script>
</head>
<body>
<script>
description("Tests navigator.clipboard.writeText() permission success.");

shouldBeDefined("PermissionsHelper.setPermission");

function successCallback(access) {
testPassed("clipboard.writeText() success (as expected).");
finishJSTest();
}

function errorCallback(error) {
testFailed(
"clipboard.writeText() does not fail (even though we hoped it would).");
finishJSTest();
}

window.jsTestIsAsync = true;

PermissionsHelper.setPermission('clipboard-write', 'granted').then(function() {
shouldBeNonNull("navigator.clipboard");
navigator.clipboard.writeText("xyz").then(successCallback, errorCallback);
});


</script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var PermissionsHelper = (function() {
return {name: "background-sync"};
case "accessibility-events":
return {name: "accessibility-events"};
case "clipboard-read":
return {name: "clipboard-read"};
case "clipboard-write":
return {name: "clipboard-write"};
default:
throw "Invalid permission name provided";
}
Expand Down
Loading

0 comments on commit ba383e4

Please sign in to comment.