From 84293afb3b83262c9ad974fb471b16508763c053 Mon Sep 17 00:00:00 2001 From: Daniel Imfeld Date: Thu, 23 Apr 2020 04:18:03 -1000 Subject: [PATCH] prevent illegal attribute names at compile time (#4650) --- src/compiler/compile/nodes/Element.ts | 10 +++++++++- .../samples/attribute-invalid-name-2/errors.json | 15 +++++++++++++++ .../samples/attribute-invalid-name-2/input.svelte | 1 + .../samples/attribute-invalid-name-3/errors.json | 15 +++++++++++++++ .../samples/attribute-invalid-name-3/input.svelte | 1 + .../samples/attribute-invalid-name-4/errors.json | 15 +++++++++++++++ .../samples/attribute-invalid-name-4/input.svelte | 1 + .../samples/attribute-invalid-name-5/errors.json | 15 +++++++++++++++ .../samples/attribute-invalid-name-5/input.svelte | 1 + .../samples/attribute-invalid-name/errors.json | 15 +++++++++++++++ .../samples/attribute-invalid-name/input.svelte | 1 + 11 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 test/validator/samples/attribute-invalid-name-2/errors.json create mode 100644 test/validator/samples/attribute-invalid-name-2/input.svelte create mode 100644 test/validator/samples/attribute-invalid-name-3/errors.json create mode 100644 test/validator/samples/attribute-invalid-name-3/input.svelte create mode 100644 test/validator/samples/attribute-invalid-name-4/errors.json create mode 100644 test/validator/samples/attribute-invalid-name-4/input.svelte create mode 100644 test/validator/samples/attribute-invalid-name-5/errors.json create mode 100644 test/validator/samples/attribute-invalid-name-5/input.svelte create mode 100644 test/validator/samples/attribute-invalid-name/errors.json create mode 100644 test/validator/samples/attribute-invalid-name/input.svelte diff --git a/src/compiler/compile/nodes/Element.ts b/src/compiler/compile/nodes/Element.ts index 8c9c0bfe1841..8ff36de31ad7 100644 --- a/src/compiler/compile/nodes/Element.ts +++ b/src/compiler/compile/nodes/Element.ts @@ -378,6 +378,14 @@ export default class Element extends Node { } } + + if (/(^[0-9-.])|[\^$@%&#?!|()[\]{}^*+~;]/.test(name)) { + component.error(attribute, { + code: `illegal-attribute`, + message: `'${name}' is not a valid attribute name`, + }); + } + if (name === 'slot') { if (!attribute.is_static) { component.error(attribute, { @@ -768,4 +776,4 @@ function within_custom_element(parent: INode) { parent = parent.parent; } return false; -} \ No newline at end of file +} diff --git a/test/validator/samples/attribute-invalid-name-2/errors.json b/test/validator/samples/attribute-invalid-name-2/errors.json new file mode 100644 index 000000000000..9e0c43733943 --- /dev/null +++ b/test/validator/samples/attribute-invalid-name-2/errors.json @@ -0,0 +1,15 @@ +[{ + "code": "illegal-attribute", + "message": "'3aa' is not a valid attribute name", + "start": { + "line": 1, + "column": 3, + "character": 3 + }, + "end": { + "line": 1, + "column": 12, + "character": 12 + }, + "pos": 3 +}] diff --git a/test/validator/samples/attribute-invalid-name-2/input.svelte b/test/validator/samples/attribute-invalid-name-2/input.svelte new file mode 100644 index 000000000000..6958a1b5aacf --- /dev/null +++ b/test/validator/samples/attribute-invalid-name-2/input.svelte @@ -0,0 +1 @@ +

Test

diff --git a/test/validator/samples/attribute-invalid-name-3/errors.json b/test/validator/samples/attribute-invalid-name-3/errors.json new file mode 100644 index 000000000000..f5832268823e --- /dev/null +++ b/test/validator/samples/attribute-invalid-name-3/errors.json @@ -0,0 +1,15 @@ +[{ + "code": "illegal-attribute", + "message": "'a*a' is not a valid attribute name", + "start": { + "line": 1, + "column": 3, + "character": 3 + }, + "end": { + "line": 1, + "column": 6, + "character": 6 + }, + "pos": 3 +}] diff --git a/test/validator/samples/attribute-invalid-name-3/input.svelte b/test/validator/samples/attribute-invalid-name-3/input.svelte new file mode 100644 index 000000000000..87c2df979a7e --- /dev/null +++ b/test/validator/samples/attribute-invalid-name-3/input.svelte @@ -0,0 +1 @@ +

Test

diff --git a/test/validator/samples/attribute-invalid-name-4/errors.json b/test/validator/samples/attribute-invalid-name-4/errors.json new file mode 100644 index 000000000000..a8959cebf3ca --- /dev/null +++ b/test/validator/samples/attribute-invalid-name-4/errors.json @@ -0,0 +1,15 @@ +[{ + "code": "illegal-attribute", + "message": "'-a' is not a valid attribute name", + "start": { + "line": 1, + "column": 3, + "character": 3 + }, + "end": { + "line": 1, + "column": 5, + "character": 5 + }, + "pos": 3 +}] diff --git a/test/validator/samples/attribute-invalid-name-4/input.svelte b/test/validator/samples/attribute-invalid-name-4/input.svelte new file mode 100644 index 000000000000..92f68449b93e --- /dev/null +++ b/test/validator/samples/attribute-invalid-name-4/input.svelte @@ -0,0 +1 @@ +

Test

diff --git a/test/validator/samples/attribute-invalid-name-5/errors.json b/test/validator/samples/attribute-invalid-name-5/errors.json new file mode 100644 index 000000000000..b1e3acc5ef20 --- /dev/null +++ b/test/validator/samples/attribute-invalid-name-5/errors.json @@ -0,0 +1,15 @@ +[{ + "code": "illegal-attribute", + "message": "'a;' is not a valid attribute name", + "start": { + "line": 1, + "column": 3, + "character": 3 + }, + "end": { + "line": 1, + "column": 11, + "character": 11 + }, + "pos": 3 +}] diff --git a/test/validator/samples/attribute-invalid-name-5/input.svelte b/test/validator/samples/attribute-invalid-name-5/input.svelte new file mode 100644 index 000000000000..3a7dd05137c2 --- /dev/null +++ b/test/validator/samples/attribute-invalid-name-5/input.svelte @@ -0,0 +1 @@ +

Test

diff --git a/test/validator/samples/attribute-invalid-name/errors.json b/test/validator/samples/attribute-invalid-name/errors.json new file mode 100644 index 000000000000..9bdde1ee46dc --- /dev/null +++ b/test/validator/samples/attribute-invalid-name/errors.json @@ -0,0 +1,15 @@ +[{ + "code": "illegal-attribute", + "message": "'}' is not a valid attribute name", + "start": { + "line": 1, + "column": 3, + "character": 3 + }, + "end": { + "line": 1, + "column": 4, + "character": 4 + }, + "pos": 3 +}] diff --git a/test/validator/samples/attribute-invalid-name/input.svelte b/test/validator/samples/attribute-invalid-name/input.svelte new file mode 100644 index 000000000000..56d9cfe23f0a --- /dev/null +++ b/test/validator/samples/attribute-invalid-name/input.svelte @@ -0,0 +1 @@ +

Test