All files / src/rules modify_only_own_db_tables.ts

97.29% Statements 72/74
90.47% Branches 19/21
100% Functions 6/6
97.29% Lines 72/74

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 751x 1x 1x 1x 1x 1x 1x 1x 17845x 17845x 17845x 17845x 17845x 1x 8923x 8923x 8923x 8923x 26615x 26615x 26615x 26615x 26615x 26615x 26615x 26615x 8923x 8923x 8699x 8699x 8923x 8923x 203x 203x 8923x 8923x 221x 221x 221x 221x 12x 12x 209x 209x 209x 221x 1195x 1195x 1195x 1195x 1195x 5x 5x     5x 5x 2x 1x 1x 2x 2x 3x 3x 5x 2x 2x 5x 1195x 209x 209x 209x 8923x 8923x  
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";
 
export class ModifyOnlyOwnDBTablesConf extends BasicRuleConfig {
  public reportDynamic: boolean = true;
  /** Case insensitve regex for own tables */
  public ownTables: string = "^[yz]";
}
 
export class ModifyOnlyOwnDBTables extends ABAPRule {
  private conf = new ModifyOnlyOwnDBTablesConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "modify_only_own_db_tables",
      title: "Modify only own DB tables",
      shortDescription: `Modify only own DB tables`,
      extendedInformation: "https://docs.abapopenchecks.org/checks/26/",
      tags: [RuleTag.Security, RuleTag.SingleFile],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: ModifyOnlyOwnDBTablesConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const output: Issue[] = [];
 
    const struc = file.getStructure();
    if (struc === undefined) {
      return [];
    }
 
    const regExp = new RegExp(this.getConfig().ownTables, "i");
 
    for (const s of file.getStatements()) {
      const g = s.get();
      if (g instanceof Statements.DeleteDatabase
          || g instanceof Statements.UpdateDatabase
          || g instanceof Statements.InsertDatabase
          || g instanceof Statements.ModifyDatabase) {
        const databaseTable = s.findFirstExpression(Expressions.DatabaseTable);
        if (databaseTable === undefined) {
          continue;
        }
 
        if (databaseTable.getFirstChild()?.get() instanceof Expressions.Dynamic) {
          if (this.getConfig().reportDynamic === true) {
            output.push(Issue.atStatement(file, s, this.getMetadata().title, this.getMetadata().key, this.getConfig().severity));
          }
          continue;
        }
 
        const concat = databaseTable.concatTokens().toUpperCase();
        if (regExp.test(concat) === false && concat !== "SCREEN") {
          output.push(Issue.atStatement(file, s, this.getMetadata().title, this.getMetadata().key, this.getConfig().severity));
        }
      }
    }
 
    return output;
  }
 
}