This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 198
/
noDocumentWriteRule.ts
52 lines (45 loc) · 1.89 KB
/
noDocumentWriteRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';
import { AstUtils } from './utils/AstUtils';
import { ExtendedMetadata } from './utils/ExtendedMetadata';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: ExtendedMetadata = {
ruleName: 'no-document-write',
type: 'maintainability',
description: 'Do not use document.write',
options: null, // tslint:disable-line:no-null-keyword
optionsDescription: '',
typescriptOnly: true,
issueClass: 'SDL',
issueType: 'Error',
severity: 'Critical',
level: 'Mandatory',
group: 'Security',
commonWeaknessEnumeration: '79, 85'
};
public static WRITE_FAILURE: string = 'Forbidden call to document.write';
public static WRITELN_FAILURE: string = 'Forbidden call to document.writeln';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}
function walk(ctx: Lint.WalkContext<void>) {
function cb(node: ts.Node): void {
if (tsutils.isCallExpression(node)) {
const functionTarget = AstUtils.getFunctionTarget(node);
if (functionTarget === 'document' || functionTarget === 'window.document') {
if (node.arguments.length === 1) {
const functionName: string = AstUtils.getFunctionName(node);
if (functionName === 'write') {
ctx.addFailureAt(node.getStart(), node.getWidth(), Rule.WRITE_FAILURE);
} else if (functionName === 'writeln') {
ctx.addFailureAt(node.getStart(), node.getWidth(), Rule.WRITELN_FAILURE);
}
}
}
}
return ts.forEachChild(node, cb);
}
return ts.forEachChild(ctx.sourceFile, cb);
}