You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Apply a collection of validation to verify if a provided PIN code is considered weak (easy to guess) or none.<br>
82
82
* This method consider that format of the PIN code is [0-9]{6,}<br>
83
-
* Rule to consider a PIN code as weak:<br>
84
-
* - Length is inferior to 6 positions.<br>
85
-
* - Contain only the same number or only a sequence of zero.<br>
86
-
* - Contain sequence of following incremental or decremental numbers.<br>
83
+
* Rule to consider a PIN code as weak:
84
+
* <ul>
85
+
* <li>Length is inferior to 6 positions.</li>
86
+
* <li>Contain only the same number or only a sequence of zero.</li>
87
+
* <li>Contain sequence of following incremental or decremental numbers.</li>
88
+
* </ul>
87
89
*
88
90
* @param pinCode PIN code to verify.
89
91
* @return True only if the PIN is considered as weak.
@@ -120,10 +122,12 @@ public static boolean isWeakPINCode(String pinCode) {
120
122
}
121
123
122
124
/**
123
-
* Apply a collection of validations on a Word 97-2003 (binary format) document file provided:<br>
124
-
* - Real Microsoft Word 97-2003 document file.<br>
125
-
* - No VBA Macro.<br>
126
-
* - No embedded objects.<br>
125
+
* Apply a collection of validations on a Word 97-2003 (binary format) document file provided:
126
+
* <ul>
127
+
* <li>Real Microsoft Word 97-2003 document file.</li>
128
+
* <li>No VBA Macro.<br></li>
129
+
* <li>No embedded objects.</li>
130
+
* </ul>
127
131
*
128
132
* @param wordFilePath Filename of the Word document file to check.
129
133
* @return True only if the file pass all validations.
@@ -269,11 +273,13 @@ public boolean accept(PDAnnotation annotation) {
269
273
}
270
274
271
275
/**
272
-
* Apply a collection of validations on a PDF file provided:<br>
273
-
* - Real PDF file<br>
274
-
* - No attachments.<br>
275
-
* - No Javascript code.<br>
276
-
* - No links using action of type URI/Launch/RemoteGoTo/ImportData.<br>
276
+
* Apply a collection of validations on a PDF file provided:
277
+
* <ul>
278
+
* <li>Real PDF file.</li>
279
+
* <li>No attachments.</li>
280
+
* <li>No Javascript code.</li>
281
+
* <li>No links using action of type URI/Launch/RemoteGoTo/ImportData.</li>
282
+
* </ul>
277
283
*
278
284
* @param pdfFilePath Filename of the PDF file to check.
279
285
* @return True only if the file pass all validations.
@@ -377,10 +383,12 @@ public static boolean isRelativeURL(String targetUrl) {
377
383
}
378
384
379
385
/**
380
-
* Apply a collection of validations on a ZIP file provided:<br>
381
-
* - Real ZIP file<br>
382
-
* - Contain less than a specified level of deepness.<br>
383
-
* - Do not contain Zip-Slip entry path.<br>
386
+
* Apply a collection of validations on a ZIP file provided:
387
+
* <ul>
388
+
* <li>Real ZIP file.</li>
389
+
* <li>Contain less than a specified level of deepness.</li>
390
+
* <li>Do not contain Zip-Slip entry path.</li>
391
+
* </ul>
384
392
*
385
393
* @param zipFilePath Filename of the ZIP file to check.
386
394
* @param maxLevelDeepness Threshold of deepness above which a ZIP archive will be rejected.
@@ -479,11 +487,14 @@ public static String identifyMimeType(byte[] content) {
479
487
}
480
488
481
489
/**
482
-
* Apply a collection of validations on a string expected to be an public IP address:<br>
483
-
* - Is a valid IP v4 or v6 address.<br>
484
-
* - Is public from an Internet perspective.<br><br>
485
-
* <b>Note:</b> I often see missing such validation in the value read from HTTP request headers like "X-Forwarded-For" or "Forwarded".
490
+
* Apply a collection of validations on a string expected to be an public IP address:
491
+
* <ul>
492
+
* <li>Is a valid IP v4 or v6 address.</li>
493
+
* <li>Is public from an Internet perspective.</li>
494
+
* </ul>
486
495
* <br>
496
+
* <b>Note:</b> I often see missing such validation in the value read from HTTP request headers like "X-Forwarded-For" or "Forwarded".
497
+
* <br><br>
487
498
* <b>Note for IPv6:</b> I used documentation found so it is really experimental!
488
499
*
489
500
* @param ip String expected to be a valid IP address.
@@ -553,9 +564,12 @@ public static boolean isPublicIPAddress(String ip) {
553
564
/**
554
565
* Compute a SHA256 hash from an input composed of a collection of strings.<br><br>
555
566
* This method take care to build the source string in a way to prevent this source string to be prone to abuse targeting the different parts composing it.<br><br>
567
+
* <p>
556
568
* Example of possible abuse without precautions applied during the hash calculation logic:<br>
557
-
* Hash of <code>SHA256("Hello", "My", "World!!!")</code> will be equals to the hash of <code>SHA256("Hell", "oMyW", "orld!!!")</code>.<br><br>
569
+
* Hash of <code>SHA256("Hello", "My", "World!!!")</code> will be equals to the hash of <code>SHA256("Hell", "oMyW", "orld!!!")</code>.<br>
570
+
* </p>
558
571
* This method ensure that both hash above will be different.<br><br>
572
+
*
559
573
* <b>Note:</b> The character <code>|</code> is used, as separator, of every parts so a part is not allowed to contains this character.
560
574
*
561
575
* @param parts Ordered list of strings to use to build the input string for which the hash must be computed on. No null value is accepted on object composing the collection.
@@ -644,9 +658,11 @@ public static boolean isXMLOnlyUseAllowedXSDorDTD(String xmlFilePath, final List
644
658
}
645
659
646
660
/**
647
-
* Apply a collection of validations on a EXCEL CSV file provided (file was expected to be opened in Microsoft EXCEL):<br>
648
-
* - Real CSV file.<br>
649
-
* - Do not contains any payload related to a CSV injections.<br><br>
661
+
* Apply a collection of validations on a EXCEL CSV file provided (file was expected to be opened in Microsoft EXCEL):
662
+
* <ul>
663
+
* <li>Real CSV file.</li>
664
+
* <li>Do not contains any payload related to a CSV injections.</li>
665
+
* </ul>
650
666
* Ensure that, if Apache Commons CSV does not find any record then, the file will be considered as NOT safe (prevent potential bypasses).<br><br>
651
667
* <b>Note:</b> Record delimiter used is the <code>,</code> (comma) character. See the Apache Commons CSV reference provided for EXCEL.<br>
652
668
*
@@ -754,11 +770,13 @@ public static Map<String, Object> ensureSerializedObjectIntegrity(ProcessingMode
754
770
}
755
771
756
772
/**
757
-
* Apply a collection of validations on a JSON string provided:<br>
758
-
* - Real JSON structure.<br>
759
-
* - Contain less than a specified number of deepness for nested objects or arrays.<br>
760
-
* - Contain less than a specified number of items in any arrays.<br><br>
761
-
*
773
+
* Apply a collection of validations on a JSON string provided:
774
+
* <ul>
775
+
* <li>Real JSON structure.</li>
776
+
* <li>Contain less than a specified number of deepness for nested objects or arrays.</li>
777
+
* <li>Contain less than a specified number of items in any arrays.</li>
778
+
* </ul>
779
+
* <br>
762
780
* <b>Note:</b> I decided to use a parsing approach using only string processing to prevent any StackOverFlow or OutOfMemory error that can be abused.<br><br>
763
781
* I used the following assumption:
764
782
* <ul>
@@ -863,11 +881,12 @@ public static boolean isJSONSafe(String json, int maxItemsByArraysCount, int max
863
881
}
864
882
865
883
/**
866
-
* Apply a collection of validations on a image file provided:<br>
867
-
* - Real image file.<br>
868
-
* - Its mime type is into the list of allowed mime types.<br>
869
-
* - Its metadata fields do not contains any characters related to a malicious payloads.<br>
870
-
*
884
+
* Apply a collection of validations on a image file provided:
885
+
* <ul>
886
+
* <li>Real image file.</li>
887
+
* <li>Its mime type is into the list of allowed mime types.</li>
888
+
* <li>Its metadata fields do not contains any characters related to a malicious payloads.</li>
889
+
* </ul>
871
890
* <br>
872
891
* <b>Important note:</b> This implementation is prone to bypass using the "<b>raw insertion</b>" method documented in the <a href="https://www.synacktiv.com/en/publications/persistent-php-payloads-in-pngs-how-to-inject-php-code-in-an-image-and-keep-it-there">blog post</a> from the Synacktiv team.
873
892
* To handle such case, it is recommended to resize the image to remove any non image-related content, see <a href="https://github.com/righettod/document-upload-protection/blob/master/src/main/java/eu/righettod/poc/sanitizer/ImageDocumentSanitizerImpl.java#L54">here</a> for an example.<br>
* Apply a collection of validations on a string expected to be an email address:<br>
986
-
* - Is a valid email address, from a parser perspective, following RFCs on email addresses.<br>
987
-
* - Is not using "Encoded-word" format.<br>
988
-
* - Is not using comment format.<br>
989
-
* - Is not using "Punycode" format.<br>
990
-
* - Is not using UUCP style addresses.<br>
991
-
* - Is not using address literals.<br>
992
-
* - Is not using source routes.<br>
993
-
* - Is not using the "percent hack".<br><br>
1004
+
* Apply a collection of validations on a string expected to be an email address:
1005
+
* <ul>
1006
+
* <li>Is a valid email address, from a parser perspective, following RFCs on email addresses.</li>
1007
+
* <li>Is not using "Encoded-word" format.</li>
1008
+
* <li>Is not using comment format.</li>
1009
+
* <li>Is not using "Punycode" format.</li>
1010
+
* <li>Is not using UUCP style addresses.</li>
1011
+
* <li>Is not using address literals.</li>
1012
+
* <li>Is not using source routes.</li>
1013
+
* <li>Is not using the "percent hack".</li>
1014
+
* </ul><br>
994
1015
* This is based on the research work from <a href="https://portswigger.net/research/gareth-heyes">Gareth Heyes</a> added in references (Portswigger).<br><br>
995
1016
*
996
1017
* <b>Note:</b> The notion of valid, here, is to take from a secure usage of the data perspective.
@@ -1131,4 +1152,39 @@ public static String applyURLDecoding(String encodedData, int decodingRoundThres
1131
1152
}
1132
1153
returncurrentRoundData;
1133
1154
}
1155
+
1156
+
/**
1157
+
* Apply a collection of validations on a string expected to be an system file/folder path:
1158
+
* <ul>
1159
+
* <li>Does not contains path traversal payload.</li>
1160
+
* </ul><br>
1161
+
* <p>
1162
+
* <b>Note:</b> This implementation is sensitive to the current folder expression <code>./</code> and <code>.\</code> - Therefore <b>it will consider the path as unsafe</b> when it contains such expression.
1163
+
* </p>
1164
+
*
1165
+
* @param path String expected to be a valid system file/folder path.
1166
+
* @return True only if the string pass all validations.
0 commit comments