Search code examples
javascriptowasp

How can I use grep or awk to count violations of ScanJS rules to assist with security reviews in a text file report?


I am trying to create a script using grep and/or awk that answers the questions listed below (refer to the figures for visual reference)

  1. How many lines within a giant text file, titled “results.txt” contain the text “scanjs-rules/”?
  2. How many of those lines in “results.txt” match up with the first entry in Figure 2?
  3. How many of those lines match up with the second entry in Figure 2? (continue to the end of those list of 108 rules)
  4. How can we create the example report listed in Figure 3?

Motivations: The reports generated by ESLint address each individual file and the potential violations for each file - but from what I understand I can't generate any statistics to help someone perform a secure code review on this.

Note: I used the ESLint config file for ScanJS to generate these text file reports but I want to make them easier to read.

Figures 1-3

Edit:

I formatted the rules so each of them looks like this: accidental_assignment

instead of:
"scanjs-rules/accidental_assignment": 1

Then I ran this command:

while read l; do grep -i "${l//\"/}" results.txt; done < rules.txt

The nice part about this: it prints out each rule violation in alphabetical order, and identifers to help you look through the results.txt file.

Resulting image: link

Desired improvements: I still want it to print out the actual rule and then the count - as shown in Figure 3. Any suggestions?


Solution

  • You should be able to count lines containing searched string with this command :

    cat results.txt | grep <searched> | wc -l 
    

    All together

    for f in `sed -e 's/"\(.*\/\)\(.*\)\(".*\)/\1\2/' pattern.txt`;
      do printf $f' - '; cat result.txt | grep $f | wc -l;
    done
    

    will print result like

    scanjs-rules/accidental_assignment - 2
    scanjs-rules/assign_to_hostname - 2
    scanjs-rules/assign_to_href - 4
    

    Where

    $ cat pattern.txt
    "scanjs-rules/accidental_assignment":1,
    "scanjs-rules/assign_to_hostname":1,
    "scanjs-rules/assign_to_href":1,
    

    and result file sample:

    $cat result.txt
    This is dummy line
    312:9 warrning from scanjs-rules/accidental_assignment
    Another dummy line
    Another dummy line
    Another dummy line
    312:9 warrning from scanjs-rules/assign_to_hostname
    312:9 warrning from scanjs-rules/accidental_assignment
    Another dummy line
    312:9 warrning from scanjs-rules/assign_to_href
    Another dummy line
    312:9 warrning from unsafe scanjs-rules/assign_to_hostname
    312:9 warrning from scanjs-rules/assign_to_href
    312:9 warrning from scanjs-rules/assign_to_href
    312:9 warrning from scanjs-rules/assign_to_href