-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Trusted Types] Get list of event handlers from WebIDL
This change retrieves the list of attributes declared as event handlers from WebIDL and uses that to check for TrustedScript, instead of using the string prefix "on". Bug: 993268, 1084587 Change-Id: Ic15bc0994bcd19d9d7385cbef4af0f01af820ae1 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3616765 Reviewed-by: Mason Freed <masonf@chromium.org> Reviewed-by: Yuki Shiino <yukishiino@chromium.org> Reviewed-by: Yifan Luo <lyf@chromium.org> Commit-Queue: Daniel Vogelheim <vogelheim@chromium.org> Cr-Commit-Position: refs/heads/main@{#1003034} NOKEYCHECK=True GitOrigin-RevId: 20acdd57bc0c2900456a9717629834686cdb4890
- Loading branch information
1 parent
ed2dd68
commit a69b64a
Showing
8 changed files
with
223 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
blink/renderer/core/trustedtypes/generate_eventhandler_names.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env python | ||
# Copyright 2022 The Chromium Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
import optparse | ||
import sys | ||
|
||
import web_idl | ||
|
||
|
||
# Read the WebIDL database and write a list of all event handler attributes. | ||
# | ||
# Reads the WebIDL database (--webidl) and writes a C++ .h file with a macro | ||
# containing all event handler names (to --out). All attributes declared as | ||
# EventHandler or On(BeforeUnload|Error)EventHandler types are considered | ||
# event handlers. | ||
# | ||
# The macro is called EVENT_HANDLER_LIST and follows the "X macro" model of | ||
# macro lists [1], as its used elsewhere [2] in the Chromium code base. | ||
# | ||
# [1] https://en.wikipedia.org/wiki/X_Macro | ||
# [2] https://source.chromium.org/search?q=%5E%23define%5C%20%5BA-Z_%5D*LIST%5C(%20file:v8 | ||
def main(argv): | ||
parser = optparse.OptionParser() | ||
parser.add_option("--out") | ||
parser.add_option("--webidl") | ||
options, args = parser.parse_args(argv[1:]) | ||
|
||
for option in ("out", "webidl"): | ||
if not getattr(options, option): | ||
parser.error(f"--{option} is required.") | ||
if args: | ||
parser.error("No positional arguments supported.") | ||
|
||
event_handlers = set() | ||
event_handler_types = ("EventHandler", "OnBeforeUnloadEventHandler", | ||
"OnErrorEventHandler") | ||
|
||
web_idl_database = web_idl.Database.read_from_file(options.webidl) | ||
for interface in web_idl_database.interfaces: | ||
for attribute in interface.attributes: | ||
idl_type = attribute.idl_type | ||
if (idl_type.is_typedef | ||
and idl_type.identifier in event_handler_types): | ||
event_handlers.add(attribute.identifier) | ||
|
||
license_and_header = """\ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
""" | ||
|
||
with open(options.out, "w") as out: | ||
print(license_and_header, file=out) | ||
print("// Generated from WebIDL database. Don't edit, just generate.", | ||
file=out) | ||
print("//", file=out) | ||
print(f"// Generator: {argv[0]}", file=out) | ||
print("", file=out) | ||
print("#define EVENT_HANDLER_LIST(EH) \\", file=out) | ||
for event in event_handlers: | ||
print(f" EH({event}) \\", file=out) | ||
print("\n", file=out) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main(sys.argv)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
blink/web_tests/external/wpt/trusted-types/trusted-types-event-handlers.tentative.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<!DOCTYPE html> | ||
<head> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'"> | ||
</head> | ||
<body> | ||
<script> | ||
const element = document.createElement("div"); | ||
|
||
[ | ||
"onclick", | ||
"onchange", | ||
"onfocus", | ||
"oNclick", | ||
"OnClIcK" | ||
].forEach(name => { | ||
test(t => { | ||
assert_throws_js(TypeError, | ||
_ => element.setAttribute(name, "2+2")); | ||
}, `Event handler ${name} should be blocked.`); | ||
}); | ||
|
||
[ | ||
"one", | ||
"oNe", | ||
"onIcon", | ||
"offIcon", | ||
"blubb" | ||
].forEach(name => { | ||
test(t => { | ||
element.setAttribute(name, "2+2"); | ||
}, `Non-event handler ${name} should not be blocked.`); | ||
}); | ||
|
||
// We'd like to be sure we're not missing anything. Let's "query" an HTML | ||
// element about which attributes it knows. | ||
const div = document.createElement("div"); | ||
for(name in div.__proto__) { | ||
const should_be_event_handler = name.startsWith("on"); | ||
if (should_be_event_handler) { | ||
test(t => { | ||
assert_throws_js(TypeError, | ||
_ => element.setAttribute(name, "2+2")); | ||
}, `Event handler div.${name} should be blocked.`); | ||
} else { | ||
test(t => { | ||
element.setAttribute(name, "2+2"); | ||
}, `Non-event handler div.${name} should not be blocked.`); | ||
} | ||
} | ||
</script> | ||
</body> |