Skip to content

Commit cb5c73e

Browse files
srawlinsCommit Queue
authored andcommitted
linter: Remove unsafe_html rule
Fixes https://github.com/dart-lang/linter/issues/5001 Change-Id: I972ea8f9fafda88b6a4836ab92107cebe8a6ad4b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391303 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 39e5e0f commit cb5c73e

File tree

12 files changed

+11
-685
lines changed

12 files changed

+11
-685
lines changed

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,12 +2441,6 @@ LintCode.unrelated_type_equality_checks_in_expression:
24412441
status: needsEvaluation
24422442
LintCode.unrelated_type_equality_checks_in_pattern:
24432443
status: needsEvaluation
2444-
LintCode.unsafe_html_attribute:
2445-
status: noFix
2446-
LintCode.unsafe_html_method:
2447-
status: noFix
2448-
LintCode.unsafe_html_constructor:
2449-
status: noFix
24502444
LintCode.use_build_context_synchronously_async_use:
24512445
status: noFix
24522446
LintCode.use_build_context_synchronously_wrong_mounted:

pkg/linter/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- _(soon to be)_ deprecated lint: `unsafe_html`
66
- new _(experimental)_ lint: `omit_obvious_property_types`
77
- new _(experimental)_ lint: `specify_nonobvious_property_types`
8+
- removed lint: `unsafe_html`
89

910
# 3.6.0
1011

pkg/linter/example/all.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ linter:
206206
- unnecessary_to_list_in_spreads
207207
- unreachable_from_main
208208
- unrelated_type_equality_checks
209-
- unsafe_html
210209
- use_build_context_synchronously
211210
- use_colored_box
212211
- use_decorated_box

pkg/linter/lib/src/lint_codes.g.dart

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,27 +1762,6 @@ class LinterLintCode extends LintCode {
17621762
uniqueName: 'unrelated_type_equality_checks_in_pattern',
17631763
);
17641764

1765-
static const LintCode unsafe_html_attribute = LinterLintCode(
1766-
LintNames.unsafe_html,
1767-
"Assigning to the attribute '{0}' is unsafe.",
1768-
correctionMessage: "Try finding a different way to implement the page.",
1769-
uniqueName: 'unsafe_html_attribute',
1770-
);
1771-
1772-
static const LintCode unsafe_html_constructor = LinterLintCode(
1773-
LintNames.unsafe_html,
1774-
"Invoking the constructor '{0}' is unsafe.",
1775-
correctionMessage: "Try finding a different way to implement the page.",
1776-
uniqueName: 'unsafe_html_constructor',
1777-
);
1778-
1779-
static const LintCode unsafe_html_method = LinterLintCode(
1780-
LintNames.unsafe_html,
1781-
"Invoking the method '{0}' is unsafe.",
1782-
correctionMessage: "Try finding a different way to implement the page.",
1783-
uniqueName: 'unsafe_html_method',
1784-
);
1785-
17861765
static const LintCode
17871766
use_build_context_synchronously_async_use = LinterLintCode(
17881767
LintNames.use_build_context_synchronously,

pkg/linter/lib/src/rules/unsafe_html.dart

Lines changed: 5 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -2,173 +2,20 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

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';
96

107
import '../analyzer.dart';
11-
import '../extensions.dart';
128

139
const _desc = '$_descPrefix.';
1410
const _descPrefix = r'Avoid unsafe HTML APIs';
1511

1612
class UnsafeHtml extends LintRule {
1713
UnsafeHtml()
1814
: 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)));
2218

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.
2619
@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;
17421
}

pkg/linter/messages.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12925,6 +12925,7 @@ LintCode:
1292512925
addedIn: "2.4"
1292612926
categories: [errorProne]
1292712927
hasPublishedDocs: false
12928+
removedIn: "3.7"
1292812929
deprecatedDetails: |-
1292912930
**NOTE:** This lint is deprecated and will be removed in a future release.
1293012931
Remove all inclusions of this lint from your analysis options.
@@ -12946,16 +12947,20 @@ LintCode:
1294612947
```dart
1294712948
var script = ScriptElement()..src = 'foo.js';
1294812949
```
12950+
12951+
This rule has been removed.
1294912952
unsafe_html_constructor:
1295012953
sharedName: unsafe_html
1295112954
problemMessage: "Invoking the constructor '{0}' is unsafe."
1295212955
correctionMessage: "Try finding a different way to implement the page."
1295312956
hasPublishedDocs: false
12957+
removedIn: "3.7"
1295412958
unsafe_html_method:
1295512959
sharedName: unsafe_html
1295612960
problemMessage: "Invoking the method '{0}' is unsafe."
1295712961
correctionMessage: "Try finding a different way to implement the page."
1295812962
hasPublishedDocs: false
12963+
removedIn: "3.7"
1295912964
use_build_context_synchronously_async_use:
1296012965
sharedName: use_build_context_synchronously
1296112966
problemMessage: "Don't use 'BuildContext's across async gaps."

pkg/linter/test/all.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import 'mocks.dart';
1515
import 'pubspec_test.dart' as pubspec;
1616
import 'rule_test.dart' as rule;
1717
import 'rules/all.dart' as rules;
18-
import 'unmocked_sdk_rule_test.dart' as unmocked_sdk_rule;
1918
import 'utils_test.dart' as utils;
2019
import 'validate_incompatible_rules_test.dart' as validate_incompatible_rules;
2120
import 'validate_no_rule_description_references_test.dart'
@@ -41,7 +40,6 @@ void main() {
4140
pubspec.main();
4241
rule.main();
4342
rules.main();
44-
unmocked_sdk_rule.main();
4543
utils.main();
4644
validate_incompatible_rules.main();
4745
validate_no_rule_description_references.main();

pkg/linter/test/rules/all.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ import 'unnecessary_to_list_in_spreads_test.dart'
288288
import 'unreachable_from_main_test.dart' as unreachable_from_main;
289289
import 'unrelated_type_equality_checks_test.dart'
290290
as unrelated_type_equality_checks;
291-
import 'unsafe_html_test.dart' as unsafe_html;
292291
import 'use_build_context_synchronously_test.dart'
293292
as use_build_context_synchronously;
294293
import 'use_colored_box_test.dart' as use_colored_box;
@@ -530,7 +529,6 @@ void main() {
530529
unnecessary_to_list_in_spreads.main();
531530
unreachable_from_main.main();
532531
unrelated_type_equality_checks.main();
533-
unsafe_html.main();
534532
use_build_context_synchronously.main();
535533
use_colored_box.main();
536534
use_decorated_box.main();

0 commit comments

Comments
 (0)