|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
5 | | -import 'package:analyzer/dart/ast/ast.dart'; |
6 | | -import 'package:analyzer/dart/ast/visitor.dart'; |
7 | | -import 'package:analyzer/dart/element/element2.dart'; |
8 | | -import 'package:analyzer/dart/element/type.dart'; |
| 5 | +import 'package:pub_semver/pub_semver.dart'; |
9 | 6 |
|
10 | 7 | import '../analyzer.dart'; |
11 | | -import '../extensions.dart'; |
12 | 8 |
|
13 | 9 | const _desc = '$_descPrefix.'; |
14 | 10 | const _descPrefix = r'Avoid unsafe HTML APIs'; |
15 | 11 |
|
16 | 12 | class UnsafeHtml extends LintRule { |
17 | 13 | UnsafeHtml() |
18 | 14 | : super( |
19 | | - name: LintNames.unsafe_html, |
20 | | - description: _desc, |
21 | | - ); |
| 15 | + name: LintNames.unsafe_html, |
| 16 | + description: _desc, |
| 17 | + state: State.removed(since: Version(3, 7, 0))); |
22 | 18 |
|
23 | | - // TODO(brianwilkerson): This lint is not yet using the generated LintCodes. |
24 | | - // We would like to use the codes in the future, but doing |
25 | | - // so requires coordination with other tool teams. |
26 | 19 | @override |
27 | | - List<LintCode> get lintCodes => [ |
28 | | - _Visitor.unsafeAttributeCode, |
29 | | - _Visitor.unsafeMethodCode, |
30 | | - _Visitor.unsafeConstructorCode |
31 | | - ]; |
32 | | - |
33 | | - @override |
34 | | - void registerNodeProcessors( |
35 | | - NodeLintRegistry registry, LinterContext context) { |
36 | | - var visitor = _Visitor(this); |
37 | | - registry.addAssignmentExpression(this, visitor); |
38 | | - registry.addInstanceCreationExpression(this, visitor); |
39 | | - registry.addMethodInvocation(this, visitor); |
40 | | - } |
41 | | -} |
42 | | - |
43 | | -class _Visitor extends SimpleAstVisitor<void> { |
44 | | - // TODO(srawlins): Reference attributes ('href', 'src', and 'srcdoc') with |
45 | | - // single-quotes to match the convention in the analyzer and linter packages. |
46 | | - // This requires some coordination within Google, as various allow-lists are |
47 | | - // keyed on the exact text of the LintCode message. |
48 | | - // Proposed replacements are commented out in `UnsafeHtml`. |
49 | | - static const unsafeAttributeCode = SecurityLintCode( |
50 | | - 'unsafe_html', |
51 | | - '$_descPrefix (assigning "{0}" attribute).', |
52 | | - uniqueName: 'LintCode.unsafe_html_attribute', |
53 | | - ); |
54 | | - static const unsafeMethodCode = SecurityLintCode( |
55 | | - 'unsafe_html', |
56 | | - "$_descPrefix (calling the '{0}' method of {1}).", |
57 | | - uniqueName: 'LintCode.unsafe_html_method', |
58 | | - ); |
59 | | - static const unsafeConstructorCode = SecurityLintCode( |
60 | | - 'unsafe_html', |
61 | | - "$_descPrefix (calling the '{0}' constructor of {1}).", |
62 | | - uniqueName: 'LintCode.unsafe_html_constructor', |
63 | | - ); |
64 | | - |
65 | | - final LintRule rule; |
66 | | - |
67 | | - _Visitor(this.rule); |
68 | | - |
69 | | - @override |
70 | | - void visitAssignmentExpression(AssignmentExpression node) { |
71 | | - var leftPart = node.leftHandSide.unParenthesized; |
72 | | - if (leftPart is SimpleIdentifier) { |
73 | | - var leftPartElement = node.writeElement2; |
74 | | - if (leftPartElement == null) return; |
75 | | - var enclosingElement = leftPartElement.enclosingElement2; |
76 | | - if (enclosingElement is ClassElement2) { |
77 | | - _checkAssignment(enclosingElement.thisType, leftPart, node); |
78 | | - } |
79 | | - } else if (leftPart is PropertyAccess) { |
80 | | - _checkAssignment( |
81 | | - leftPart.realTarget.staticType, leftPart.propertyName, node); |
82 | | - } else if (leftPart is PrefixedIdentifier) { |
83 | | - _checkAssignment(leftPart.prefix.staticType, leftPart.identifier, node); |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - @override |
88 | | - void visitInstanceCreationExpression(InstanceCreationExpression node) { |
89 | | - var type = node.staticType; |
90 | | - if (type == null) return; |
91 | | - |
92 | | - var constructorName = node.constructorName; |
93 | | - if (constructorName.name?.name == 'html') { |
94 | | - if (type.extendsDartHtmlClass('DocumentFragment')) { |
95 | | - rule.reportLint(node, |
96 | | - arguments: ['html', 'DocumentFragment'], |
97 | | - errorCode: unsafeConstructorCode); |
98 | | - } else if (type.extendsDartHtmlClass('Element')) { |
99 | | - rule.reportLint(node, |
100 | | - arguments: ['html', 'Element'], errorCode: unsafeConstructorCode); |
101 | | - } |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - @override |
106 | | - void visitMethodInvocation(MethodInvocation node) { |
107 | | - var methodName = node.methodName.name; |
108 | | - |
109 | | - // The static type of the target. |
110 | | - DartType? type; |
111 | | - if (node.realTarget == null) { |
112 | | - // Implicit `this` target. |
113 | | - var methodElement = node.methodName.element; |
114 | | - if (methodElement == null) return; |
115 | | - var enclosingElement = methodElement.enclosingElement2; |
116 | | - if (enclosingElement is ClassElement2) { |
117 | | - type = enclosingElement.thisType; |
118 | | - } else { |
119 | | - return; |
120 | | - } |
121 | | - } else { |
122 | | - type = node.realTarget?.staticType; |
123 | | - if (type == null) return; |
124 | | - } |
125 | | - |
126 | | - if (methodName == 'createFragment' && |
127 | | - (type is DynamicType || type.extendsDartHtmlClass('Element'))) { |
128 | | - rule.reportLint(node, |
129 | | - arguments: ['createFragment', 'Element'], |
130 | | - errorCode: unsafeMethodCode); |
131 | | - } else if (methodName == 'setInnerHtml' && |
132 | | - (type is DynamicType || type.extendsDartHtmlClass('Element'))) { |
133 | | - rule.reportLint(node, |
134 | | - arguments: ['setInnerHtml', 'Element'], errorCode: unsafeMethodCode); |
135 | | - } else if (methodName == 'open' && |
136 | | - (type is DynamicType || type.extendsDartHtmlClass('Window'))) { |
137 | | - rule.reportLint(node, |
138 | | - arguments: ['open', 'Window'], errorCode: unsafeMethodCode); |
139 | | - } |
140 | | - } |
141 | | - |
142 | | - void _checkAssignment(DartType? type, SimpleIdentifier property, |
143 | | - AssignmentExpression assignment) { |
144 | | - if (type == null) return; |
145 | | - |
146 | | - // It is more efficient to check the setter's name before checking whether |
147 | | - // the target is an interesting type. |
148 | | - if (property.name == 'href') { |
149 | | - if (type is DynamicType || type.extendsDartHtmlClass('AnchorElement')) { |
150 | | - rule.reportLint(assignment, |
151 | | - arguments: ['href'], errorCode: unsafeAttributeCode); |
152 | | - } |
153 | | - } else if (property.name == 'src') { |
154 | | - if (type is DynamicType || |
155 | | - type.extendsDartHtmlClass('EmbedElement') || |
156 | | - type.extendsDartHtmlClass('IFrameElement') || |
157 | | - type.extendsDartHtmlClass('ScriptElement')) { |
158 | | - rule.reportLint(assignment, |
159 | | - arguments: ['src'], errorCode: unsafeAttributeCode); |
160 | | - } |
161 | | - } else if (property.name == 'srcdoc') { |
162 | | - if (type is DynamicType || type.extendsDartHtmlClass('IFrameElement')) { |
163 | | - rule.reportLint(assignment, |
164 | | - arguments: ['srcdoc'], errorCode: unsafeAttributeCode); |
165 | | - } |
166 | | - } |
167 | | - } |
168 | | -} |
169 | | - |
170 | | -extension on DartType? { |
171 | | - /// Returns whether this type extends [className] from the dart:html library. |
172 | | - bool extendsDartHtmlClass(String className) => |
173 | | - extendsClass(className, 'dart.dom.html'); |
| 20 | + LintCode get lintCode => LinterLintCode.removed_lint; |
174 | 21 | } |
0 commit comments