All files / src/rules dangerous_statement.ts

94.24% Statements 131/139
79.54% Branches 35/44
100% Functions 8/8
94.24% Lines 131/139

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 1401x 1x 1x 1x 1x 1x 1x 1x 1x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 29366x 1x 14684x 14684x 14684x 14684x 14684x 43917x 43917x 43917x 43917x 43917x 43917x 43917x 43917x 43917x 43917x 43917x 14684x 14684x 9x 9x 14684x 14684x 14120x 14120x 14684x 14684x 265x 265x 14684x 14684x 308x 308x 308x 308x 1686x 1686x 65x 65x 65x 1686x 65x 65x 1686x 1686x 1686x 1x 1686x 1x 1685x 1x 1684x 1x 1683x   1682x   1682x   1682x   1682x   1682x   1682x   1682x   1682x 1682x 1682x 1x 1x 1686x 1686x 5x 5x 1686x 1686x 1684x 1684x 4x 4x 1684x 1686x 308x 308x 308x 14684x 14684x 1684x 1684x 1684x 1684x 1684x 1684x 1684x 11x 11x 4x 4x 11x 1680x 1680x 14684x 14684x  
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {StatementNode} from "../abap/nodes";
 
export class DangerousStatementConf extends BasicRuleConfig {
  /** Detects execSQL (dynamic SQL) */
  public execSQL: boolean = true;
  /** Detects kernel calls */
  public kernelCall: boolean = true;
  /** Detects SYSTEM-CALL */
  public systemCall: boolean = true;
  /** Detects INSERT REPORT */
  public insertReport: boolean = true;
  public generateDynpro: boolean = true;
  public generateReport: boolean = true;
  public generateSubroutine: boolean = true;
  public deleteReport: boolean = true;
  public deleteTextpool: boolean = true;
  public insertTextpool: boolean = true;
  public deleteDynpro: boolean = true;
  public exportDynpro: boolean = true;
  public editorCallForReport: boolean = true;
  /** Finds instances of dynamic SQL: SELECT, UPDATE, DELETE, INSERT, MODIFY */
  public dynamicSQL: boolean = true;
  /** Ignore dynamic SQL in IF_RAP_QUERY_PROVIDER~SELECT implementations */
  public ignoreRAPQueryProvider: boolean = true;
}
 
export class DangerousStatement extends ABAPRule {
 
  private conf = new DangerousStatementConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "dangerous_statement",
      title: "Dangerous statement",
      shortDescription: `Detects potentially dangerous statements`,
      extendedInformation: `Dynamic SQL: Typically ABAP logic does not need dynamic SQL,
dynamic SQL can potentially create SQL injection problems`,
      tags: [RuleTag.SingleFile, RuleTag.Security],
      badExample: `SELECT * FROM (lv_table_name) INTO TABLE @DATA(lt_rows).`,
      goodExample: `SELECT * FROM zknown_table INTO TABLE @DATA(lt_rows).`,
    };
  }
 
  private getDescription(statement: string): string {
    return "Potential dangerous statement " + statement;
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: DangerousStatementConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
    let ignoreDynamicSQL = false;
 
    for (const statementNode of file.getStatements()) {
      const statement = statementNode.get();
      if (statement instanceof Statements.MethodImplementation) {
        const methodName = statementNode.findFirstExpression(Expressions.MethodName)?.concatTokens().toUpperCase();
        ignoreDynamicSQL = this.conf.ignoreRAPQueryProvider === true
          && methodName === "IF_RAP_QUERY_PROVIDER~SELECT";
      } else if (statement instanceof Statements.EndMethod) {
        ignoreDynamicSQL = false;
      }
 
      let message: string | undefined = undefined;
      if (this.conf.execSQL && statement instanceof Statements.ExecSQL) {
        message = "EXEC SQL";
      } else if (this.conf.kernelCall && statement instanceof Statements.CallKernel) {
        message = "KERNEL CALL";
      } else if (this.conf.systemCall && statement instanceof Statements.SystemCall) {
        message = "SYSTEM-CALL";
      } else if (this.conf.insertReport && statement instanceof Statements.InsertReport) {
        message = "INSERT REPORT";
      } else if (this.conf.generateDynpro && statement instanceof Statements.GenerateDynpro) {
        message = "GENERATE DYNPRO";
      } else if (this.conf.generateReport && statement instanceof Statements.GenerateReport) {
        message = "GENERATE REPORT";
      } else if (this.conf.generateSubroutine && statement instanceof Statements.GenerateSubroutine) {
        message = "GENERATE SUBROUTINE";
      } else if (this.conf.deleteReport && statement instanceof Statements.DeleteReport) {
        message = "DELETE REPORT";
      } else if (this.conf.deleteTextpool && statement instanceof Statements.DeleteTextpool) {
        message = "DELETE TEXTPOOL";
      } else if (this.conf.insertTextpool && statement instanceof Statements.InsertTextpool) {
        message = "INSERT TEXTPOOL";
      } else if (this.conf.deleteDynpro && statement instanceof Statements.DeleteDynpro) {
        message = "DELETE DYNPRO";
      } else if (this.conf.exportDynpro && statement instanceof Statements.ExportDynpro) {
        message = "EXPORT DYNPRO";
      } else if (this.conf.editorCallForReport
          && statement instanceof Statements.EditorCall
          && statementNode.findDirectTokenByText("REPORT")) {
        message = "EDITOR-CALL FOR REPORT";
      }
 
      if (message) {
        issues.push(Issue.atStatement(file, statementNode, this.getDescription(message), this.getMetadata().key, this.conf.severity));
      }
 
      if (this.conf.dynamicSQL && ignoreDynamicSQL === false) {
        message = this.findDynamicSQL(statementNode);
        if (message) {
          issues.push(Issue.atStatement(file, statementNode, this.getDescription(message), this.getMetadata().key, this.conf.severity));
        }
      }
    }
 
    return issues;
  }
 
  private findDynamicSQL(statementNode: StatementNode): string | undefined {
    const statement = statementNode.get();
    if (statement instanceof Statements.UpdateDatabase
        || statement instanceof Statements.Select
        || statement instanceof Statements.SelectLoop
        || statement instanceof Statements.InsertDatabase
        || statement instanceof Statements.ModifyDatabase
        || statement instanceof Statements.DeleteDatabase) {
      const dyn = statementNode.findFirstExpression(Expressions.Dynamic);
      if (dyn && dyn.findDirectExpression(Expressions.Constant) === undefined) {
        return "Dynamic SQL";
      }
    }
    return undefined;
  }
 
}