-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCVE-2025-53622-query.ql
More file actions
230 lines (221 loc) · 9.31 KB
/
CVE-2025-53622-query.ql
File metadata and controls
230 lines (221 loc) · 9.31 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* @name Path traversal in DSpace import operations without validation
* @description File paths from external sources are used directly in file operations without proper validation, potentially allowing directory traversal attacks
* @problem.severity error
* @security-severity 7.5
* @precision high
* @tags security
* @kind path-problem
* @id java/dspace-path-traversal-unvalidated
*/
import java
import semmle.code.java.dataflow.DataFlow
import semmle.code.java.dataflow.FlowSources
import semmle.code.java.dataflow.TaintTracking
private import semmle.code.java.dataflow.ExternalFlow
class Source extends DataFlow::Node {
Source() {
// File path parameters in vulnerable methods (focused on actual target methods)
exists(Parameter p |
p.getType() instanceof TypeString and
p.getCallable().getDeclaringType().getName() = "ItemImportServiceImpl" and
// Focus on the exact methods we need to hit
(p.getCallable().getName() = "processContentsFile" and p.getName() = "sFilePath") or
(p.getCallable().getName() = "processContentFileEntry" and p.getName() = "fileName") or
(p.getCallable().getName() = "addDCValue" and p.getName().matches("%path%")) or
(p.getCallable().getName() = "addDCValue" and p.getName().matches("%file%"))
|
this.asParameter() = p
) or
// Bitstream getName() calls in getFile method
exists(MethodCall mc |
mc.getMethod().getName() = "getName" and
mc.getMethod().getDeclaringType().getName().matches("%Bitstream%") and
mc.getEnclosingCallable().getName() = "getFile" and
mc.getEnclosingCallable().getDeclaringType().getName() = "DSBitStoreService"
|
this.asExpr() = mc
) or
// String operations constructing file paths in target methods
exists(AddExpr ae |
ae.getEnclosingCallable().getName() in ["processContentsFile", "processContentFileEntry", "getFile", "addDCValue"] and
// File path construction patterns
(ae.getAnOperand().(StringLiteral).getValue().matches("%/%") or
ae.getAnOperand().(FieldRead).getField().getName() = "separatorChar")
|
this.asExpr() = ae
) or
// Variable reads of potentially tainted file path variables
exists(VarAccess va |
va.getEnclosingCallable().getName() in ["processContentsFile", "processContentFileEntry", "getFile", "addDCValue"] and
(va.getVariable().getName() = "fileName" or
va.getVariable().getName() = "sFilePath" or
va.getVariable().getName() = "fullpath" or
va.getVariable().getName().matches("%path%"))
|
this.asExpr() = va
)
}
}
class Sink extends DataFlow::Node {
Sink() {
// Method calls that perform file operations in target methods
exists(MethodCall mc |
mc.getEnclosingCallable().getName() in ["processContentsFile", "processContentFileEntry", "getFile", "addDCValue"] and
(
// registerBitstream calls in ItemImportServiceImpl
(mc.getMethod().getName() = "registerBitstream" and
mc.getMethod().getDeclaringType().getName().matches("%ItemImportService%")) or
// File constructor calls
(mc.getMethod().getName() = "File" and
mc.getMethod().getDeclaringType().getName() = "File") or
// FileInputStream, FileOutputStream etc.
(mc.getMethod().getName().matches("File%Stream") and
mc.getMethod().getDeclaringType().getName().matches("%File%Stream")) or
// File operations like createNewFile, exists, etc.
(mc.getMethod().getDeclaringType().getName() = "File" and
mc.getMethod().getName() in ["createNewFile", "exists", "delete", "mkdir", "mkdirs"])
)
|
this.asExpr() = mc.getAnArgument()
) or
// Return statements in getFile method (vulnerable pattern)
exists(ReturnStmt ret |
ret.getEnclosingCallable().getName() = "getFile" and
ret.getEnclosingCallable().getDeclaringType().getName() = "DSBitStoreService"
|
this.asExpr() = ret.getResult()
) or
// Assignment to file path variables before operations
exists(Assignment assign |
assign.getEnclosingCallable().getName() in ["processContentsFile", "processContentFileEntry", "getFile", "addDCValue"] and
(assign.getDest().(VarAccess).getVariable().getName() = "fullpath" or
assign.getDest().(VarAccess).getVariable().getName().matches("%path%"))
|
this.asExpr() = assign.getSource()
)
}
}
// Detect path validation - this is the critical sanitizer added in the fix
class PathValidator extends DataFlow::Node {
PathValidator() {
// Direct validateFilePath method calls (the primary fix)
exists(MethodCall mc |
mc.getMethod().getName() = "validateFilePath" and
mc.getMethod().getDeclaringType().getName() = "ItemImportServiceImpl"
|
this.asExpr() = mc.getAnArgument()
) or
// The actual validation pattern from the fix: canonical path with startsWith check
exists(MethodCall canonical, MethodCall startsWith |
canonical.getMethod().getName() = "getCanonicalPath" and
startsWith.getMethod().getName() = "startsWith" and
canonical.getEnclosingCallable() = startsWith.getEnclosingCallable() and
// Make sure this is in the context of a conditional check
exists(IfStmt ifStmt |
ifStmt.getCondition().getAChildExpr*() = startsWith
)
|
this.asExpr() = canonical.getQualifier()
) or
// Path normalization with subsequent startsWith validation (DSBitStoreService fix)
exists(MethodCall normalize, MethodCall startsWith |
normalize.getMethod().getName() = "normalize" and
startsWith.getMethod().getName() = "startsWith" and
normalize.getEnclosingCallable() = startsWith.getEnclosingCallable() and
// Make sure this is in the context of a conditional check
exists(IfStmt ifStmt |
ifStmt.getCondition().getAChildExpr*() = startsWith
)
|
this.asExpr() = normalize.getQualifier()
)
}
}
module MyPathConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) {
source instanceof Source
}
predicate isSink(DataFlow::Node sink) {
sink instanceof Sink
}
predicate isBarrier(DataFlow::Node sanitizer) {
sanitizer instanceof PathValidator
}
predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) {
// String concatenation flow
exists(AddExpr ae |
n1.asExpr() = ae.getAnOperand() and
n2.asExpr() = ae
) or
// Variable assignments
exists(Assignment assign |
n1.asExpr() = assign.getSource() and
n2.asExpr() = assign.getDest()
) or
// Method call argument flow
exists(MethodCall mc, Parameter p |
n1.asExpr() = mc.getAnArgument() and
n2.asParameter() = p and
mc.getMethod().getParameter(_) = p
) or
// File object construction
exists(ConstructorCall cc |
cc.getConstructedType().getName() = "File" and
n1.asExpr() = cc.getAnArgument() and
n2.asExpr() = cc
) or
// StringBuilder/StringBuffer operations
exists(MethodCall mc |
(mc.getMethod().getName() = "append" or mc.getMethod().getName() = "toString") and
n1.asExpr() = mc.getQualifier() and
n2.asExpr() = mc
)
}
}
module MyPathFlow = TaintTracking::Global<MyPathConfig>;
import MyPathFlow::PathGraph
from
MyPathFlow::PathNode source,
MyPathFlow::PathNode sink
where
MyPathFlow::flowPath(source, sink) and
// Only report flows in the target methods we want to detect
exists(Method m |
m.getName() in ["processContentsFile", "processContentFileEntry", "getFile", "addDCValue"] and
(sink.getNode().asExpr().getEnclosingCallable() = m or
source.getNode().asParameter().getCallable() = m or
source.getNode().asExpr().getEnclosingCallable() = m)
) and
// CRITICAL: Exclude flows where any path validation occurs in the same method or calling context
not exists(MethodCall validateCall |
(validateCall.getMethod().getName() = "validateFilePath" or
(validateCall.getMethod().getName() = "getCanonicalPath" and
exists(MethodCall startsWithCall |
startsWithCall.getMethod().getName() = "startsWith" and
startsWithCall.getEnclosingCallable() = validateCall.getEnclosingCallable()
)) or
(validateCall.getMethod().getName() = "normalize" and
exists(MethodCall startsWithCall |
startsWithCall.getMethod().getName() = "startsWith" and
startsWithCall.getEnclosingCallable() = validateCall.getEnclosingCallable()
))) and
(validateCall.getEnclosingCallable() = sink.getNode().asExpr().getEnclosingCallable() or
// Also check if validation happens in a method that's called from the sink method
exists(MethodCall callToValidationMethod |
callToValidationMethod.getEnclosingCallable() = sink.getNode().asExpr().getEnclosingCallable() and
callToValidationMethod.getMethod() = validateCall.getEnclosingCallable()
))
) and
// Additional check: exclude flows where the validateFilePath method exists in the same class (fixed version)
not exists(Method validateMethod |
validateMethod.getName() = "validateFilePath" and
validateMethod.getDeclaringType() = sink.getNode().asExpr().getEnclosingCallable().getDeclaringType()
)
select
sink.getNode(),
source,
sink,
"Unvalidated file path from $@ is used in file operation without proper validation, potentially allowing directory traversal",
source.getNode(),
"external source"