-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathSafeFile.java
More file actions
107 lines (90 loc) · 3.84 KB
/
SafeFile.java
File metadata and controls
107 lines (90 loc) · 3.84 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* OWASP Enterprise Security API (ESAPI)
*
* This file is part of the Open Web Application Security Project (OWASP)
* Enterprise Security API (ESAPI) project. For details, please see
* <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
*
* Copyright (c) 2008 - The OWASP Foundation
*
* The ESAPI is published by OWASP under the BSD license. You should read and accept the
* LICENSE before you use, modify, and/or redistribute this software.
*
* @author Arshan Dabirsiaghi <a href="http://www.aspectsecurity.com">Aspect Security</a>
* @created 2008
*/
package org.owasp.esapi;
import java.io.File;
import java.net.URI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.owasp.esapi.errors.ValidationException;
/**
* Extension to java.io.File to prevent against null byte injections and
* other unforeseen problems resulting from unprintable characters
* causing problems in path lookups. This does _not_ prevent against
* directory traversal attacks.
*/
public class SafeFile extends File {
private static final long serialVersionUID = 1L;
private static final Pattern PERCENTS_PAT = Pattern.compile("(%)([0-9a-fA-F])([0-9a-fA-F])");
private static final Pattern FILE_BLACKLIST_PAT = Pattern.compile("([\\\\/:*?<>|^])");
private static final Pattern DIR_BLACKLIST_PAT = Pattern.compile("([*?<>|^])");
public SafeFile(String path) throws ValidationException {
super(path);
doDirCheck(this.getParent());
doFileCheck(this.getName());
}
public SafeFile(String parent, String child) throws ValidationException {
super(parent, child);
doDirCheck(this.getParent());
doFileCheck(this.getName());
}
public SafeFile(File parent, String child) throws ValidationException {
super(parent, child);
doDirCheck(this.getParent());
doFileCheck(this.getName());
}
public SafeFile(URI uri) throws ValidationException {
super(uri);
doDirCheck(this.getParent());
doFileCheck(this.getName());
}
private void doDirCheck(String path) throws ValidationException {
Matcher m1 = DIR_BLACKLIST_PAT.matcher( path );
if ( null != m1 && m1.find() ) {
throw new ValidationException( "Invalid directory", "Directory path (" + path + ") contains illegal character: " + m1.group() );
}
Matcher m2 = PERCENTS_PAT.matcher( path );
if (null != m2 && m2.find() ) {
throw new ValidationException( "Invalid directory", "Directory path (" + path + ") contains encoded characters: " + m2.group() );
}
int ch = containsUnprintableCharacters(path);
if (ch != -1) {
throw new ValidationException("Invalid directory", "Directory path (" + path + ") contains unprintable character: " + ch);
}
}
private void doFileCheck(String path) throws ValidationException {
Matcher m1 = FILE_BLACKLIST_PAT.matcher( path );
if ( m1.find() ) {
throw new ValidationException( "Invalid directory", "Directory path (" + path + ") contains illegal character: " + m1.group() );
}
Matcher m2 = PERCENTS_PAT.matcher( path );
if ( m2.find() ) {
throw new ValidationException( "Invalid file", "File path (" + path + ") contains encoded characters: " + m2.group() );
}
int ch = containsUnprintableCharacters(path);
if (ch != -1) {
throw new ValidationException("Invalid file", "File path (" + path + ") contains unprintable character: " + ch);
}
}
private int containsUnprintableCharacters(String s) {
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (((int) ch) < 32 || ((int) ch) > 126) {
return (int) ch;
}
}
return -1;
}
}