All files / src/rules modify_only_own_db_tables.ts

96.97% Statements 32/33
90.91% Branches 20/22
100% Functions 6/6
96.97% Lines 32/33

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 14531x   14531x     1x 7266x     21666x                   7137x       115x       136x   136x 136x 12x     124x   124x 712x 712x       6x 6x       6x 2x 1x   2x     4x 4x 2x         124x        
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);
        Iif (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;
  }
 
}