Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] add security warning for anchor element (rel attribute) #6289

Merged
merged 3 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,26 @@ export default class Element extends Node {
const href_attribute = attribute_map.get('href') || attribute_map.get('xlink:href');
const id_attribute = attribute_map.get('id');
const name_attribute = attribute_map.get('name');
const target_attribute = attribute_map.get('target');

if (target_attribute && target_attribute.get_static_value() === '_blank' && href_attribute) {
const href_static_value = href_attribute.get_static_value() ? href_attribute.get_static_value().toLowerCase() : null;

if (href_static_value === null || href_static_value.match(/^(https?:)?\/\//i)) {
const rel = attribute_map.get('rel');
const rel_values = rel ? rel.get_static_value().split(' ') : [];
const expected_values = ['noopener', 'noreferrer'];

expected_values.forEach(expected_value => {
if (!rel || rel && rel_values.indexOf(expected_value) < 0) {
component.warn(this, {
code: `security-anchor-rel-${expected_value}`,
message: `Security: Anchor with "target=_blank" should have rel attribute containing the value "${expected_value}"`
});
}
});
}
}

if (href_attribute) {
const href_value = href_attribute.get_static_value();
Expand Down
50 changes: 50 additions & 0 deletions test/validator/samples/security-anchor-rel-noopener/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="https://svelte.dev" target="_blank">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="https://svelte.dev" target="_blank" rel="">svelte website (invalid)</a>
<a href="https://svelte.dev" target="_blank" rel="noreferrer">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href={'https://svelte.dev'} target="_blank">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href={'https://svelte.dev'} target="_blank" rel="">svelte website (invalid)</a>
<a href={'https://svelte.dev'} target="_blank" rel="noreferrer">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="//svelte.dev" target="_blank">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="//svelte.dev" target="_blank" rel="">svelte website (invalid)</a>
<a href="//svelte.dev" target="_blank" rel="noreferrer">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="http://svelte.dev" target="_blank">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="http://svelte.dev" target="_blank" rel="">svelte website (invalid)</a>
<a href="http://svelte.dev" target="_blank" rel="noreferrer">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="HTTP://svelte.dev" target="_blank" rel="">svelte website (invalid)</a>
<a href="HTTP://svelte.dev" target="_blank" rel="noreferrer">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="HTTPS://svelte.dev" target="_blank" rel="">svelte website (invalid)</a>
<a href="HTTPS://svelte.dev" target="_blank" rel="noreferrer">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="HTTP://svelte.dev" target="_blank">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="HTTPS://svelte.dev" target="_blank">svelte website (invalid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="same-host" target="_blank">Same host (valid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="same-host" target="_blank" rel="">Same host (valid)</a>
<a href="same-host" target="_blank" rel="noreferrer">Same host (valid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="http://svelte.dev" target="_blank" rel="noopener">svelte website (valid)</a>
<a href="http://svelte.dev" target="_blank" rel="noreferrer noopener">svelte website (valid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="https://svelte.dev" target="_blank" rel="noopener">svelte website (valid)</a>
<a href="https://svelte.dev" target="_blank" rel="noreferrer noopener">svelte website (valid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="HTTP://svelte.dev" target="_blank" rel="noopener">svelte website (valid)</a>
<a href="HTTP://svelte.dev" target="_blank" rel="noreferrer noopener">svelte website (valid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="HTTPS://svelte.dev" target="_blank" rel="noopener">svelte website (valid)</a>
<a href="HTTPS://svelte.dev" target="_blank" rel="noreferrer noopener">svelte website (valid)</a>
<!-- svelte-ignore security-anchor-rel-noreferrer -->
<a href="//svelte.dev" target="_blank" rel="noopener">svelte website (valid)</a>
<a href="//svelte.dev" target="_blank" rel="noreferrer noopener">svelte website (valid)</a>
272 changes: 272 additions & 0 deletions test/validator/samples/security-anchor-rel-noopener/warnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
[
{
"code": "security-anchor-rel-noopener",
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"start": {
"line": 2,
"column": 0,
"character": 54
},
"end": {
"line": 2,
"column": 73,
"character": 127
},
"pos": 54
},
{
"code": "security-anchor-rel-noopener",
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"start": {
"line": 4,
"column": 0,
"character": 182
},
"end": {
"line": 4,
"column": 80,
"character": 262
},
"pos": 182
},
{
"code": "security-anchor-rel-noopener",
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"start": {
"line": 5,
"column": 0,
"character": 263
},
"end": {
"line": 5,
"column": 90,
"character": 353
},
"pos": 263
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 483,
"column": 75,
"line": 7
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 408,
"start": {
"character": 408,
"column": 0,
"line": 7
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 620,
"column": 82,
"line": 9
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 538,
"start": {
"character": 538,
"column": 0,
"line": 9
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 713,
"column": 92,
"line": 10
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 621,
"start": {
"character": 621,
"column": 0,
"line": 10
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 835,
"column": 67,
"line": 12
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 768,
"start": {
"character": 768,
"column": 0,
"line": 12
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 964,
"column": 74,
"line": 14
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 890,
"start": {
"character": 890,
"column": 0,
"line": 14
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 1049,
"column": 84,
"line": 15
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 965,
"start": {
"character": 965,
"column": 0,
"line": 15
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 1176,
"column": 72,
"line": 17
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 1104,
"start": {
"character": 1104,
"column": 0,
"line": 17
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 1310,
"column": 79,
"line": 19
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 1231,
"start": {
"character": 1231,
"column": 0,
"line": 19
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 1400,
"column": 89,
"line": 20
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 1311,
"start": {
"character": 1311,
"column": 0,
"line": 20
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 1534,
"column": 79,
"line": 22
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 1455,
"start": {
"character": 1455,
"column": 0,
"line": 22
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 1624,
"column": 89,
"line": 23
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 1535,
"start": {
"character": 1535,
"column": 0,
"line": 23
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 1759,
"column": 80,
"line": 25
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 1679,
"start": {
"character": 1679,
"column": 0,
"line": 25
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 1850,
"column": 90,
"line": 26
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 1760,
"start": {
"character": 1760,
"column": 0,
"line": 26
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 1977,
"column": 72,
"line": 28
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 1905,
"start": {
"character": 1905,
"column": 0,
"line": 28
}
},
{
"code": "security-anchor-rel-noopener",
"end": {
"character": 2105,
"column": 73,
"line": 30
},
"message": "Security: Anchor with \"target=_blank\" should have rel attribute containing the value \"noopener\"",
"pos": 2032,
"start": {
"character": 2032,
"column": 0,
"line": 30
}
}
]
Loading