Skip to content

Commit 7672478

Browse files
committed
add new script
1 parent 05173fa commit 7672478

File tree

2 files changed

+88
-28
lines changed

2 files changed

+88
-28
lines changed

docs/8-CODEREVIEW_UTILS.md

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,7 @@ $ semgrep scan --config "p/default" --force-color --text --metrics off --disable
114114

115115
* [Restrictions on values](https://www.w3schools.com/xml/schema_facets.asp).
116116

117-
💻 Useful commands to find type of processing (excludes test related content):
118-
119-
```shell
120-
# Find if input validation is in place into XSD schemas via regular expressions
121-
$ grep -F ":pattern" -rn --exclude-dir=test --include=\*.xsd .
122-
# Find if input validation is in place into XSD schemas via restriction instructions other than regular expressions
123-
$ grep -E ":(enumeration|fractionDigits|length|maxExclusive|maxInclusive|maxLength|minExclusive|minInclusive|minLength|totalDigits)\s+" -rn --exclude-dir=test --include=\*.xsd .
124-
```
117+
💻 Useful script to identify if input validation is in place: See [here](../scripts/identify-input-validation-presence.sh).
125118

126119
### JavaScript
127120

@@ -175,26 +168,7 @@ grep -Frn "@Matches(" --exclude-dir=test --exclude-dir=node_modules .
175168
* [Apache Commons Lang - Javadoc](https://commons.apache.org/proper/commons-lang/apidocs/index.html).
176169
* [Java API for XML Processing security guide](https://docs.oracle.com/en/java/javase/21/security/java-api-xml-processing-jaxp-security-guide.html).
177170

178-
💻 Useful commands to find type of processing (excludes test related content):
179-
180-
```shell
181-
# Find if input validation is in place using beans validation constraints
182-
$ grep -E "@(Pattern|Size|Digits|Email|Negative|Positive|Length|Range)" -rn --exclude-dir=test --include=\*.java .
183-
# Find if input validation is in place using regex
184-
$ grep -F "Pattern" -rn --exclude-dir=test --include=\*.java .
185-
# Find if input validation is in place using regex and focusing on regular expressions defined
186-
$ grep -F "Pattern.compile(" -rn --exclude-dir=test --include=\*.java .
187-
# Find if input validation is in place using Apache Commons-Lang features
188-
$ grep -E "\.(isAlpha|isNumeric|isDigits|isParsable)" -rn --exclude-dir=test --include=\*.java .
189-
# Find if input validation is in place but limited to the presence of a value
190-
$ grep -E "\.(isNull|isEmpty|isBlank|isNotNull|isNotEmpty|isNotBlank|isAllBlank|isAllEmpty|isNoneBlank|isNoneEmpty)" -rn --exclude-dir=test --include=\*.java .
191-
# Identify XML processing to check for exposure to XXE
192-
$ grep -E "(DocumentBuilderFactory|XMLInputFactory|TransformerFactory|JAXBContext)" -rn --exclude-dir=test --include=\*.java .
193-
# Identify cryptography related processing to check for weaknesses in usage/implementation
194-
$ grep -E "(MessageDigest|Cipher|ParameterSpec|SecretKey|PrivateKey|PublicKey|KeyGenerator)" -rn --exclude-dir=test --include=\*.java .
195-
# Identify system command execution
196-
$ grep -F ".exec(" -rn --exclude-dir=test --include=\*.java .
197-
```
171+
💻 Useful script to identify if input validation is in place: See [here](../scripts/identify-input-validation-presence.sh).
198172

199173
💻 Useful commands to find type of files (excludes test related content):
200174

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
####################################################################
3+
# Script to find if input validation is in place across a codebase.
4+
# Precisely if content is inspected/constrained.
5+
####################################################################
6+
WORK="/tmp/work.tmp"
7+
8+
# Utility functions
9+
function write_step(){
10+
echo "🔎 $1"
11+
}
12+
13+
function print_current_location(){
14+
echo "📁 Current location:"
15+
pwd
16+
}
17+
18+
function apply_for_java(){
19+
echo "📦 Count of java files:"
20+
find . -name "*.java" | wc -l
21+
write_step "Find if input validation is in place using beans validation constraints:"
22+
grep --color=always -E "@(Pattern|Size|Digits|Email|Negative|Positive|Length|Range)" -rn --exclude-dir=test --include=\*.java .
23+
echo "---"
24+
write_step "Find if input validation is in place using regex:"
25+
grep --color=always -F "Pattern." -rn --exclude-dir=test --include=\*.java .
26+
echo "---"
27+
write_step "Find if input validation is in place using Apache Commons-Lang features:"
28+
grep --color=always -E "\.(isAlpha|isNumeric|isDigits|isParsable)" -rn --exclude-dir=test --include=\*.java .
29+
}
30+
31+
function apply_for_xsd(){
32+
echo "📦 Count of XSD files:"
33+
find . -name "*.xsd" | wc -l
34+
write_step "Find if input validation is in place via regular expressions:"
35+
grep --color=always -F ":pattern" -rn --exclude-dir=test --include=\*.xsd .
36+
}
37+
38+
function apply_for_openapi(){
39+
echo "📦 Count of OpenAPI descriptor files:"
40+
grep -Fr "openapi:" --exclude-dir=test --include=\*.yml . > $WORK
41+
wc -l $WORK | cut -d' ' -f1
42+
write_step "Find if input validation is in place via regular expressions:"
43+
while IFS= read -r line; do
44+
filepath=$(echo $line | cut -d':' -f1)
45+
openapi_descriptor="$(pwd)/$filepath"
46+
found=$(grep -Fc "pattern:" $openapi_descriptor)
47+
if [ $found -ne 0 ]
48+
then
49+
echo "=> $filepath:"
50+
grep --color=always -Fn "pattern:" $openapi_descriptor
51+
fi
52+
53+
done < $WORK
54+
}
55+
56+
# Entry point
57+
if [ "$#" -lt 1 ]; then
58+
script_name=$(basename "$0")
59+
echo "Usage:"
60+
echo " $script_name (java|xsd|openapi)"
61+
echo ""
62+
echo ""
63+
echo "Call example:"
64+
echo " $script_name java"
65+
echo " $script_name xsd"
66+
echo " $script_name openapi"
67+
exit 1
68+
fi
69+
technology=$1
70+
71+
print_current_location
72+
case "$technology" in
73+
java)
74+
apply_for_java
75+
;;
76+
xsd)
77+
apply_for_xsd
78+
;;
79+
openapi)
80+
apply_for_openapi
81+
;;
82+
*)
83+
echo "❌ Invalid technology specified."
84+
exit 2
85+
;;
86+
esac

0 commit comments

Comments
 (0)