jsx-no-target-blank.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @fileoverview Forbid target='_blank' attribute
  3. * @author Kevin Miller
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. const linkComponentsUtil = require('../util/linkComponents');
  8. // ------------------------------------------------------------------------------
  9. // Rule Definition
  10. // ------------------------------------------------------------------------------
  11. function findLastIndex(arr, condition) {
  12. for (let i = arr.length - 1; i >= 0; i -= 1) {
  13. if (condition(arr[i])) {
  14. return i;
  15. }
  16. }
  17. return -1;
  18. }
  19. function attributeValuePossiblyBlank(attribute) {
  20. if (!attribute || !attribute.value) {
  21. return false;
  22. }
  23. const value = attribute.value;
  24. if (value.type === 'Literal') {
  25. return typeof value.value === 'string' && value.value.toLowerCase() === '_blank';
  26. }
  27. if (value.type === 'JSXExpressionContainer') {
  28. const expr = value.expression;
  29. if (expr.type === 'Literal') {
  30. return typeof expr.value === 'string' && expr.value.toLowerCase() === '_blank';
  31. }
  32. if (expr.type === 'ConditionalExpression') {
  33. if (expr.alternate.type === 'Literal' && expr.alternate.value && expr.alternate.value.toLowerCase() === '_blank') {
  34. return true;
  35. }
  36. if (expr.consequent.type === 'Literal' && expr.consequent.value && expr.consequent.value.toLowerCase() === '_blank') {
  37. return true;
  38. }
  39. }
  40. }
  41. return false;
  42. }
  43. function hasExternalLink(node, linkAttribute, warnOnSpreadAttributes, spreadAttributeIndex) {
  44. const linkIndex = findLastIndex(node.attributes, (attr) => attr.name && attr.name.name === linkAttribute);
  45. const foundExternalLink = linkIndex !== -1 && ((attr) => attr.value.type === 'Literal' && /^(?:\w+:|\/\/)/.test(attr.value.value))(
  46. node.attributes[linkIndex]);
  47. return foundExternalLink || (warnOnSpreadAttributes && linkIndex < spreadAttributeIndex);
  48. }
  49. function hasDynamicLink(node, linkAttribute) {
  50. const dynamicLinkIndex = findLastIndex(node.attributes, (attr) => attr.name
  51. && attr.name.name === linkAttribute
  52. && attr.value
  53. && attr.value.type === 'JSXExpressionContainer');
  54. if (dynamicLinkIndex !== -1) {
  55. return true;
  56. }
  57. }
  58. function getStringFromValue(value) {
  59. if (value) {
  60. if (value.type === 'Literal') {
  61. return value.value;
  62. }
  63. if (value.type === 'JSXExpressionContainer') {
  64. if (value.expression.type === 'TemplateLiteral') {
  65. return value.expression.quasis[0].value.cooked;
  66. }
  67. return value.expression && value.expression.value;
  68. }
  69. }
  70. return null;
  71. }
  72. function hasSecureRel(node, allowReferrer, warnOnSpreadAttributes, spreadAttributeIndex) {
  73. const relIndex = findLastIndex(node.attributes, (attr) => (attr.type === 'JSXAttribute' && attr.name.name === 'rel'));
  74. if (relIndex === -1 || (warnOnSpreadAttributes && relIndex < spreadAttributeIndex)) {
  75. return false;
  76. }
  77. const relAttribute = node.attributes[relIndex];
  78. const value = getStringFromValue(relAttribute.value);
  79. const tags = value && typeof value === 'string' && value.toLowerCase().split(' ');
  80. const noreferrer = tags && tags.indexOf('noreferrer') >= 0;
  81. if (noreferrer) {
  82. return true;
  83. }
  84. return allowReferrer && tags && tags.indexOf('noopener') >= 0;
  85. }
  86. module.exports = {
  87. meta: {
  88. fixable: 'code',
  89. docs: {
  90. description: 'Forbid `target="_blank"` attribute without `rel="noreferrer"`',
  91. category: 'Best Practices',
  92. recommended: true,
  93. url: docsUrl('jsx-no-target-blank')
  94. },
  95. messages: {
  96. noTargetBlank: 'Using target="_blank" without rel="noreferrer" '
  97. + 'is a security risk: see https://html.spec.whatwg.org/multipage/links.html#link-type-noopener'
  98. },
  99. schema: [{
  100. type: 'object',
  101. properties: {
  102. allowReferrer: {
  103. type: 'boolean'
  104. },
  105. enforceDynamicLinks: {
  106. enum: ['always', 'never']
  107. },
  108. warnOnSpreadAttributes: {
  109. type: 'boolean'
  110. }
  111. },
  112. additionalProperties: false
  113. }]
  114. },
  115. create(context) {
  116. const configuration = context.options[0] || {};
  117. const allowReferrer = configuration.allowReferrer || false;
  118. const warnOnSpreadAttributes = configuration.warnOnSpreadAttributes || false;
  119. const enforceDynamicLinks = configuration.enforceDynamicLinks || 'always';
  120. const components = linkComponentsUtil.getLinkComponents(context);
  121. return {
  122. JSXOpeningElement(node) {
  123. if (!components.has(node.name.name)) {
  124. return;
  125. }
  126. const targetIndex = findLastIndex(node.attributes, (attr) => attr.name && attr.name.name === 'target');
  127. const spreadAttributeIndex = findLastIndex(node.attributes, (attr) => (attr.type === 'JSXSpreadAttribute'));
  128. if (!attributeValuePossiblyBlank(node.attributes[targetIndex])) {
  129. const hasSpread = spreadAttributeIndex >= 0;
  130. if (warnOnSpreadAttributes && hasSpread) {
  131. // continue to check below
  132. } else if ((hasSpread && targetIndex < spreadAttributeIndex) || !hasSpread || !warnOnSpreadAttributes) {
  133. return;
  134. }
  135. }
  136. const linkAttribute = components.get(node.name.name);
  137. const hasDangerousLink = hasExternalLink(node, linkAttribute, warnOnSpreadAttributes, spreadAttributeIndex)
  138. || (enforceDynamicLinks === 'always' && hasDynamicLink(node, linkAttribute));
  139. if (hasDangerousLink && !hasSecureRel(node, allowReferrer, warnOnSpreadAttributes, spreadAttributeIndex)) {
  140. context.report({
  141. node,
  142. messageId: 'noTargetBlank',
  143. fix(fixer) {
  144. // eslint 5 uses `node.attributes`; eslint 6+ uses `node.parent.attributes`
  145. const nodeWithAttrs = node.parent.attributes ? node.parent : node;
  146. // eslint 5 does not provide a `name` property on JSXSpreadElements
  147. const relAttribute = nodeWithAttrs.attributes.find((attr) => attr.name && attr.name.name === 'rel');
  148. if (targetIndex < spreadAttributeIndex || (spreadAttributeIndex >= 0 && !relAttribute)) {
  149. return null;
  150. }
  151. if (!relAttribute) {
  152. return fixer.insertTextAfter(nodeWithAttrs.attributes.slice(-1)[0], ' rel="noreferrer"');
  153. }
  154. if (!relAttribute.value) {
  155. return fixer.insertTextAfter(relAttribute, '="noreferrer"');
  156. }
  157. if (relAttribute.value.type === 'Literal') {
  158. const parts = relAttribute.value.value
  159. .split('noreferrer')
  160. .filter(Boolean);
  161. return fixer.replaceText(relAttribute.value, `"${parts.concat('noreferrer').join(' ')}"`);
  162. }
  163. if (relAttribute.value.type === 'JSXExpressionContainer') {
  164. if (relAttribute.value.expression.type === 'Literal') {
  165. if (typeof relAttribute.value.expression.value === 'string') {
  166. const parts = relAttribute.value.expression.value
  167. .split('noreferrer')
  168. .filter(Boolean);
  169. return fixer.replaceText(relAttribute.value.expression, `"${parts.concat('noreferrer').join(' ')}"`);
  170. }
  171. // for undefined, boolean, number, symbol, bigint, and null
  172. return fixer.replaceText(relAttribute.value, '"noreferrer"');
  173. }
  174. }
  175. return null;
  176. }
  177. });
  178. }
  179. }
  180. };
  181. }
  182. };