Add trusted modifier (#6149)

Fixes #6137

Adding a trusted modifier to make events not be dispatchable by console/sourcecode.
Useful to prevent injected code to automatically dispatch event for preventing botting
pull/6461/head
Dennis Kaspar 3 years ago committed by GitHub
parent e43778a0c4
commit b464320607
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -82,7 +82,8 @@ const valid_modifiers = new Set([
'once',
'passive',
'nonpassive',
'self'
'self',
'trusted'
]);
const passive_events = new Set([

@ -42,6 +42,7 @@ export default class EventHandlerWrapper {
if (this.node.modifiers.has('preventDefault')) snippet = x`@prevent_default(${snippet})`;
if (this.node.modifiers.has('stopPropagation')) snippet = x`@stop_propagation(${snippet})`;
if (this.node.modifiers.has('self')) snippet = x`@self(${snippet})`;
if (this.node.modifiers.has('trusted')) snippet = x`@trusted(${snippet})`;
const args = [];

@ -214,6 +214,13 @@ export function self(fn) {
};
}
export function trusted(fn) {
return function(event) {
// @ts-ignore
if (event.isTrusted) fn.call(this, event);
};
}
export function attr(node: Element, attribute: string, value?: string) {
if (value == null) node.removeAttribute(attribute);
else if (node.getAttribute(attribute) !== value) node.setAttribute(attribute, value);

@ -0,0 +1,9 @@
export default {
async test({ assert, component, target, window }) {
const button = target.querySelector('button');
const event = new window.MouseEvent('click');
await button.dispatchEvent(event);
assert.equal(component.trusted, true);
}
};

@ -0,0 +1,5 @@
<script>
export let trusted = true;
</script>
<button on:click|trusted="{() => trusted = false}">Only trusted events: {trusted?'true':'false'}</button>

@ -1,5 +1,5 @@
[{
"message": "Valid event modifiers are preventDefault, stopPropagation, capture, once, passive, nonpassive or self",
"message": "Valid event modifiers are preventDefault, stopPropagation, capture, once, passive, nonpassive, self or trusted",
"code": "invalid-event-modifier",
"start": {
"line": 1,

Loading…
Cancel
Save