allowedExtensions) {
89 | this.allowedExtensions = allowedExtensions;
90 | }
91 |
92 | public Integer getMaxFilePathSize() {
93 | return maxFilePathSize;
94 | }
95 |
96 | public void setMaxFilePathSize(Integer maxFilePathSize) {
97 | this.maxFilePathSize = maxFilePathSize;
98 | }
99 |
100 | public Encoder getFileEncoder() {
101 | return fileEncoder;
102 | }
103 |
104 | public void setFileEncoder(Encoder fileEncoder) {
105 | this.fileEncoder = fileEncoder;
106 | }
107 |
108 |
109 |
110 | /**
111 | * Calls getValidDirectoryPath and returns true if no exceptions are thrown.
112 | *
113 | * Note: On platforms that support symlinks, this function will fail canonicalization if directorypath is a symlink. For example, on MacOS X, /etc is actually /private/etc. If you mean
114 | * to use /etc, use its real path (/private/etc), not the symlink (/etc).
115 | *
116 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
117 | * value passed in.
118 | * @param input The actual input data to validate.
119 | * @param parent A File indicating the parent directory into which the input File will be placed.
120 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
121 | *
122 | * @return true if no validation exceptions are thrown
123 | */
124 | public boolean isValidDirectoryPath(String context, String input, File parent, boolean allowNull) {
125 | try {
126 | getValidDirectoryPath(context, input, parent, allowNull);
127 | return true;
128 | } catch (ValidationException e) {
129 | return false;
130 | }
131 | }
132 |
133 | /**
134 | * Calls getValidDirectoryPath and returns true if no exceptions are thrown.
135 | *
136 | * Note: On platforms that support symlinks, this function will fail canonicalization if directorypath is a symlink. For example, on MacOS X, /etc is actually /private/etc. If you mean
137 | * to use /etc, use its real path (/private/etc), not the symlink (/etc).
138 | *
139 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
140 | * value passed in.
141 | * @param input The actual input data to validate.
142 | * @param parent A File indicating the parent directory into which the input File will be placed.
143 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
144 | * @param errors A List to contain any validation errors.
145 | *
146 | * @return true if no validation exceptions are thrown
147 | */
148 | public boolean isValidDirectoryPath(String context, String input, File parent, boolean allowNull, List errors) {
149 | try {
150 | getValidDirectoryPath(context, input, parent, allowNull);
151 | return true;
152 | } catch (ValidationException e) {
153 | errors.add(e);
154 | }
155 |
156 | return false;
157 | }
158 |
159 | /**
160 | * Returns a canonicalized and validated directory path as a String, provided that the input maps to an existing directory that is an existing subdirectory (at any level) of the specified parent.
161 | * Invalid input will generate a descriptive ValidationException, and input that is clearly an attack will generate a descriptive IntrusionException. Instead of throwing a ValidationException on
162 | * error, this variant will store the exception inside of the ValidationErrorList.
163 | *
164 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
165 | * value passed in.
166 | * @param input The actual input data to validate.
167 | * @param parent A File indicating the parent directory into which the input File will be placed.
168 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
169 | *
170 | * @return A valid directory path
171 | *
172 | * @throws ValidationException if validation errors occur
173 | */
174 | public String getValidDirectoryPath(String context, String input, File parent, boolean allowNull) throws ValidationException {
175 | try {
176 | if (Utils.isEmpty(input)) {
177 | if (allowNull) {
178 | return null;
179 | }
180 | throw new ValidationException(context + ": Input directory path required", "Input directory path required: context=" + context + ", input=" + input, context);
181 | }
182 |
183 | File dir = new File(input);
184 |
185 | // check dir exists and parent exists and dir is inside parent
186 | if (!dir.exists()) {
187 | throw new ValidationException(context + ": Invalid directory name", "Invalid directory, does not exist: context=" + context + ", input=" + input);
188 | }
189 | if (!dir.isDirectory()) {
190 | throw new ValidationException(context + ": Invalid directory name", "Invalid directory, not a directory: context=" + context + ", input=" + input);
191 | }
192 | if (!parent.exists()) {
193 | throw new ValidationException(context + ": Invalid directory name", "Invalid directory, specified parent does not exist: context=" + context + ", input=" + input + ", parent=" + parent);
194 | }
195 | if (!parent.isDirectory()) {
196 | throw new ValidationException(context + ": Invalid directory name", "Invalid directory, specified parent is not a directory: context=" + context + ", input=" + input + ", parent=" + parent);
197 | }
198 | if (!dir.getCanonicalPath().startsWith(parent.getCanonicalPath())) {
199 | throw new ValidationException(context + ": Invalid directory name", "Invalid directory, not inside specified parent: context=" + context + ", input=" + input + ", parent=" + parent);
200 | }
201 |
202 | // check canonical form matches input
203 | String canonicalPath = dir.getCanonicalPath();
204 | String canonical = getValidInput(context, canonicalPath, DIRECTORY_NAME_REGEX, maxFilePathSize, false);
205 | if (!canonical.equals(input)) {
206 | throw new ValidationException(context + ": Invalid directory name", "Invalid directory name does not match the canonical path: context=" + context + ", input=" + input + ", canonical=" + canonical, context);
207 | }
208 | return canonical;
209 | } catch (Exception e) {
210 | throw new ValidationException(context + ": Invalid directory name", "Failure to validate directory path: context=" + context + ", input=" + input, e, context);
211 | }
212 | }
213 |
214 | /**
215 | * Calls getValidDirectoryPath with the supplied error List to capture ValidationExceptions
216 | *
217 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
218 | * value passed in.
219 | * @param input The actual input data to validate.
220 | * @param parent A File indicating the parent directory into which the input File will be placed.
221 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
222 | * @param errors A List to contain any validation errors.
223 | *
224 | * @return A valid directory path
225 | */
226 | public String getValidDirectoryPath(String context, String input, File parent, boolean allowNull, List errors) {
227 |
228 | try {
229 | return getValidDirectoryPath(context, input, parent, allowNull);
230 | } catch (ValidationException e) {
231 | errors.add(e);
232 | }
233 |
234 | return "";
235 | }
236 |
237 | /**
238 | * Calls getValidFileName with the default list of allowedExtensions
239 | *
240 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
241 | * value passed in.
242 | * @param input The actual input data to validate.
243 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
244 | *
245 | * @return true if no validation exceptions occur
246 | */
247 | public boolean isValidFileName(String context, String input, boolean allowNull) {
248 | return isValidFileName(context, input, null, allowNull);
249 | }
250 |
251 | /**
252 | * Calls getValidFileName with the default list of allowedExtensions
253 | *
254 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
255 | * value passed in.
256 | * @param input The actual input data to validate.
257 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
258 | * @param errors A List to contain any validation errors.
259 | *
260 | * @return true if no validation exceptions occur
261 | */
262 | public boolean isValidFileName(String context, String input, boolean allowNull, List errors) {
263 | return isValidFileName(context, input, null, allowNull, errors);
264 | }
265 |
266 | /**
267 | * Calls getValidFileName with the default list of allowedExtensions
268 | *
269 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
270 | * value passed in.
271 | * @param input The actual input data to validate.
272 | * @param allowedExtensions A List of allowed file extensions to validate against
273 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
274 | *
275 | * @return true if no validation exceptions occur
276 | */
277 | public boolean isValidFileName(String context, String input, List allowedExtensions, boolean allowNull) {
278 | try {
279 | getValidFileName(context, input, allowedExtensions, allowNull);
280 | return true;
281 | } catch (Exception e) {
282 | return false;
283 | }
284 | }
285 |
286 | /**
287 | * Calls getValidFileName with the default list of allowedExtensions
288 | *
289 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
290 | * value passed in.
291 | * @param input The actual input data to validate.
292 | * @param allowedExtensions A List of allowed file extensions to validate against
293 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
294 | * @param errors A List to contain any validation errors.
295 | *
296 | * @return true if no validation exceptions occur
297 | */
298 | public boolean isValidFileName(String context, String input, List allowedExtensions, boolean allowNull, List errors) {
299 | try {
300 | getValidFileName(context, input, allowedExtensions, allowNull);
301 | return true;
302 | } catch (ValidationException e) {
303 | errors.add(e);
304 | }
305 |
306 | return false;
307 | }
308 |
309 | /**
310 | * Returns a canonicalized and validated file name as a String. Implementors should check for allowed file extensions here, as well as allowed file name characters, as declared in
311 | * "ESAPI.properties". Invalid input will generate a descriptive ValidationException, and input that is clearly an attack will generate a descriptive IntrusionException.
312 | *
313 | * Note: If you do not explicitly specify a white list of allowed extensions, all extensions will be allowed by default.
314 | *
315 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
316 | * value passed in.
317 | * @param input The actual input data to validate.
318 | * @param allowedExtensions A List of allowed file extensions to validate against
319 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
320 | *
321 | * @return A valid file name
322 | *
323 | * @throws ValidationException if validation errors occur
324 | */
325 | public String getValidFileName(String context, String input, List allowedExtensions, boolean allowNull) throws ValidationException {
326 |
327 | String canonical = "";
328 | // detect path manipulation
329 | try {
330 | if (Utils.isEmpty(input)) {
331 | if (allowNull) {
332 | return null;
333 | }
334 | throw new ValidationException(context + ": Input file name required", "Input required: context=" + context + ", input=" + input, context);
335 | }
336 |
337 | // do basic validation
338 | canonical = new File(input).getCanonicalFile().getName();
339 | getValidInput(context, input, FILE_NAME_REGEX, 255, true);
340 |
341 | File f = new File(canonical);
342 | String c = f.getCanonicalPath();
343 | String cpath = c.substring(c.lastIndexOf(File.separator) + 1);
344 |
345 |
346 | // the path is valid if the input matches the canonical path
347 | if (!input.equals(cpath)) {
348 | throw new ValidationException(context + ": Invalid file name", "Invalid directory name does not match the canonical path: context=" + context + ", input=" + input + ", canonical=" + canonical, context);
349 | }
350 |
351 | } catch (IOException e) {
352 | throw new ValidationException(context + ": Invalid file name", "Invalid file name does not exist: context=" + context + ", canonical=" + canonical, e, context);
353 | }
354 |
355 | // verify extensions
356 | if ((allowedExtensions == null) || (allowedExtensions.isEmpty())) {
357 | return canonical;
358 | } else {
359 | Iterator i = allowedExtensions.iterator();
360 | while (i.hasNext()) {
361 | String ext = i.next();
362 | if (input.toLowerCase().endsWith(ext.toLowerCase())) {
363 | return canonical;
364 | }
365 | }
366 | throw new ValidationException(context + ": Invalid file name does not have valid extension ( " + allowedExtensions + ")", "Invalid file name does not have valid extension ( " + allowedExtensions + "): context=" + context + ", input=" + input, context);
367 | }
368 | }
369 |
370 | /**
371 | * Calls getValidFileName with the supplied List to capture ValidationExceptions
372 | *
373 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
374 | * value passed in.
375 | * @param input The actual input data to validate.
376 | * @param allowedExtensions A List of allowed file extensions to validate against
377 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
378 | * @param errors A List to contain any validation errors.
379 | *
380 | * @return A valid file name
381 | */
382 | public String getValidFileName(String context, String input, List allowedExtensions, boolean allowNull, List errors) {
383 | try {
384 | return getValidFileName(context, input, allowedExtensions, allowNull);
385 | } catch (ValidationException e) {
386 | errors.add(e);
387 | }
388 |
389 | return "";
390 | }
391 |
392 | /**
393 | * Calls getValidFileUpload and returns true if no exceptions are thrown.
394 | *
395 | * Note: On platforms that support symlinks, this function will fail canonicalization if directorypath is a symlink. For example, on MacOS X, /etc is actually /private/etc. If you mean
396 | * to use /etc, use its real path (/private/etc), not the symlink (/etc).
397 | *
398 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
399 | * value passed in.
400 | * @param directorypath The file path of the uploaded file.
401 | * @param filename The filename of the uploaded file
402 | * @param parent A File indicating the parent directory into which the input File will be placed.
403 | * @param content A byte array containing the content of the uploaded file.
404 | * @param maxBytes The max number of bytes allowed for a legal file upload.
405 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
406 | *
407 | * @return true if no validation exceptions are thrown
408 | *
409 | * @throws ValidationException if validation errors occur
410 | */
411 | public boolean isValidFileUpload(String context, String directorypath, String filename, File parent, byte[] content, int maxBytes, boolean allowNull) throws ValidationException {
412 | return (isValidFileName(context, filename, allowNull)
413 | && isValidDirectoryPath(context, directorypath, parent, allowNull)
414 | && isValidFileContent(context, content, maxBytes, allowNull));
415 | }
416 |
417 | /**
418 | * Calls getValidFileUpload and returns true if no exceptions are thrown.
419 | *
420 | * Note: On platforms that support symlinks, this function will fail canonicalization if directorypath is a symlink. For example, on MacOS X, /etc is actually /private/etc. If you mean
421 | * to use /etc, use its real path (/private/etc), not the symlink (/etc).
422 | *
423 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
424 | * value passed in.
425 | * @param directorypath The file path of the uploaded file.
426 | * @param filename The filename of the uploaded file
427 | * @param parent A File indicating the parent directory into which the input File will be placed.
428 | * @param content A byte array containing the content of the uploaded file.
429 | * @param maxBytes The max number of bytes allowed for a legal file upload.
430 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
431 | * @param errors A List to contain any validation errors.
432 | *
433 | * @return true if no validation exceptions are thrown
434 | */
435 | public boolean isValidFileUpload(String context, String directorypath, String filename, File parent, byte[] content, int maxBytes, boolean allowNull, List errors) {
436 | return (isValidFileName(context, filename, allowNull, errors)
437 | && isValidDirectoryPath(context, directorypath, parent, allowNull, errors)
438 | && isValidFileContent(context, content, maxBytes, allowNull, errors));
439 | }
440 |
441 | /**
442 | * Validates the filepath, filename, and content of a file. Invalid input will generate a descriptive ValidationException.
443 | *
444 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
445 | * value passed in.
446 | * @param directorypath The file path of the uploaded file.
447 | * @param filename The filename of the uploaded file
448 | * @param parent A File indicating the parent directory into which the input File will be placed.
449 | * @param content A byte array containing the content of the uploaded file.
450 | * @param maxBytes The max number of bytes allowed for a legal file upload.
451 | * @param allowedExtensions A List of allowed file extensions to validate against
452 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
453 | *
454 | * @throws ValidationException if validation errors occur
455 | */
456 | public void assertValidFileUpload(String context, String directorypath, String filename, File parent, byte[] content, int maxBytes, List allowedExtensions, boolean allowNull) throws ValidationException {
457 | getValidFileName(context, filename, allowedExtensions, allowNull);
458 | getValidDirectoryPath(context, directorypath, parent, allowNull);
459 | getValidFileContent(context, content, maxBytes, allowNull);
460 | }
461 |
462 | /**
463 | * Calls getValidFileUpload with the supplied List to capture ValidationExceptions
464 | *
465 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
466 | * value passed in.
467 | * @param directorypath The file path of the uploaded file.
468 | * @param filename The filename of the uploaded file
469 | * @param parent A File indicating the parent directory into which the input File will be placed.
470 | * @param content A byte array containing the content of the uploaded file.
471 | * @param maxBytes The max number of bytes allowed for a legal file upload.
472 | * @param allowedExtensions A List of allowed file extensions to validate against
473 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
474 | * @param errors A List to contain any validation errors.
475 | */
476 | public void assertValidFileUpload(String context, String directorypath, String filename, File parent, byte[] content, int maxBytes, List allowedExtensions, boolean allowNull, List errors) {
477 | try {
478 | assertValidFileUpload(context, directorypath, filename, parent, content, maxBytes, allowedExtensions, allowNull);
479 | } catch (ValidationException e) {
480 | errors.add(e);
481 | }
482 | }
483 |
484 | /**
485 | * Calls getValidFileContent and returns true if no exceptions are thrown.
486 | *
487 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
488 | * value passed in.
489 | * @param input The actual input data to validate.
490 | * @param maxBytes The max number of bytes allowed for a legal file upload.
491 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
492 | *
493 | * @return true if no validation exceptions occur
494 | */
495 | public boolean isValidFileContent(String context, byte[] input, int maxBytes, boolean allowNull) {
496 | try {
497 | getValidFileContent(context, input, maxBytes, allowNull);
498 | return true;
499 | } catch (Exception e) {
500 | return false;
501 | }
502 | }
503 |
504 | /**
505 | * Calls getValidFileContent and returns true if no exceptions are thrown.
506 | *
507 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
508 | * value passed in.
509 | * @param input The actual input data to validate.
510 | * @param maxBytes The max number of bytes allowed for a legal file upload.
511 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
512 | * @param errors A List to contain any validation errors.
513 | *
514 | * @return true if no validation exceptions occur
515 | */
516 | public boolean isValidFileContent(String context, byte[] input, int maxBytes, boolean allowNull, List errors) {
517 | try {
518 | getValidFileContent(context, input, maxBytes, allowNull);
519 | return true;
520 | } catch (ValidationException e) {
521 | errors.add(e);
522 | return false;
523 | }
524 | }
525 |
526 | /**
527 | * Returns validated file content as a byte array. This method checks for max file size (according to the value configured in the maxFileUploadSize class variable)
528 | * and null input ONLY. It can be extended to check for allowed character sets, and do virus scans. Invalid
529 | * input will generate a descriptive ValidationException.
530 | *
531 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
532 | * value passed in.
533 | * @param input The actual input data to validate.
534 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
535 | *
536 | * @return A byte array containing valid file content.
537 | *
538 | * @throws ValidationException if validation errors occur
539 | */
540 | public byte[] getValidFileContent(String context, byte[] input, boolean allowNull) throws ValidationException {
541 | return getValidFileContent(context, input, getMaxFileUploadSize(), allowNull);
542 | }
543 |
544 | /**
545 | * Returns validated file content as a byte array. This method checks for max file size and null input ONLY. It can be extended to check for allowed character sets, and do virus scans. Invalid
546 | * input will generate a descriptive ValidationException.
547 | *
548 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
549 | * value passed in.
550 | * @param input The actual input data to validate.
551 | * @param maxBytes The max number of bytes allowed for a legal file upload.
552 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
553 | *
554 | * @return A byte array containing valid file content.
555 | *
556 | * @throws ValidationException if validation errors occur
557 | */
558 | public byte[] getValidFileContent(String context, byte[] input, long maxBytes, boolean allowNull) throws ValidationException {
559 | if (Utils.isEmpty(input)) {
560 | if (allowNull) {
561 | return null;
562 | }
563 | throw new ValidationException(context + ": Input required", "Input required: context=" + context + ", input=" + Arrays.toString(input), context);
564 | }
565 |
566 | if (input.length > maxBytes) {
567 | throw new ValidationException(context + ": Invalid file content can not exceed " + maxBytes + " bytes", "Exceeded maxBytes ( " + input.length + ")", context);
568 | }
569 |
570 | return input;
571 | }
572 |
573 | /**
574 | * Calls getValidFileContent with the supplied List to capture ValidationExceptions
575 | *
576 | * @param context A descriptive name of the parameter that you are validating (e.g., LoginPage_UsernameField). This value is used by any logging or error handling that is done with respect to the
577 | * value passed in.
578 | * @param input The actual input data to validate.
579 | * @param maxBytes The max number of bytes allowed for a legal file upload.
580 | * @param allowNull If allowNull is true then an input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
581 | * @param errors A List to contain any validation errors.
582 | *
583 | * @return A byte array containing valid file content.
584 | *
585 | * @throws ValidationException if validation errors occur
586 | */
587 | public byte[] getValidFileContent(String context, byte[] input, int maxBytes, boolean allowNull, List errors) throws ValidationException {
588 | try {
589 | return getValidFileContent(context, input, maxBytes, allowNull);
590 | } catch (ValidationException e) {
591 | errors.add(e);
592 | }
593 | // return empty byte array on error
594 | return new byte[0];
595 | }
596 |
597 | /**
598 | * Validates data received from the browser and returns a safe version. Double encoding is treated as an attack. The default encoder supports html encoding, URL encoding, and javascript escaping.
599 | * Input is canonicalized by default before validation.
600 | *
601 | * @param context A descriptive name for the field to validate. This is used for error facing validation messages and element identification.
602 | * @param input The actual user input data to validate.
603 | * @param type The regular expression name which maps to the actual regular expression from "ESAPI.properties".
604 | * @param maxLength The maximum post-canonicalized String length allowed.
605 | * @param allowNull If allowNull is true then a input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
606 | *
607 | * @return The canonicalized user input.
608 | *
609 | * @throws ValidationException if validation errors occur
610 | */
611 | public String getValidInput(String context, String input, String type, int maxLength, boolean allowNull) throws ValidationException {
612 | return getValidInput(context, input, type, maxLength, allowNull, true);
613 | }
614 |
615 | /**
616 | * Validates data received from the browser and returns a safe version. Only URL encoding is supported. Double encoding is treated as an attack.
617 | *
618 | * @param context A descriptive name for the field to validate. This is used for error facing validation messages and element identification.
619 | * @param input The actual user input data to validate.
620 | * @param type The regular expression name which maps to the actual regular expression in the ESAPI validation configuration file
621 | * @param maxLength The maximum String length allowed. If input is canonicalized per the canonicalize argument, then maxLength must be verified after canonicalization
622 | * @param allowNull If allowNull is true then a input that is NULL or an empty string will be legal. If allowNull is false then NULL or an empty String will throw a ValidationException.
623 | * @param canonicalize If canonicalize is true then input will be canonicalized before validation
624 | *
625 | * @return The user input, may be canonicalized if canonicalize argument is true
626 | *
627 | * @throws ValidationException if validation errors occur
628 | */
629 | public String getValidInput(String context, String input, String type, int maxLength, boolean allowNull, boolean canonicalize) throws ValidationException {
630 | StringValidationRule rvr = new StringValidationRule(type, fileEncoder);
631 |
632 | Pattern p = Pattern.compile(type);
633 | rvr.addWhitelistPattern( p );
634 |
635 | rvr.setMaximumLength(maxLength);
636 | rvr.setAllowNull(allowNull);
637 | rvr.setValidateInputAndCanonical(canonicalize);
638 | return rvr.getValid(context, input);
639 | }
640 |
641 | }
--------------------------------------------------------------------------------