@dronedeploy/rocos-js-sdk 3.0.1-alpha.20 → 3.0.1-alpha.21
Sign up to get free protection for your applications and to get access to all the features.
- package/cjs/helpers/index.d.ts +1 -0
- package/cjs/helpers/index.js +1 -0
- package/cjs/helpers/kscript/Context.d.ts +34 -0
- package/cjs/helpers/kscript/Context.js +52 -0
- package/cjs/helpers/kscript/index.d.ts +1 -0
- package/cjs/helpers/kscript/index.js +27 -0
- package/cjs/helpers/kscript/kscript.d.ts +6 -0
- package/cjs/helpers/kscript/kscript.js +115 -0
- package/cjs/helpers/kscript/nodes/Identifier.d.ts +12 -0
- package/cjs/helpers/kscript/nodes/Identifier.js +20 -0
- package/cjs/helpers/kscript/nodes/Literal.d.ts +11 -0
- package/cjs/helpers/kscript/nodes/Literal.js +22 -0
- package/cjs/helpers/kscript/nodes/Node.d.ts +9 -0
- package/cjs/helpers/kscript/nodes/Node.js +15 -0
- package/cjs/helpers/kscript/nodes/Program.d.ts +11 -0
- package/cjs/helpers/kscript/nodes/Program.js +24 -0
- package/cjs/helpers/kscript/nodes/TemplateLiteral.d.ts +23 -0
- package/cjs/helpers/kscript/nodes/TemplateLiteral.js +35 -0
- package/cjs/helpers/kscript/nodes/expressions/ArrayExpression.d.ts +7 -0
- package/cjs/helpers/kscript/nodes/expressions/ArrayExpression.js +31 -0
- package/cjs/helpers/kscript/nodes/expressions/ArrowFunctionExpression.d.ts +14 -0
- package/cjs/helpers/kscript/nodes/expressions/ArrowFunctionExpression.js +40 -0
- package/cjs/helpers/kscript/nodes/expressions/BinaryExpression.d.ts +16 -0
- package/cjs/helpers/kscript/nodes/expressions/BinaryExpression.js +104 -0
- package/cjs/helpers/kscript/nodes/expressions/CallExpression.d.ts +8 -0
- package/cjs/helpers/kscript/nodes/expressions/CallExpression.js +38 -0
- package/cjs/helpers/kscript/nodes/expressions/ChainExpression.d.ts +16 -0
- package/cjs/helpers/kscript/nodes/expressions/ChainExpression.js +25 -0
- package/cjs/helpers/kscript/nodes/expressions/ConditionalExpression.d.ts +14 -0
- package/cjs/helpers/kscript/nodes/expressions/ConditionalExpression.js +30 -0
- package/cjs/helpers/kscript/nodes/expressions/ExpressionStatement.d.ts +7 -0
- package/cjs/helpers/kscript/nodes/expressions/ExpressionStatement.js +18 -0
- package/cjs/helpers/kscript/nodes/expressions/LogicalExpression.d.ts +10 -0
- package/cjs/helpers/kscript/nodes/expressions/LogicalExpression.js +47 -0
- package/cjs/helpers/kscript/nodes/expressions/MemberExpression.d.ts +44 -0
- package/cjs/helpers/kscript/nodes/expressions/MemberExpression.js +98 -0
- package/cjs/helpers/kscript/nodes/expressions/ObjectExpression.d.ts +11 -0
- package/cjs/helpers/kscript/nodes/expressions/ObjectExpression.js +59 -0
- package/cjs/helpers/kscript/nodes/expressions/UnaryExpression.d.ts +12 -0
- package/cjs/helpers/kscript/nodes/expressions/UnaryExpression.js +44 -0
- package/cjs/helpers/kscript/nodes/expressions/index.d.ts +11 -0
- package/cjs/helpers/kscript/nodes/expressions/index.js +28 -0
- package/cjs/helpers/kscript/nodes/index.d.ts +4 -0
- package/cjs/helpers/kscript/nodes/index.js +10 -0
- package/cjs/helpers/kscript/nodes/nodeTypes.d.ts +33 -0
- package/cjs/helpers/kscript/nodes/nodeTypes.js +29 -0
- package/cjs/helpers/kscript/utils.d.ts +4 -0
- package/cjs/helpers/kscript/utils.js +15 -0
- package/cjs/services/AuthService.d.ts +3 -3
- package/cjs/services/AuthService.js +7 -6
- package/esm/helpers/index.d.ts +1 -0
- package/esm/helpers/index.js +1 -0
- package/esm/helpers/kscript/Context.d.ts +34 -0
- package/esm/helpers/kscript/Context.js +49 -0
- package/esm/helpers/kscript/index.d.ts +1 -0
- package/esm/helpers/kscript/index.js +1 -0
- package/esm/helpers/kscript/kscript.d.ts +6 -0
- package/esm/helpers/kscript/kscript.js +107 -0
- package/esm/helpers/kscript/nodes/Identifier.d.ts +12 -0
- package/esm/helpers/kscript/nodes/Identifier.js +14 -0
- package/esm/helpers/kscript/nodes/Literal.d.ts +11 -0
- package/esm/helpers/kscript/nodes/Literal.js +16 -0
- package/esm/helpers/kscript/nodes/Node.d.ts +9 -0
- package/esm/helpers/kscript/nodes/Node.js +12 -0
- package/esm/helpers/kscript/nodes/Program.d.ts +11 -0
- package/esm/helpers/kscript/nodes/Program.js +18 -0
- package/esm/helpers/kscript/nodes/TemplateLiteral.d.ts +23 -0
- package/esm/helpers/kscript/nodes/TemplateLiteral.js +29 -0
- package/esm/helpers/kscript/nodes/expressions/ArrayExpression.d.ts +7 -0
- package/esm/helpers/kscript/nodes/expressions/ArrayExpression.js +25 -0
- package/esm/helpers/kscript/nodes/expressions/ArrowFunctionExpression.d.ts +14 -0
- package/esm/helpers/kscript/nodes/expressions/ArrowFunctionExpression.js +34 -0
- package/esm/helpers/kscript/nodes/expressions/BinaryExpression.d.ts +16 -0
- package/esm/helpers/kscript/nodes/expressions/BinaryExpression.js +98 -0
- package/esm/helpers/kscript/nodes/expressions/CallExpression.d.ts +8 -0
- package/esm/helpers/kscript/nodes/expressions/CallExpression.js +32 -0
- package/esm/helpers/kscript/nodes/expressions/ChainExpression.d.ts +16 -0
- package/esm/helpers/kscript/nodes/expressions/ChainExpression.js +19 -0
- package/esm/helpers/kscript/nodes/expressions/ConditionalExpression.d.ts +14 -0
- package/esm/helpers/kscript/nodes/expressions/ConditionalExpression.js +24 -0
- package/esm/helpers/kscript/nodes/expressions/ExpressionStatement.d.ts +7 -0
- package/esm/helpers/kscript/nodes/expressions/ExpressionStatement.js +12 -0
- package/esm/helpers/kscript/nodes/expressions/LogicalExpression.d.ts +10 -0
- package/esm/helpers/kscript/nodes/expressions/LogicalExpression.js +41 -0
- package/esm/helpers/kscript/nodes/expressions/MemberExpression.d.ts +44 -0
- package/esm/helpers/kscript/nodes/expressions/MemberExpression.js +92 -0
- package/esm/helpers/kscript/nodes/expressions/ObjectExpression.d.ts +11 -0
- package/esm/helpers/kscript/nodes/expressions/ObjectExpression.js +53 -0
- package/esm/helpers/kscript/nodes/expressions/UnaryExpression.d.ts +12 -0
- package/esm/helpers/kscript/nodes/expressions/UnaryExpression.js +38 -0
- package/esm/helpers/kscript/nodes/expressions/index.d.ts +11 -0
- package/esm/helpers/kscript/nodes/expressions/index.js +11 -0
- package/esm/helpers/kscript/nodes/index.d.ts +4 -0
- package/esm/helpers/kscript/nodes/index.js +3 -0
- package/esm/helpers/kscript/nodes/nodeTypes.d.ts +33 -0
- package/esm/helpers/kscript/nodes/nodeTypes.js +24 -0
- package/esm/helpers/kscript/utils.d.ts +4 -0
- package/esm/helpers/kscript/utils.js +11 -0
- package/esm/services/AuthService.d.ts +3 -3
- package/esm/services/AuthService.js +7 -6
- package/package.json +2 -1
@@ -0,0 +1,8 @@
|
|
1
|
+
import { AnyNode, CallExpression } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
export default class CallExpressionNode extends Node<CallExpression> {
|
5
|
+
constructor(node: AnyNode, context: Context);
|
6
|
+
run(): unknown;
|
7
|
+
private spreadArgs;
|
8
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const Node_1 = __importDefault(require("../Node"));
|
7
|
+
const utils_1 = require("../../utils");
|
8
|
+
class CallExpressionNode extends Node_1.default {
|
9
|
+
constructor(node, context) {
|
10
|
+
super(node, context, 'CallExpression');
|
11
|
+
}
|
12
|
+
run() {
|
13
|
+
if (!this.context.isFunctionCallAllowed)
|
14
|
+
throw new Error('Function calls are not allowed');
|
15
|
+
const callee = (0, utils_1.construct)(this.node.callee, this.context).run();
|
16
|
+
let thisArg = {};
|
17
|
+
if (this.node.callee.type === 'MemberExpression') {
|
18
|
+
thisArg = (0, utils_1.construct)(this.node.callee.object, this.context).run();
|
19
|
+
}
|
20
|
+
if (typeof callee !== 'function') {
|
21
|
+
throw new Error('callee is not a function');
|
22
|
+
}
|
23
|
+
const args = this.spreadArgs(this.node.arguments);
|
24
|
+
return callee.call(thisArg, ...args);
|
25
|
+
}
|
26
|
+
spreadArgs(args) {
|
27
|
+
return args.reduce((acc, arg) => {
|
28
|
+
if (arg.type === 'SpreadElement') {
|
29
|
+
acc.push(...(0, utils_1.construct)(arg.argument, this.context).run());
|
30
|
+
}
|
31
|
+
else {
|
32
|
+
acc.push((0, utils_1.construct)(arg, this.context).run());
|
33
|
+
}
|
34
|
+
return acc;
|
35
|
+
}, []);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
exports.default = CallExpressionNode;
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { AnyNode, ChainExpression } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
/**
|
5
|
+
* A ChainExpression node is created when the chain operator is used.
|
6
|
+
*
|
7
|
+
* e.g. `a?.b?.c`
|
8
|
+
*
|
9
|
+
* This node does not need to do anything special, as the chain operator is handled by the MemberExpressionNode.
|
10
|
+
*
|
11
|
+
* @see MemberExpressionNode
|
12
|
+
*/
|
13
|
+
export default class ExpressionStatementNode extends Node<ChainExpression> {
|
14
|
+
constructor(node: AnyNode, context: Context);
|
15
|
+
run(): unknown;
|
16
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const Node_1 = __importDefault(require("../Node"));
|
7
|
+
const utils_1 = require("../../utils");
|
8
|
+
/**
|
9
|
+
* A ChainExpression node is created when the chain operator is used.
|
10
|
+
*
|
11
|
+
* e.g. `a?.b?.c`
|
12
|
+
*
|
13
|
+
* This node does not need to do anything special, as the chain operator is handled by the MemberExpressionNode.
|
14
|
+
*
|
15
|
+
* @see MemberExpressionNode
|
16
|
+
*/
|
17
|
+
class ExpressionStatementNode extends Node_1.default {
|
18
|
+
constructor(node, context) {
|
19
|
+
super(node, context, 'ChainExpression');
|
20
|
+
}
|
21
|
+
run() {
|
22
|
+
return (0, utils_1.construct)(this.node.expression, this.context).run();
|
23
|
+
}
|
24
|
+
}
|
25
|
+
exports.default = ExpressionStatementNode;
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { AnyNode, ConditionalExpression, IfStatement } from 'acorn';
|
2
|
+
import type Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
/** Represents a ternary conditional expression (a ? b : c) or an if statement (if (a) b else c).
|
5
|
+
*
|
6
|
+
* If a (test) is truthy, b (consequent) is returned. Otherwise, c (alternate) is returned.
|
7
|
+
*
|
8
|
+
* Since the if statement and the conditional expression are so similar, they are both handled by this node,
|
9
|
+
* even though they are separate in the AST.
|
10
|
+
*/
|
11
|
+
export default class ConditionalExpressionNode extends Node<IfStatement | ConditionalExpression> {
|
12
|
+
constructor(node: AnyNode, context: Context);
|
13
|
+
run(): unknown;
|
14
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const Node_1 = __importDefault(require("../Node"));
|
7
|
+
const utils_1 = require("../../utils");
|
8
|
+
/** Represents a ternary conditional expression (a ? b : c) or an if statement (if (a) b else c).
|
9
|
+
*
|
10
|
+
* If a (test) is truthy, b (consequent) is returned. Otherwise, c (alternate) is returned.
|
11
|
+
*
|
12
|
+
* Since the if statement and the conditional expression are so similar, they are both handled by this node,
|
13
|
+
* even though they are separate in the AST.
|
14
|
+
*/
|
15
|
+
class ConditionalExpressionNode extends Node_1.default {
|
16
|
+
constructor(node, context) {
|
17
|
+
super(node, context, ['ConditionalExpression', 'IfStatement']);
|
18
|
+
}
|
19
|
+
run() {
|
20
|
+
const test = (0, utils_1.construct)(this.node.test, this.context).run();
|
21
|
+
if (test) {
|
22
|
+
return (0, utils_1.construct)(this.node.consequent, this.context).run();
|
23
|
+
}
|
24
|
+
if (this.node.alternate === null || this.node.alternate === undefined) {
|
25
|
+
return undefined;
|
26
|
+
}
|
27
|
+
return (0, utils_1.construct)(this.node.alternate, this.context).run();
|
28
|
+
}
|
29
|
+
}
|
30
|
+
exports.default = ConditionalExpressionNode;
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { AnyNode, ExpressionStatement } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
export default class ExpressionStatementNode extends Node<ExpressionStatement> {
|
5
|
+
constructor(node: AnyNode, context: Context);
|
6
|
+
run(): unknown;
|
7
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const Node_1 = __importDefault(require("../Node"));
|
7
|
+
const utils_1 = require("../../utils");
|
8
|
+
class ExpressionStatementNode extends Node_1.default {
|
9
|
+
constructor(node, context) {
|
10
|
+
super(node, context, 'ExpressionStatement');
|
11
|
+
}
|
12
|
+
run() {
|
13
|
+
if (this.node.directive)
|
14
|
+
throw new Error('Directives are not supported');
|
15
|
+
return (0, utils_1.construct)(this.node.expression, this.context).run();
|
16
|
+
}
|
17
|
+
}
|
18
|
+
exports.default = ExpressionStatementNode;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { AnyNode, LogicalExpression } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
export default class BinaryExpressionNode extends Node<LogicalExpression> {
|
5
|
+
constructor(node: AnyNode, context: Context);
|
6
|
+
run(): unknown;
|
7
|
+
/** Try and skip evaluating the right operand */
|
8
|
+
private shortCircuit;
|
9
|
+
private operate;
|
10
|
+
}
|
@@ -0,0 +1,47 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const Node_1 = __importDefault(require("../Node"));
|
7
|
+
const utils_1 = require("../../utils");
|
8
|
+
class BinaryExpressionNode extends Node_1.default {
|
9
|
+
constructor(node, context) {
|
10
|
+
super(node, context, 'LogicalExpression');
|
11
|
+
}
|
12
|
+
run() {
|
13
|
+
const left = (0, utils_1.construct)(this.node.left, this.context).run();
|
14
|
+
const shortCircuit = this.shortCircuit(left);
|
15
|
+
if (shortCircuit !== null) {
|
16
|
+
return shortCircuit;
|
17
|
+
}
|
18
|
+
const right = (0, utils_1.construct)(this.node.right, this.context).run();
|
19
|
+
return this.operate(left, right);
|
20
|
+
}
|
21
|
+
/** Try and skip evaluating the right operand */
|
22
|
+
shortCircuit(left) {
|
23
|
+
if (this.node.operator === '&&' && !left) {
|
24
|
+
return false;
|
25
|
+
}
|
26
|
+
if (this.node.operator === '||' && left) {
|
27
|
+
return true;
|
28
|
+
}
|
29
|
+
if (this.node.operator === '??' && left !== null && left !== undefined) {
|
30
|
+
return left;
|
31
|
+
}
|
32
|
+
return null;
|
33
|
+
}
|
34
|
+
operate(left, right) {
|
35
|
+
switch (this.node.operator) {
|
36
|
+
case '||':
|
37
|
+
return left || right;
|
38
|
+
case '&&':
|
39
|
+
return left && right;
|
40
|
+
case '??':
|
41
|
+
return left ?? right;
|
42
|
+
default:
|
43
|
+
throw new Error(`Unsupported operator: ${this.node.operator}`);
|
44
|
+
}
|
45
|
+
}
|
46
|
+
}
|
47
|
+
exports.default = BinaryExpressionNode;
|
@@ -0,0 +1,44 @@
|
|
1
|
+
import { AnyNode, MemberExpression } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
/**
|
5
|
+
* A member expression is an expression that accesses a property of an object.
|
6
|
+
*
|
7
|
+
* Examples of member expressions are:
|
8
|
+
* - `a.b`
|
9
|
+
* - `a['b']`
|
10
|
+
* - `a[0]`
|
11
|
+
* - `a?.b`
|
12
|
+
* - `a?.['b']`
|
13
|
+
* - `a?.[0]`
|
14
|
+
*
|
15
|
+
* A member access in the sandbox is considered secure if the following conditions are met:
|
16
|
+
* - The property is not named 'constructor'
|
17
|
+
* - Either:
|
18
|
+
* * The property is owned by the object and is *not* on the whitelist.
|
19
|
+
* * The prototype is whitelisted and the property list is null or contains the property.
|
20
|
+
*
|
21
|
+
* @see Context.isPrototypeAllowed
|
22
|
+
*/
|
23
|
+
export default class MemberExpressionNode extends Node<MemberExpression> {
|
24
|
+
constructor(node: AnyNode, context: Context);
|
25
|
+
run(): unknown;
|
26
|
+
/** Returns the name of the property being accessed.
|
27
|
+
*
|
28
|
+
* Handles both computed and non-computed properties.
|
29
|
+
*/
|
30
|
+
private getPropertyIdentifier;
|
31
|
+
/** Checks if the property can be accessed on the object.
|
32
|
+
*
|
33
|
+
* @throws {Error} If the property is not allowed to be accessed.
|
34
|
+
*/
|
35
|
+
private checkPropertyAccess;
|
36
|
+
/** Searches the prototype chain of the object for the prototype that defines the property. */
|
37
|
+
private getPropertyPrototype;
|
38
|
+
/**
|
39
|
+
* Returns true if the property exists on the object.
|
40
|
+
*
|
41
|
+
* i.e. the property is defined somewhere on the prototype chain.
|
42
|
+
*/
|
43
|
+
private propertyExists;
|
44
|
+
}
|
@@ -0,0 +1,98 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const Node_1 = __importDefault(require("../Node"));
|
7
|
+
const utils_1 = require("../../utils");
|
8
|
+
/**
|
9
|
+
* A member expression is an expression that accesses a property of an object.
|
10
|
+
*
|
11
|
+
* Examples of member expressions are:
|
12
|
+
* - `a.b`
|
13
|
+
* - `a['b']`
|
14
|
+
* - `a[0]`
|
15
|
+
* - `a?.b`
|
16
|
+
* - `a?.['b']`
|
17
|
+
* - `a?.[0]`
|
18
|
+
*
|
19
|
+
* A member access in the sandbox is considered secure if the following conditions are met:
|
20
|
+
* - The property is not named 'constructor'
|
21
|
+
* - Either:
|
22
|
+
* * The property is owned by the object and is *not* on the whitelist.
|
23
|
+
* * The prototype is whitelisted and the property list is null or contains the property.
|
24
|
+
*
|
25
|
+
* @see Context.isPrototypeAllowed
|
26
|
+
*/
|
27
|
+
class MemberExpressionNode extends Node_1.default {
|
28
|
+
constructor(node, context) {
|
29
|
+
super(node, context, 'MemberExpression');
|
30
|
+
}
|
31
|
+
run() {
|
32
|
+
const object = (0, utils_1.construct)(this.node.object, this.context).run();
|
33
|
+
if (object === null || object === undefined) {
|
34
|
+
if (this.node.optional)
|
35
|
+
return undefined;
|
36
|
+
throw new Error(`Cannot read property of ${object}`);
|
37
|
+
}
|
38
|
+
const propertyIdentifier = this.getPropertyIdentifier();
|
39
|
+
if (!this.propertyExists(object, propertyIdentifier)) {
|
40
|
+
return undefined;
|
41
|
+
}
|
42
|
+
this.checkPropertyAccess(object, propertyIdentifier);
|
43
|
+
return object[propertyIdentifier];
|
44
|
+
}
|
45
|
+
/** Returns the name of the property being accessed.
|
46
|
+
*
|
47
|
+
* Handles both computed and non-computed properties.
|
48
|
+
*/
|
49
|
+
getPropertyIdentifier() {
|
50
|
+
if (this.node.computed) {
|
51
|
+
const name = (0, utils_1.construct)(this.node.property, this.context).run();
|
52
|
+
if (typeof name !== 'string' && typeof name !== 'number') {
|
53
|
+
throw new Error('Only string or number computed properties are supported');
|
54
|
+
}
|
55
|
+
return name;
|
56
|
+
}
|
57
|
+
if (this.node.property.type !== 'Identifier') {
|
58
|
+
throw new Error('Only identifiers are supported for non-computed member expression properties');
|
59
|
+
}
|
60
|
+
return this.node.property.name;
|
61
|
+
}
|
62
|
+
/** Checks if the property can be accessed on the object.
|
63
|
+
*
|
64
|
+
* @throws {Error} If the property is not allowed to be accessed.
|
65
|
+
*/
|
66
|
+
checkPropertyAccess(object, propertyIdentifier) {
|
67
|
+
if (typeof propertyIdentifier === 'number')
|
68
|
+
return;
|
69
|
+
if (propertyIdentifier === 'constructor') {
|
70
|
+
throw new Error('Cannot access constructor property');
|
71
|
+
}
|
72
|
+
const definingProto = this.getPropertyPrototype(object, propertyIdentifier);
|
73
|
+
const ownProperty = Object.prototype.hasOwnProperty.call(object, propertyIdentifier);
|
74
|
+
if (!this.context.isPrototypeAllowed(definingProto, propertyIdentifier, ownProperty)) {
|
75
|
+
throw new Error(`SecurityError: Access to property '${propertyIdentifier}' is not allowed`);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
/** Searches the prototype chain of the object for the prototype that defines the property. */
|
79
|
+
getPropertyPrototype(object, propertyIdentifier) {
|
80
|
+
let proto = object;
|
81
|
+
while (proto !== null) {
|
82
|
+
if (Object.prototype.hasOwnProperty.call(proto, propertyIdentifier)) {
|
83
|
+
return proto;
|
84
|
+
}
|
85
|
+
proto = Object.getPrototypeOf(proto);
|
86
|
+
}
|
87
|
+
return null;
|
88
|
+
}
|
89
|
+
/**
|
90
|
+
* Returns true if the property exists on the object.
|
91
|
+
*
|
92
|
+
* i.e. the property is defined somewhere on the prototype chain.
|
93
|
+
*/
|
94
|
+
propertyExists(object, propertyIdentifier) {
|
95
|
+
return this.getPropertyPrototype(object, propertyIdentifier) !== null;
|
96
|
+
}
|
97
|
+
}
|
98
|
+
exports.default = MemberExpressionNode;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { AnyNode, ObjectExpression } from 'acorn';
|
2
|
+
import type Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
export default class ObjectExpressionNode extends Node<ObjectExpression> {
|
5
|
+
constructor(node: AnyNode, context: Context);
|
6
|
+
run(): unknown;
|
7
|
+
private addProperty;
|
8
|
+
private spreadProperty;
|
9
|
+
private getKey;
|
10
|
+
private getStaticKey;
|
11
|
+
}
|
@@ -0,0 +1,59 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const Node_1 = __importDefault(require("../Node"));
|
7
|
+
const utils_1 = require("../../utils");
|
8
|
+
class ObjectExpressionNode extends Node_1.default {
|
9
|
+
constructor(node, context) {
|
10
|
+
super(node, context, 'ObjectExpression');
|
11
|
+
}
|
12
|
+
run() {
|
13
|
+
const object = {};
|
14
|
+
for (const property of this.node.properties) {
|
15
|
+
if (property.type === 'SpreadElement') {
|
16
|
+
this.spreadProperty(property, object);
|
17
|
+
continue;
|
18
|
+
}
|
19
|
+
this.addProperty(property, object);
|
20
|
+
}
|
21
|
+
return object;
|
22
|
+
}
|
23
|
+
addProperty(property, object) {
|
24
|
+
if (property.kind !== 'init')
|
25
|
+
throw new Error('Only init properties are supported');
|
26
|
+
const key = this.getKey(property);
|
27
|
+
object[key] = (0, utils_1.construct)(property.value, this.context).run();
|
28
|
+
}
|
29
|
+
spreadProperty(property, object) {
|
30
|
+
const value = (0, utils_1.construct)(property.argument, this.context).run();
|
31
|
+
if (!(typeof value === 'object' && value !== null)) {
|
32
|
+
throw new Error('Spread argument must be an object');
|
33
|
+
}
|
34
|
+
Object.assign(object, value);
|
35
|
+
}
|
36
|
+
getKey(property) {
|
37
|
+
let key;
|
38
|
+
if (property.computed) {
|
39
|
+
key = (0, utils_1.construct)(property.key, this.context).run();
|
40
|
+
}
|
41
|
+
else {
|
42
|
+
key = this.getStaticKey(property.key);
|
43
|
+
}
|
44
|
+
if (key === null || key === undefined)
|
45
|
+
throw new Error('Key cannot be null or undefined');
|
46
|
+
if (typeof key !== 'string' && typeof key !== 'number' && typeof key !== 'symbol') {
|
47
|
+
throw new Error(`Unsupported key type: ${typeof key}`);
|
48
|
+
}
|
49
|
+
return key;
|
50
|
+
}
|
51
|
+
getStaticKey(key) {
|
52
|
+
if (key.type === 'Identifier')
|
53
|
+
return key.name;
|
54
|
+
if (key.type === 'Literal')
|
55
|
+
return key.value;
|
56
|
+
throw new Error('Only identifier and literal keys are supported');
|
57
|
+
}
|
58
|
+
}
|
59
|
+
exports.default = ObjectExpressionNode;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { AnyNode, UnaryExpression } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
/** A unary expression is an expression that consists of a single operand and an operator
|
5
|
+
*
|
6
|
+
* e.g. `+1`, `-2`, `!true`, `typeof x`
|
7
|
+
*/
|
8
|
+
export default class UnaryExpressionNode extends Node<UnaryExpression> {
|
9
|
+
constructor(node: AnyNode, context: Context);
|
10
|
+
run(): unknown;
|
11
|
+
private arithmetic;
|
12
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const Node_1 = __importDefault(require("../Node"));
|
7
|
+
const utils_1 = require("../../utils");
|
8
|
+
/** A unary expression is an expression that consists of a single operand and an operator
|
9
|
+
*
|
10
|
+
* e.g. `+1`, `-2`, `!true`, `typeof x`
|
11
|
+
*/
|
12
|
+
class UnaryExpressionNode extends Node_1.default {
|
13
|
+
constructor(node, context) {
|
14
|
+
super(node, context, 'UnaryExpression');
|
15
|
+
}
|
16
|
+
run() {
|
17
|
+
const argument = (0, utils_1.construct)(this.node.argument, this.context).run();
|
18
|
+
switch (this.node.operator) {
|
19
|
+
case '+':
|
20
|
+
case '-':
|
21
|
+
return this.arithmetic(this.node.operator, argument);
|
22
|
+
case '!':
|
23
|
+
return !argument;
|
24
|
+
case 'typeof':
|
25
|
+
return typeof argument;
|
26
|
+
default:
|
27
|
+
throw new Error(`Unsupported operator: ${this.node.operator}`);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
arithmetic(operator, argument) {
|
31
|
+
if (typeof argument !== 'number') {
|
32
|
+
throw new Error('Unary arithmetic operators are only supported for numbers');
|
33
|
+
}
|
34
|
+
switch (operator) {
|
35
|
+
case '+':
|
36
|
+
return +argument;
|
37
|
+
case '-':
|
38
|
+
return -argument;
|
39
|
+
default:
|
40
|
+
throw new Error(`Unsupported operator: ${operator}`);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
}
|
44
|
+
exports.default = UnaryExpressionNode;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
export { default as ExpressionStatement } from './ExpressionStatement';
|
2
|
+
export { default as ArrayExpression } from './ArrayExpression';
|
3
|
+
export { default as ObjectExpression } from './ObjectExpression';
|
4
|
+
export { default as BinaryExpression } from './BinaryExpression';
|
5
|
+
export { default as UnaryExpression } from './UnaryExpression';
|
6
|
+
export { default as LogicalExpression } from './LogicalExpression';
|
7
|
+
export { default as ConditionalExpression } from './ConditionalExpression';
|
8
|
+
export { default as CallExpression } from './CallExpression';
|
9
|
+
export { default as ArrowFunctionExpression } from './ArrowFunctionExpression';
|
10
|
+
export { default as MemberExpression } from './MemberExpression';
|
11
|
+
export { default as ChainExpression } from './ChainExpression';
|
@@ -0,0 +1,28 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.ChainExpression = exports.MemberExpression = exports.ArrowFunctionExpression = exports.CallExpression = exports.ConditionalExpression = exports.LogicalExpression = exports.UnaryExpression = exports.BinaryExpression = exports.ObjectExpression = exports.ArrayExpression = exports.ExpressionStatement = void 0;
|
7
|
+
var ExpressionStatement_1 = require("./ExpressionStatement");
|
8
|
+
Object.defineProperty(exports, "ExpressionStatement", { enumerable: true, get: function () { return __importDefault(ExpressionStatement_1).default; } });
|
9
|
+
var ArrayExpression_1 = require("./ArrayExpression");
|
10
|
+
Object.defineProperty(exports, "ArrayExpression", { enumerable: true, get: function () { return __importDefault(ArrayExpression_1).default; } });
|
11
|
+
var ObjectExpression_1 = require("./ObjectExpression");
|
12
|
+
Object.defineProperty(exports, "ObjectExpression", { enumerable: true, get: function () { return __importDefault(ObjectExpression_1).default; } });
|
13
|
+
var BinaryExpression_1 = require("./BinaryExpression");
|
14
|
+
Object.defineProperty(exports, "BinaryExpression", { enumerable: true, get: function () { return __importDefault(BinaryExpression_1).default; } });
|
15
|
+
var UnaryExpression_1 = require("./UnaryExpression");
|
16
|
+
Object.defineProperty(exports, "UnaryExpression", { enumerable: true, get: function () { return __importDefault(UnaryExpression_1).default; } });
|
17
|
+
var LogicalExpression_1 = require("./LogicalExpression");
|
18
|
+
Object.defineProperty(exports, "LogicalExpression", { enumerable: true, get: function () { return __importDefault(LogicalExpression_1).default; } });
|
19
|
+
var ConditionalExpression_1 = require("./ConditionalExpression");
|
20
|
+
Object.defineProperty(exports, "ConditionalExpression", { enumerable: true, get: function () { return __importDefault(ConditionalExpression_1).default; } });
|
21
|
+
var CallExpression_1 = require("./CallExpression");
|
22
|
+
Object.defineProperty(exports, "CallExpression", { enumerable: true, get: function () { return __importDefault(CallExpression_1).default; } });
|
23
|
+
var ArrowFunctionExpression_1 = require("./ArrowFunctionExpression");
|
24
|
+
Object.defineProperty(exports, "ArrowFunctionExpression", { enumerable: true, get: function () { return __importDefault(ArrowFunctionExpression_1).default; } });
|
25
|
+
var MemberExpression_1 = require("./MemberExpression");
|
26
|
+
Object.defineProperty(exports, "MemberExpression", { enumerable: true, get: function () { return __importDefault(MemberExpression_1).default; } });
|
27
|
+
var ChainExpression_1 = require("./ChainExpression");
|
28
|
+
Object.defineProperty(exports, "ChainExpression", { enumerable: true, get: function () { return __importDefault(ChainExpression_1).default; } });
|
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.Context = exports.nodes = void 0;
|
7
|
+
const nodeTypes_1 = __importDefault(require("./nodeTypes"));
|
8
|
+
Object.defineProperty(exports, "nodes", { enumerable: true, get: function () { return nodeTypes_1.default; } });
|
9
|
+
const Context_1 = __importDefault(require("../Context"));
|
10
|
+
exports.Context = Context_1.default;
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import { ArrayExpression, ArrowFunctionExpression, BinaryExpression, CallExpression, ChainExpression, ConditionalExpression, ExpressionStatement, LogicalExpression, MemberExpression, ObjectExpression, UnaryExpression } from './expressions';
|
2
|
+
import { AnyNode } from 'acorn';
|
3
|
+
import type Context from '../Context';
|
4
|
+
import Identifier from './Identifier';
|
5
|
+
import Literal from './Literal';
|
6
|
+
import Node from './Node';
|
7
|
+
import Program from './Program';
|
8
|
+
import TemplateLiteral from './TemplateLiteral';
|
9
|
+
type NodeConstructor = {
|
10
|
+
new (node: AnyNode, scope: Context): Node<AnyNode>;
|
11
|
+
};
|
12
|
+
export type NodeMap = Map<AnyNode['type'], NodeConstructor>;
|
13
|
+
declare const nodes: {
|
14
|
+
readonly Program: typeof Program;
|
15
|
+
readonly Literal: typeof Literal;
|
16
|
+
readonly ExpressionStatement: typeof ExpressionStatement;
|
17
|
+
readonly ArrayExpression: typeof ArrayExpression;
|
18
|
+
readonly ObjectExpression: typeof ObjectExpression;
|
19
|
+
readonly Identifier: typeof Identifier;
|
20
|
+
readonly BinaryExpression: typeof BinaryExpression;
|
21
|
+
readonly UnaryExpression: typeof UnaryExpression;
|
22
|
+
readonly LogicalExpression: typeof LogicalExpression;
|
23
|
+
readonly ConditionalExpression: typeof ConditionalExpression;
|
24
|
+
readonly IfStatement: typeof ConditionalExpression;
|
25
|
+
readonly TemplateLiteral: typeof TemplateLiteral;
|
26
|
+
readonly ArrowFunctionExpression: typeof ArrowFunctionExpression;
|
27
|
+
readonly CallExpression: typeof CallExpression;
|
28
|
+
readonly MemberExpression: typeof MemberExpression;
|
29
|
+
readonly ChainExpression: typeof ChainExpression;
|
30
|
+
};
|
31
|
+
export type NodeType = keyof typeof nodes;
|
32
|
+
declare const _default: NodeMap;
|
33
|
+
export default _default;
|
@@ -0,0 +1,29 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const expressions_1 = require("./expressions");
|
7
|
+
const Identifier_1 = __importDefault(require("./Identifier"));
|
8
|
+
const Literal_1 = __importDefault(require("./Literal"));
|
9
|
+
const Program_1 = __importDefault(require("./Program"));
|
10
|
+
const TemplateLiteral_1 = __importDefault(require("./TemplateLiteral"));
|
11
|
+
const nodes = {
|
12
|
+
Program: Program_1.default,
|
13
|
+
Literal: Literal_1.default,
|
14
|
+
ExpressionStatement: expressions_1.ExpressionStatement,
|
15
|
+
ArrayExpression: expressions_1.ArrayExpression,
|
16
|
+
ObjectExpression: expressions_1.ObjectExpression,
|
17
|
+
Identifier: Identifier_1.default,
|
18
|
+
BinaryExpression: expressions_1.BinaryExpression,
|
19
|
+
UnaryExpression: expressions_1.UnaryExpression,
|
20
|
+
LogicalExpression: expressions_1.LogicalExpression,
|
21
|
+
ConditionalExpression: expressions_1.ConditionalExpression,
|
22
|
+
IfStatement: expressions_1.ConditionalExpression,
|
23
|
+
TemplateLiteral: TemplateLiteral_1.default,
|
24
|
+
ArrowFunctionExpression: expressions_1.ArrowFunctionExpression,
|
25
|
+
CallExpression: expressions_1.CallExpression,
|
26
|
+
MemberExpression: expressions_1.MemberExpression,
|
27
|
+
ChainExpression: expressions_1.ChainExpression,
|
28
|
+
};
|
29
|
+
exports.default = new Map(Object.entries(nodes));
|
@@ -0,0 +1,15 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.construct = void 0;
|
4
|
+
const nodes_1 = require("./nodes");
|
5
|
+
function construct(node, context) {
|
6
|
+
if (!context.isNodeAllowed(node.type)) {
|
7
|
+
throw new Error(`Blacklisted Operation: ${node.type}`);
|
8
|
+
}
|
9
|
+
const nodeClass = nodes_1.nodes.get(node.type);
|
10
|
+
if (!nodeClass) {
|
11
|
+
throw new Error(`Unknown node type: ${node.type}`);
|
12
|
+
}
|
13
|
+
return Reflect.construct(nodeClass, [node, context]);
|
14
|
+
}
|
15
|
+
exports.construct = construct;
|