Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0136ee4e444a520b9099afcac6fe03ee0cdbc4b0 (plain) (blame)
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
//
//  ========================================================================
//  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.server.handler;

import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.jetty.server.handler.ContextHandler.AliasCheck;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;


/* ------------------------------------------------------------ */
/** Symbolic Link AliasChecker.
 * <p>An instance of this class can be registered with {@link ContextHandler#addAliasCheck(AliasCheck)}
 * to check resources that are aliased to other locations.   The checker uses the 
 * Java {@link Files#readSymbolicLink(Path)} and {@link Path#toRealPath(java.nio.file.LinkOption...)}
 * APIs to check if a file is aliased with symbolic links.</p>
 */
public class AllowSymLinkAliasChecker implements AliasCheck
{
    private static final Logger LOG = Log.getLogger(AllowSymLinkAliasChecker.class);
    
    @Override
    public boolean check(String path, Resource resource)
    {
        try
        {
            File file =resource.getFile();
            if (file==null)
                return false;
            
            // If the file exists
            if (file.exists())
            {
                // we can use the real path method to check the symlinks resolve to the alias
                URI real = file.toPath().toRealPath().toUri();
                if (real.equals(resource.getAlias()))
                {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Allow symlink {} --> {}",resource,real);
                    return true;
                }
            }
            else
            {
                // file does not exists, so we have to walk the path and links ourselves.
                Path p = file.toPath().toAbsolutePath();
                File d = p.getRoot().toFile();
                for (Path e:p)
                {
                    d=new File(d,e.toString());
                    
                    while (d.exists() && Files.isSymbolicLink(d.toPath()))
                    {
                        Path link=Files.readSymbolicLink(d.toPath());
                        if (!link.isAbsolute())
                            link=link.resolve(d.toPath());
                        d=link.toFile().getAbsoluteFile().getCanonicalFile();
                    }
                }
                if (resource.getAlias().equals(d.toURI()))
                {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Allow symlink {} --> {}",resource,d);
                    return true;
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            LOG.ignore(e);
        }
        return false;
    }

}

Back to the top