File: DirResourceSet.java

package info (click to toggle)
tomcat11 11.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 46,360 kB
  • sloc: java: 366,026; xml: 55,052; jsp: 4,700; sh: 1,304; perl: 314; makefile: 25; ansic: 15
file content (447 lines) | stat: -rw-r--r-- 18,115 bytes parent folder | download | duplicates (3)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.catalina.webresources;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.jar.Manifest;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResource;
import org.apache.catalina.WebResourceLockSet;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.WebResourceRoot.ResourceSetType;
import org.apache.catalina.util.ResourceSet;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.compat.JreCompat;
import org.apache.tomcat.util.http.RequestUtil;

/**
 * Represents a {@link org.apache.catalina.WebResourceSet} based on a directory.
 */
public class DirResourceSet extends AbstractFileResourceSet implements WebResourceLockSet {

    private static final Log log = LogFactory.getLog(DirResourceSet.class);

    private final Map<String,ResourceLock> resourceLocksByPath = new HashMap<>();
    private final Object resourceLocksByPathLock = new Object();


    /**
     * A no argument constructor is required for this to work with the digester.
     */
    public DirResourceSet() {
        super("/");
    }

    /**
     * Creates a new {@link org.apache.catalina.WebResourceSet} based on a directory.
     *
     * @param root         The {@link WebResourceRoot} this new {@link org.apache.catalina.WebResourceSet} will be added
     *                         to.
     * @param webAppMount  The path within the web application at which this {@link org.apache.catalina.WebResourceSet}
     *                         will be mounted. For example, to add a directory of JARs to a web application, the
     *                         directory would be mounted at "/WEB-INF/lib/"
     * @param base         The absolute path to the directory on the file system from which the resources will be
     *                         served.
     * @param internalPath The path within this new {@link org.apache.catalina.WebResourceSet} where resources will be
     *                         served from.
     */
    public DirResourceSet(WebResourceRoot root, String webAppMount, String base, String internalPath) {
        super(internalPath);
        setRoot(root);
        setWebAppMount(webAppMount);
        setBase(base);

        if (root.getContext().getAddWebinfClassesResources()) {
            File f = new File(base, internalPath);
            f = new File(f, "/WEB-INF/classes/META-INF/resources");

            if (f.isDirectory()) {
                root.createWebResourceSet(ResourceSetType.RESOURCE_JAR, "/", f.getAbsolutePath(), null, "/");
            }
        }

        if (getRoot().getState().isAvailable()) {
            try {
                start();
            } catch (LifecycleException e) {
                throw new IllegalStateException(e);
            }
        }
    }


    @SuppressWarnings("null") // lock can never be null when lock.key is read
    @Override
    public WebResource getResource(String path) {
        checkPath(path);
        String webAppMount = getWebAppMount();
        WebResourceRoot root = getRoot();
        boolean readOnly = isReadOnly();
        if (path.startsWith(webAppMount)) {
            /*
             * Lock the path for reading until the WebResource has been constructed. The lock prevents concurrent reads
             * and writes (e.g. HTTP GET and PUT / DELETE) for the same path causing corruption of the FileResource
             * where some of the fields are set as if the file exists and some as set as if it does not.
             */
            ResourceLock lock = readOnly ? null : lockForRead(path);
            try {
                File f = file(path.substring(webAppMount.length()), false);
                if (f == null) {
                    return new EmptyResource(root, path);
                }
                if (!f.exists()) {
                    return new EmptyResource(root, path, f);
                }
                if (f.isDirectory() && path.charAt(path.length() - 1) != '/') {
                    path = path + '/';
                }
                return new FileResource(root, path, f, readOnly, getManifest(), this, readOnly ? null : lock.key);
            } finally {
                if (!readOnly) {
                    unlockForRead(lock);
                }
            }
        } else {
            return new EmptyResource(root, path);
        }
    }


    @Override
    public String[] list(String path) {
        checkPath(path);
        String webAppMount = getWebAppMount();
        if (path.startsWith(webAppMount)) {
            File f = file(path.substring(webAppMount.length()), true);
            if (f == null) {
                return EMPTY_STRING_ARRAY;
            }
            String[] result = f.list();
            return Objects.requireNonNullElse(result, EMPTY_STRING_ARRAY);
        } else {
            if (!path.endsWith("/")) {
                path = path + "/";
            }
            if (webAppMount.startsWith(path)) {
                int i = webAppMount.indexOf('/', path.length());
                if (i == -1) {
                    return new String[] { webAppMount.substring(path.length()) };
                } else {
                    return new String[] { webAppMount.substring(path.length(), i) };
                }
            }
            return EMPTY_STRING_ARRAY;
        }
    }

    @Override
    public Set<String> listWebAppPaths(String path) {
        checkPath(path);
        String webAppMount = getWebAppMount();
        ResourceSet<String> result = new ResourceSet<>();
        if (path.startsWith(webAppMount)) {
            File f = file(path.substring(webAppMount.length()), true);
            if (f != null) {
                File[] list = f.listFiles();
                if (list != null) {
                    for (File entry : list) {
                        // f has already been validated so the following checks
                        // can be much simpler than those in file()
                        if (!getRoot().getAllowLinking()) {
                            // allow linking is disabled so need to check for
                            // symlinks
                            boolean symlink = true;
                            String absPath = null;
                            String canPath = null;
                            try {
                                // We know that 'f' must be valid since it will
                                // have been checked in the call to file()
                                // above. Therefore strip off the path of the
                                // path that was contributed by 'f' and check
                                // that what is left does not contain a symlink.
                                absPath = entry.getAbsolutePath().substring(f.getAbsolutePath().length());
                                String entryCanPath = entry.getCanonicalPath();
                                String fCanPath = f.getCanonicalPath();
                                if (entryCanPath.length() >= fCanPath.length()) {
                                    canPath = entryCanPath.substring(fCanPath.length());
                                    if (absPath.equals(canPath)) {
                                        symlink = false;
                                    }
                                }
                            } catch (IOException ioe) {
                                // Ignore the exception. Assume we have a symlink.
                                canPath = "Unknown";
                            }
                            if (symlink) {
                                logIgnoredSymlink(getRoot().getContext().getName(), absPath, canPath);
                                continue;
                            }
                        }
                        StringBuilder sb = new StringBuilder(path);
                        if (path.charAt(path.length() - 1) != '/') {
                            sb.append('/');
                        }
                        sb.append(entry.getName());
                        if (entry.isDirectory()) {
                            sb.append('/');
                        }
                        result.add(sb.toString());
                    }
                }
            }
        } else {
            if (!path.endsWith("/")) {
                path = path + "/";
            }
            if (webAppMount.startsWith(path)) {
                int i = webAppMount.indexOf('/', path.length());
                if (i == -1) {
                    result.add(webAppMount + "/");
                } else {
                    result.add(webAppMount.substring(0, i + 1));
                }
            }
        }
        result.setLocked(true);
        return result;
    }

    @Override
    public boolean mkdir(String path) {
        checkPath(path);
        if (isReadOnly()) {
            return false;
        }
        String webAppMount = getWebAppMount();
        if (path.startsWith(webAppMount)) {
            File f = file(path.substring(webAppMount.length()), false);
            if (f == null) {
                return false;
            }
            return f.mkdir();
        } else {
            return false;
        }
    }

    @Override
    public boolean write(String path, InputStream is, boolean overwrite) {
        checkPath(path);

        if (is == null) {
            throw new NullPointerException(sm.getString("dirResourceSet.writeNpe"));
        }

        if (isReadOnly()) {
            return false;
        }

        // write() is meant to create a file so ensure that the path doesn't
        // end in '/'
        if (path.endsWith("/")) {
            return false;
        }

        String webAppMount = getWebAppMount();
        if (!path.startsWith(webAppMount)) {
            return false;
        }

        File dest;
        /*
         * Lock the path for writing until the write is complete. The lock prevents concurrent reads and writes (e.g.
         * HTTP GET and PUT / DELETE) for the same path causing corruption of the FileResource where some of the fields
         * are set as if the file exists and some as set as if it does not.
         */
        ResourceLock lock = lockForWrite(path);
        try {
            dest = file(path.substring(webAppMount.length()), false);
            if (dest == null) {
                return false;
            }

            if (dest.exists() && !overwrite) {
                return false;
            }

            try {
                if (overwrite) {
                    Files.copy(is, dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
                } else {
                    Files.copy(is, dest.toPath());
                }
            } catch (IOException ioe) {
                return false;
            }

            return true;
        } finally {
            unlockForWrite(lock);
        }
    }

    @Override
    protected void checkType(File file) {
        if (!file.isDirectory()) {
            throw new IllegalArgumentException(
                    sm.getString("dirResourceSet.notDirectory", getBase(), File.separator, getInternalPath()));
        }
    }

    // -------------------------------------------------------- Lifecycle methods
    @Override
    protected void initInternal() throws LifecycleException {
        super.initInternal();
        // Is this an exploded web application?
        if (getWebAppMount().isEmpty()) {
            // Look for a manifest
            File mf = file("META-INF/MANIFEST.MF", true);
            if (mf != null && mf.isFile()) {
                try (FileInputStream fis = new FileInputStream(mf)) {
                    setManifest(new Manifest(fis));
                } catch (IOException e) {
                    log.warn(sm.getString("dirResourceSet.manifestFail", mf.getAbsolutePath()), e);
                }
            }
        }
        // Check for exposure to CVE-2024-56337
        if (isReadOnly()) {
            // CVE-2024-56337 (nor CVE-2024-50379) is not exploitable on a read-only ResourceSet
            return;
        }
        if (JreCompat.getInstance().isCanonCachesDisabled()) {
            // CVE-2024-56337 (nor CVE-2024-50379) is not exploitable if the canonical file name cache is disabled
            return;
        }
        // This ResourceSet may be exposed to CVE-2024-56337.
        if (JreCompat.getInstance().disableCanonCaches()) {
            /*
             * The canonical file name cache was enabled and is now disabled.
             */
            log.warn(sm.getString("dirResourceSet.canonCaches.disabled", getFileBase(),
                    getRoot().getContext().getName()));
        } else {
            /*
             * The canonical file name cache could not be disabled (or Tomcat cannot confirm it has been disabled). This
             * ResourceSet may be exposed to CVE-2024-56337.
             */
            throw new IllegalStateException(sm.getString("dirResourceSet.canonCaches.enabled", getFileBase(),
                    getRoot().getContext().getName()));
        }
    }


    private String getLockKey(String path) {
        /*
         * Normalize path to ensure that the same key is used for the same path. Always convert path to lower case as
         * the file system may be case insensitive. A minor performance improvement is possible by removing the
         * conversion to lower case for case sensitive file systems but confirming that all the directories within a
         * DirResourceSet are case sensitive is much harder than it might first appear due to various edge cases. In
         * particular, Windows can make individual directories case sensitive and File.getCanonicalPath() doesn't return
         * the canonical file name on Linux for some case insensitive file systems (such as mounted Windows shares).
         */
        return RequestUtil.normalize(path).toLowerCase(Locale.ENGLISH);
    }


    @Override
    public ResourceLock lockForRead(String path) {
        String key = getLockKey(path);
        ResourceLock resourceLock;
        synchronized (resourceLocksByPathLock) {
            /*
             * Obtain the ResourceLock and increment the usage count inside the sync to ensure that that map always has
             * a consistent view of the currently "in-use" ResourceLocks.
             */
            resourceLock = resourceLocksByPath.get(key);
            if (resourceLock == null) {
                resourceLock = new ResourceLock(key);
                resourceLocksByPath.put(key, resourceLock);
            }
            resourceLock.count.incrementAndGet();
        }
        // Obtain the lock outside the sync as it will block if there is a current write lock.
        resourceLock.reentrantLock.readLock().lock();
        return resourceLock;
    }


    @Override
    public void unlockForRead(ResourceLock resourceLock) {
        // Unlock outside the sync as there is no need to do it inside.
        resourceLock.reentrantLock.readLock().unlock();
        synchronized (resourceLocksByPathLock) {
            /*
             * Decrement the usage count and remove ResourceLocks no longer required inside the sync to ensure that that
             * map always has a consistent view of the currently "in-use" ResourceLocks.
             */
            if (resourceLock.count.decrementAndGet() == 0) {
                resourceLocksByPath.remove(resourceLock.key);
            }
        }
    }


    @Override
    public ResourceLock lockForWrite(String path) {
        String key = getLockKey(path);
        ResourceLock resourceLock;
        synchronized (resourceLocksByPathLock) {
            /*
             * Obtain the ResourceLock and increment the usage count inside the sync to ensure that that map always has
             * a consistent view of the currently "in-use" ResourceLocks.
             */
            resourceLock = resourceLocksByPath.get(key);
            if (resourceLock == null) {
                resourceLock = new ResourceLock(key);
                resourceLocksByPath.put(key, resourceLock);
            }
            resourceLock.count.incrementAndGet();
        }
        // Obtain the lock outside the sync as it will block if there are any other current locks.
        resourceLock.reentrantLock.writeLock().lock();
        return resourceLock;
    }


    @Override
    public void unlockForWrite(ResourceLock resourceLock) {
        // Unlock outside the sync as there is no need to do it inside.
        resourceLock.reentrantLock.writeLock().unlock();
        synchronized (resourceLocksByPathLock) {
            /*
             * Decrement the usage count and remove ResourceLocks no longer required inside the sync to ensure that that
             * map always has a consistent view of the currently "in-use" ResourceLocks.
             */
            if (resourceLock.count.decrementAndGet() == 0) {
                resourceLocksByPath.remove(resourceLock.key);
            }
        }
    }
}