All files / src/rules dangerous_statement.ts

93.22% Statements 110/118
80% Branches 28/35
100% Functions 8/8
93.22% Lines 110/118

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 1191x 1x 1x 1x 1x 1x 1x 1x 1x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 20647x 1x 10324x 10324x 10324x 10324x 10324x 30808x 30808x 30808x 30808x 30808x 30808x 30808x 30808x 30808x 10324x 10324x 7x 7x 10324x 10324x 9811x 9811x 10324x 10324x 241x 241x 10324x 10324x 265x 265x 265x 1451x 1451x 1451x 1x 1451x 1x 1450x 1x 1449x 1x 1448x   1447x   1447x   1447x   1447x   1447x   1447x     1451x 1451x 4x 4x 1451x 1451x 1451x 1451x 3x 3x 1451x 1451x 265x 265x 265x 10324x 10324x 1451x 1451x 1451x 1451x 1451x 1451x 1451x 10x 10x 3x 3x 10x 1448x 1448x 10324x 10324x  
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 deleteDynpro: boolean = true;
  public exportDynpro: boolean = true;
  /** Finds instances of dynamic SQL: SELECT, UPDATE, DELETE, INSERT, MODIFY */
  public dynamicSQL: 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],
    };
  }
 
  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[] = [];
 
    for (const statementNode of file.getStatements()) {
      const statement = statementNode.get();
      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.deleteDynpro && statement instanceof Statements.DeleteDynpro) {
        message = "DELETE DYNPRO";
      } else if (this.conf.exportDynpro && statement instanceof Statements.ExportDynpro) {
        message = "EXPORT DYNPRO";
      }
 
      if (message) {
        issues.push(Issue.atStatement(file, statementNode, this.getDescription(message), this.getMetadata().key, this.conf.severity));
      }
 
      if (this.conf.dynamicSQL) {
        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;
  }
 
}