|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const rule = require('../../../lib/rules/template-no-invalid-link-href'); |
| 4 | +const RuleTester = require('eslint').RuleTester; |
| 5 | + |
| 6 | +const ruleTester = new RuleTester({ |
| 7 | + parser: require.resolve('ember-eslint-parser'), |
| 8 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 9 | +}); |
| 10 | + |
| 11 | +ruleTester.run('template-no-invalid-link-href', rule, { |
| 12 | + valid: [ |
| 13 | + // Valid navigable hrefs. |
| 14 | + '<template><a href="/x">Link</a></template>', |
| 15 | + '<template><a href="https://example.com">Link</a></template>', |
| 16 | + '<template><a href="#section">Link</a></template>', |
| 17 | + '<template><a href="mailto:[email protected]">Email</a></template>', |
| 18 | + '<template><a href="tel:+47123">Phone</a></template>', |
| 19 | + |
| 20 | + // Dynamic href — rule can't statically validate, skips. |
| 21 | + '<template><a href={{this.url}}>Link</a></template>', |
| 22 | + '<template><a href="{{this.prefix}}/{{this.slug}}">Link</a></template>', |
| 23 | + |
| 24 | + // No href at all — handled by template-link-href-attributes, not this rule. |
| 25 | + '<template><a>Not a link</a></template>', |
| 26 | + |
| 27 | + // Non-anchor elements are not in scope. |
| 28 | + '<template><button>Click me</button></template>', |
| 29 | + '<template><div href="#">Not an anchor</div></template>', |
| 30 | + |
| 31 | + // <area> is in scope — same href semantics as <a>. Valid values pass. |
| 32 | + '<template><map name="m"><area href="/region-a" shape="rect" coords="0,0,10,10" /></map></template>', |
| 33 | + '<template><area href="#section" shape="default" /></template>', |
| 34 | + // Dynamic area href — skip. |
| 35 | + '<template><area href={{this.url}} shape="rect" coords="0,0,1,1" /></template>', |
| 36 | + |
| 37 | + // Non-scheme URLs that happen to contain `javascript:` are not javascript: |
| 38 | + // URLs — they are relative paths or fragments. The URL parser resolves |
| 39 | + // them against the base URL; no script runs. |
| 40 | + '<template><a href="./javascript:foo">Relative path</a></template>', |
| 41 | + '<template><a href="#javascript:foo">Fragment id</a></template>', |
| 42 | + '<template><a href="/javascript:foo">Absolute path</a></template>', |
| 43 | + '<template><a href="?q=javascript:foo">Query string</a></template>', |
| 44 | + // `javascript` as a bare word (no colon) — treated as a relative path. |
| 45 | + // Our regex only matches the `javascript:` scheme (with colon), so these pass. |
| 46 | + '<template><a href="javascript">Relative</a></template>', |
| 47 | + '<template><a href="javascriptFoo">Relative</a></template>', |
| 48 | + ], |
| 49 | + invalid: [ |
| 50 | + // Plain "#" placeholder. |
| 51 | + { |
| 52 | + code: '<template><a href="#">Click</a></template>', |
| 53 | + output: null, |
| 54 | + errors: [{ messageId: 'invalidHref' }], |
| 55 | + }, |
| 56 | + { |
| 57 | + code: '<template><a href="#!">Click</a></template>', |
| 58 | + output: null, |
| 59 | + errors: [{ messageId: 'invalidHref' }], |
| 60 | + }, |
| 61 | + // Empty / whitespace href. |
| 62 | + { |
| 63 | + code: '<template><a href="">Click</a></template>', |
| 64 | + output: null, |
| 65 | + errors: [{ messageId: 'invalidHref' }], |
| 66 | + }, |
| 67 | + { |
| 68 | + code: '<template><a href=" ">Click</a></template>', |
| 69 | + output: null, |
| 70 | + errors: [{ messageId: 'invalidHref' }], |
| 71 | + }, |
| 72 | + { |
| 73 | + code: '<template><a href>Click</a></template>', |
| 74 | + output: null, |
| 75 | + errors: [{ messageId: 'invalidHref' }], |
| 76 | + }, |
| 77 | + // javascript: protocol. |
| 78 | + { |
| 79 | + code: '<template><a href="javascript:void(0)">Click</a></template>', |
| 80 | + output: null, |
| 81 | + errors: [{ messageId: 'invalidHref' }], |
| 82 | + }, |
| 83 | + { |
| 84 | + code: '<template><a href="JavaScript:alert(1)">Click</a></template>', |
| 85 | + output: null, |
| 86 | + errors: [{ messageId: 'invalidHref' }], |
| 87 | + }, |
| 88 | + // Leading whitespace — catches obfuscations. |
| 89 | + { |
| 90 | + code: '<template><a href=" javascript:void(0)">Click</a></template>', |
| 91 | + output: null, |
| 92 | + errors: [{ messageId: 'invalidHref' }], |
| 93 | + }, |
| 94 | + // Mustache-string-literal hrefs resolve to their static value via the |
| 95 | + // shared `getStaticAttrValue` helper — the rule validates them the same |
| 96 | + // as text-node values. Covers the common bypass hole where authors |
| 97 | + // wrap a literal href in mustaches (`{{"#"}}`) to dodge a simple |
| 98 | + // "is this a text node" check. |
| 99 | + { |
| 100 | + code: '<template><a href={{"#"}}>Click</a></template>', |
| 101 | + output: null, |
| 102 | + errors: [{ messageId: 'invalidHref' }], |
| 103 | + }, |
| 104 | + { |
| 105 | + code: '<template><a href={{"javascript:void(0)"}}>Click</a></template>', |
| 106 | + output: null, |
| 107 | + errors: [{ messageId: 'invalidHref' }], |
| 108 | + }, |
| 109 | + // Single-part quoted-mustache (GlimmerConcatStatement wrapping a |
| 110 | + // literal) resolves the same way. |
| 111 | + { |
| 112 | + code: '<template><a href="{{\'#\'}}">Click</a></template>', |
| 113 | + output: null, |
| 114 | + errors: [{ messageId: 'invalidHref' }], |
| 115 | + }, |
| 116 | + // <area> shares <a>'s href semantics — same invalid values flag. |
| 117 | + { |
| 118 | + code: '<template><area href="#" shape="rect" coords="0,0,10,10" /></template>', |
| 119 | + output: null, |
| 120 | + errors: [{ messageId: 'invalidHref' }], |
| 121 | + }, |
| 122 | + { |
| 123 | + code: '<template><area href="" shape="rect" coords="0,0,10,10" /></template>', |
| 124 | + output: null, |
| 125 | + errors: [{ messageId: 'invalidHref' }], |
| 126 | + }, |
| 127 | + { |
| 128 | + code: '<template><area href="javascript:alert(1)" shape="rect" coords="0,0,10,10" /></template>', |
| 129 | + output: null, |
| 130 | + errors: [{ messageId: 'invalidHref' }], |
| 131 | + }, |
| 132 | + ], |
| 133 | +}); |
| 134 | + |
| 135 | +const hbsRuleTester = new RuleTester({ |
| 136 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 137 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 138 | +}); |
| 139 | + |
| 140 | +hbsRuleTester.run('template-no-invalid-link-href', rule, { |
| 141 | + valid: ['<a href="/x">Link</a>', '<a href={{this.url}}>Link</a>', '<a>Not a link</a>'], |
| 142 | + invalid: [ |
| 143 | + { |
| 144 | + code: '<a href="#">Click</a>', |
| 145 | + output: null, |
| 146 | + errors: [{ messageId: 'invalidHref' }], |
| 147 | + }, |
| 148 | + { |
| 149 | + code: '<a href="javascript:void(0)">Click</a>', |
| 150 | + output: null, |
| 151 | + errors: [{ messageId: 'invalidHref' }], |
| 152 | + }, |
| 153 | + ], |
| 154 | +}); |
0 commit comments