Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 42237x 1x 21120x 21120x 21120x 21120x 21120x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 83313x 21120x 21120x 20205x 20205x 21120x 21120x 393x 393x 21120x 21120x 483x 483x 483x 2558x 2558x 6x 6x 6x 6x 6x 2552x 2552x 2558x 51x 51x 51x 2558x 483x 483x 483x 21120x 21120x 2552x 2552x 2552x 2552x 13x 13x 2539x 2552x 4x 4x 1x 1x 2552x 1x 1x 1x 2535x 3x 2x 2x 2534x 2x 2x 2x 1x 1x 2531x 6x 3x 3x 2529x 3x 2x 2x 2523x 7x 3x 3x 2520x 7x 2x 2x 2513x 2x 2x 2x 2506x 2504x 25x 25x 9x 9x 2504x 4x 2x 2x 2479x 1x 1x 1x 2475x 2474x 2474x 2474x 2474x 2474x 2474x 2474x 16x 9x 9x 16x 2501x 2501x 2501x 21120x 21120x 21120x 21120x 21120x 2558x 2558x 2558x 2541x 2541x 17x 17x 2558x 2558x 7x 7x 10x 10x 10x 10x 2558x 6x 2558x 4x 4x 10x 2558x 4x 4x 6x 6x 21120x 21120x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 4x 4x 21120x 21120x 2532x 15x 15x 15x 13x 13x 15x 2519x 2519x 21120x 21120x 3x 3x 3x 21120x 21120x 59x 59x 21120x 21120x 5x 5x 21120x 21120x 7x 7x 21120x 21120x | import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Issue} from "../issue";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ExpressionNode, StatementNode} from "../abap/nodes";
import {ABAPObject} from "../objects/_abap_object";
import {LanguageVersion} from "../version";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ClassAttribute} from "../abap/types/class_attribute";
import {IdentifierMeta} from "../abap/types/_typed_identifier";
export class NoDynamicStuffConf extends BasicRuleConfig {
/** Detects dynamic method calls, ie. CALL METHOD (name), SET HANDLER and CALL BADI */
public callMethod: boolean = true;
/** Detects dynamic CALL FUNCTION, ie. the function module name is not a literal */
public callFunction: boolean = true;
/** Detects CALL DATABASE PROCEDURE, the procedure name is always dynamic */
public callDatabaseProcedure: boolean = true;
/** Detects dynamic CALL TRANSFORMATION */
public callTransformation: boolean = true;
/** Detects dynamic CALL TRANSACTION, ie. the transaction code is not a literal */
public callTransaction: boolean = true;
/** Detects dynamic PERFORM */
public perform: boolean = true;
/** Detects dynamic SUBMIT */
public submit: boolean = true;
/** Detects dynamic CREATE OBJECT */
public createObject: boolean = true;
/** Detects dynamic CREATE DATA */
public createData: boolean = true;
/** Detects dynamic GET BADI */
public getBadi: boolean = true;
/** Detects dynamic ASSIGN, including ASSIGN COMPONENT */
public assign: boolean = true;
/** Detects dynamic internal table access, ie. dynamic WHERE, keys, SORT BY and TRANSPORTING */
public internalTable: boolean = true;
/** Detects dynamic EXPORT and IMPORT */
public exportImport: boolean = true;
/** Only with syntax version Cloud: detects the old variant of the dynamic ASSIGN, ie. ASSIGN (name) where name is a
* literal or constant holding a path with a selector, eg. 'MO_APP->ATTR'. Reported also when "assign" is off */
public assignOldVariantCloud: boolean = true;
}
export class NoDynamicStuff extends ABAPRule {
private conf = new NoDynamicStuffConf();
public getMetadata(): IRuleMetadata {
return {
key: "no_dynamic_stuff",
title: "No dynamic stuff",
shortDescription: `Detects dynamic calls and other dynamic language constructs`,
extendedInformation: `Dynamic constructs cannot be checked statically, they are not found by
where-used lists and refactorings, and they can introduce injection vulnerabilities.
Dynamic tokens are also reported when containing a literal, eg. CALL METHOD go_calendar->('RESET_DAY_INFO'),
the syntax check does not resolve these either, so they behave like any other dynamic token.
Dynamic SQL is reported by rule dangerous_statement.`,
tags: [RuleTag.SingleFile, RuleTag.Security],
badExample: `CALL METHOD (lv_class)=>(lv_method).
CALL METHOD go_calendar->('RESET_DAY_INFO').
CALL FUNCTION lv_function_module.
CREATE OBJECT ref TYPE (lv_class).
ASSIGN (lv_name) TO <fs>.
SORT tab BY (lv_field).`,
goodExample: `cl_class=>method( ).
go_calendar->reset_day_info( ).
CALL FUNCTION 'ZFOO'.
CREATE OBJECT ref TYPE cl_class.
ASSIGN foo TO <fs>.
SORT tab BY field.`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: NoDynamicStuffConf): void {
this.conf = conf;
}
public runParsed(file: ABAPFile, obj: ABAPObject) {
const issues: Issue[] = [];
for (const statementNode of file.getStatements()) {
const path = this.assignOldVariantPath(statementNode, file, obj);
if (path !== undefined) {
const message = `Old variant of dynamic ASSIGN, path ${path} given as text should not be used in ABAP for Cloud Development, ` +
`use oref->(attr), struc-(comp), class=>(attr) or dref->* instead`;
issues.push(Issue.atStatement(file, statementNode, message, this.getMetadata().key, this.conf.severity));
continue;
}
const found = this.check(statementNode);
if (found !== undefined) {
issues.push(Issue.atStatement(file, statementNode, "Dynamic " + found,
this.getMetadata().key, this.conf.severity));
}
}
return issues;
}
private check(node: StatementNode): string | undefined {
const statement = node.get();
// note that MethodSource is also used by SET HANDLER and CALL BADI
if (this.conf.callMethod === true && this.dynamicMethodSource(node) === true) {
return "method call";
}
if (statement instanceof Statements.CallFunction) {
if (this.conf.callFunction === true
&& this.notLiteral(node.findDirectExpression(Expressions.FunctionName))) {
return "CALL FUNCTION";
}
} else if (statement instanceof Statements.CallDatabase) {
if (this.conf.callDatabaseProcedure === true) {
return "CALL DATABASE PROCEDURE";
}
} else if (statement instanceof Statements.CallTransformation) {
if (this.conf.callTransformation === true && this.hasDynamic(node)) {
return "CALL TRANSFORMATION";
}
} else if (statement instanceof Statements.CallTransaction) {
// the transaction code is a Source, ie. there is no parenthesized dynamic variant
if (this.conf.callTransaction === true
&& this.notLiteral(node.findDirectExpression(Expressions.Source))) {
return "CALL TRANSACTION";
}
} else if (statement instanceof Statements.Perform) {
if (this.conf.perform === true && this.hasDynamic(node)) {
return "PERFORM";
}
} else if (statement instanceof Statements.Submit) {
if (this.conf.submit === true && this.hasDynamic(node)) {
return "SUBMIT";
}
} else if (statement instanceof Statements.CreateObject) {
if (this.conf.createObject === true && this.hasDynamic(node)) {
return "CREATE OBJECT";
}
} else if (statement instanceof Statements.CreateData) {
if (this.conf.createData === true && this.hasDynamic(node)) {
return "CREATE DATA";
}
} else if (statement instanceof Statements.GetBadi) {
if (this.conf.getBadi === true && this.hasDynamic(node)) {
return "GET BADI";
}
} else if (statement instanceof Statements.Assign
|| statement instanceof Statements.AssignLocalCopy) {
if (this.conf.assign === true
&& (this.hasDynamic(node) || this.dynamicAssignComponent(node))) {
return "ASSIGN";
}
} else if (statement instanceof Statements.Export) {
if (this.conf.exportImport === true && this.hasDynamic(node)) {
return "EXPORT";
}
} else if (statement instanceof Statements.Import) {
if (this.conf.exportImport === true && this.hasDynamic(node)) {
return "IMPORT";
}
} else if (statement instanceof Statements.At
|| statement instanceof Statements.DeleteInternal
|| statement instanceof Statements.InsertInternal
|| statement instanceof Statements.Loop
|| statement instanceof Statements.ModifyInternal
|| statement instanceof Statements.ReadTable
|| statement instanceof Statements.Sort
|| statement instanceof Statements.SortDataset) {
if (this.conf.internalTable === true && this.hasDynamic(node)) {
return "internal table access";
}
}
return undefined;
}
/** ABAP for Cloud Development warns about the old variant of the dynamic data object, ie. ASSIGN (name) where the
* text in name is a whole operand including selectors. Only reported when the text is known: a literal or a constant.
* Returns the text, or undefined if not the old variant */
private assignOldVariantPath(node: StatementNode, file: ABAPFile, obj: ABAPObject): string | undefined {
if (this.conf.assignOldVariantCloud !== true
|| this.reg.getConfig().getLanguageVersion() !== LanguageVersion.Cloud
|| !(node.get() instanceof Statements.Assign)) {
return undefined;
}
// the old variant is a single Dynamic, forms like (class)=>(attr) have more children
const children = node.findDirectExpression(Expressions.AssignSource)?.getChildren() || [];
if (children.length !== 1 || !(children[0] instanceof ExpressionNode) || !(children[0].get() instanceof Expressions.Dynamic)) {
return undefined;
}
let text: string | undefined = undefined;
const literal = children[0].findDirectExpression(Expressions.Constant);
const name = children[0].findDirectExpression(Expressions.FieldChain);
if (literal) {
text = literal.concatTokens();
} else if (name?.getChildren().length === 1) {
text = this.constantValue(name, file, obj);
}
if (text === undefined || !(text.includes("->") || text.includes("=>") || text.includes("-"))) {
return undefined;
}
return text;
}
private constantValue(name: ExpressionNode, file: ABAPFile, obj: ABAPObject): string | undefined {
const token = name.getFirstToken();
const scope = new SyntaxLogic(this.reg, obj).run().spaghetti.lookupPosition(token.getStart(), file.getFilename());
const found = scope?.findVariable(token.getStr());
// class attributes can be READ-ONLY with a VALUE, but are not constants
if (found === undefined
|| found instanceof ClassAttribute
|| found.getMeta().includes(IdentifierMeta.ReadOnly) === false
|| found.getMeta().includes(IdentifierMeta.Static) === false) {
return undefined;
}
const value = found.getValue();
return typeof value === "string" ? value : undefined;
}
private dynamicMethodSource(node: StatementNode): boolean {
for (const source of node.findAllExpressions(Expressions.MethodSource)) {
// Dynamic is always a direct child of MethodSource, dynamics further down
// are parameters of the method call and not part of the method name
if (source.findDirectExpression(Expressions.Dynamic) !== undefined) {
return true;
}
}
return false;
}
private dynamicAssignComponent(node: StatementNode): boolean {
const component = node.findDirectExpression(Expressions.AssignSource)?.findExpressionAfterToken("COMPONENT");
return component !== undefined && this.isLiteral(component) === false;
}
private hasDynamic(node: StatementNode): boolean {
return node.findFirstExpression(Expressions.Dynamic) !== undefined;
}
private notLiteral(node: ExpressionNode | undefined): boolean {
return node !== undefined && this.isLiteral(node) === false;
}
private isLiteral(node: ExpressionNode): boolean {
return node.findDirectExpression(Expressions.Constant) !== undefined;
}
}
|