Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit fcc02dd

Browse files
committed
[feat] added tsr-detect-unsafe-properties-access rule; optimized imports in source files
1 parent c4391f1 commit fcc02dd

21 files changed

+91
-19
lines changed

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ By default `tslint-config-security` enables [all rules](#rules), but you may dis
3131
"tsr-detect-html-injection": false,
3232
"tsr-detect-unsafe-regexp": false
3333
}
34+
}
3435
```
3536

3637

@@ -209,3 +210,34 @@ const myWindow = document.getElementById('myIFrame').contentWindow;
209210

210211
myWindow.postMessage(message, "*"); // Noncompliant
211212
```
213+
214+
#### tsr-detect-unsafe-properties-access
215+
216+
Detects a potential unsafe access to the object properties
217+
218+
```js
219+
/*
220+
221+
It equals to `new Function(prop3)`
222+
223+
const a = {};
224+
225+
a["constructor"]["constructor"]("alert(1)")()
226+
*/
227+
228+
// unsafe
229+
obj[prop1][prop2](prop3)
230+
231+
// unsafe
232+
obj[prop1][prop2](prop3)()
233+
234+
```
235+
236+
More information:
237+
* [Web Puzzlers - Securing Dynamic Systems](https://youtu.be/SkNWAjDRLDY)
238+
* [Defensive JavaScript](https://www.javascriptjanuary.com/blog/defensive-javascript)
239+
240+
Solutions:
241+
* use [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
242+
* use `.hasOwnProperty` check
243+
* use `Content-Security-Policy` on your page

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
'tsr-disable-mustache-escape': [true],
1616
'tsr-detect-html-injection': [true],
1717
'tsr-detect-sql-literal-injection': [true],
18-
'tsr-detect-unsafe-cross-origin-communication': [true]
18+
'tsr-detect-unsafe-cross-origin-communication': [true],
19+
'tsr-detect-unsafe-properties-access': [true]
1920
}
2021
};

npm-shrinkwrap.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tslint-config-security",
3-
"version": "1.14.0",
3+
"version": "1.15.0",
44
"description": "TSLint security rules",
55
"main": "./index.js",
66
"files": [

src/rules/tsrDetectBufferNoassertRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33

44
const readMethods: string[] = [
55
'readUInt8',

src/rules/tsrDetectChildProcessRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33
import {StringLiteral, stringLiteralKinds} from '../node-kind';
44

55
export class Rule extends Lint.Rules.AbstractRule {

src/rules/tsrDetectEvalWithExpressionRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33
import {stringLiteralKinds} from '../node-kind';
44
import syntaxKindToName from '../syntax-kind-to-name';
55

src/rules/tsrDetectHtmlInjectionRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33
import {stringLiteralKinds} from '../node-kind';
44

55
export class Rule extends Lint.Rules.AbstractRule {

src/rules/tsrDetectNoCsrfBeforeMethodOverrideRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33

44
export class Rule extends Lint.Rules.AbstractRule {
55
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {

src/rules/tsrDetectNonLiteralBufferRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33
import {stringLiteralKinds} from '../node-kind';
44

55
export class Rule extends Lint.Rules.AbstractRule {

src/rules/tsrDetectNonLiteralRegexpRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33
import {stringLiteralKinds} from '../node-kind';
44

55
export class Rule extends Lint.Rules.AbstractRule {

src/rules/tsrDetectNonLiteralRequireRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33
import {stringLiteralKinds} from '../node-kind';
44

55
export class Rule extends Lint.Rules.AbstractRule {

src/rules/tsrDetectPossibleTimingAttacksRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33
import {StringLiteral, stringLiteralKinds} from '../node-kind';
44

55
const keywordMask = new RegExp(

src/rules/tsrDetectPseudoRandomBytesRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33

44
export class Rule extends Lint.Rules.AbstractRule {
55
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {

src/rules/tsrDetectSqlLiteralInjectionRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33
import {isSqlQuery} from '../is-sql-query';
44
import {stringLiteralKinds} from '../node-kind';
55

src/rules/tsrDetectUnsafeCrossOriginCommunicationRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33

44
export class Rule extends Lint.Rules.AbstractRule {
55
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
3+
4+
export class Rule extends Lint.Rules.AbstractRule {
5+
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
6+
return this.applyWithWalker(new RuleWalker(sourceFile, this.getOptions()));
7+
}
8+
}
9+
10+
class RuleWalker extends Lint.RuleWalker {
11+
visitCallExpression(node: ts.CallExpression) {
12+
const {expression, arguments: args} = node;
13+
14+
if (
15+
expression &&
16+
args &&
17+
expression.kind === ts.SyntaxKind.ElementAccessExpression &&
18+
args.find(ts.isIdentifier)
19+
) {
20+
this.addFailureAtNode(node, 'Found unsafe properties access');
21+
}
22+
23+
super.visitCallExpression(node);
24+
}
25+
}

src/rules/tsrDetectUnsafeRegexpRule.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as ts from 'typescript';
1+
// @ts-ignore
2+
import * as isSafeRegexp from 'safe-regex';
23
import * as Lint from 'tslint';
4+
import * as ts from 'typescript';
35
import {StringLiteral, stringLiteralKinds} from '../node-kind';
46

5-
const isSafeRegexp = require('safe-regex');
6-
77
export class Rule extends Lint.Rules.AbstractRule {
88
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
99
return this.applyWithWalker(new RuleWalker(sourceFile, this.getOptions()));

src/rules/tsrDisableMustacheEscapeRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ts from 'typescript';
21
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
33

44
export class Rule extends Lint.Rules.AbstractRule {
55
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
obj[prop1][prop2](prop3)
2+
~~~~~~~~~~~~~~~~~~~~~~~~ [Found unsafe properties access]
3+
4+
obj[prop1][prop2](prop3)()
5+
~~~~~~~~~~~~~~~~~~~~~~~~ [Found unsafe properties access]
6+
7+
obj[prop1][prop2].knownApiMethod()
8+
obj.prop1.prop2(prop3)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"rulesDirectory": "../../../../dist/rules",
3+
"rules": {
4+
"tsr-detect-unsafe-properties-access": true
5+
}
6+
}

0 commit comments

Comments
 (0)