• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "config.h"
32 #include "modules/filesystem/DOMFilePath.h"
33 
34 #include "wtf/Vector.h"
35 #include "wtf/text/CString.h"
36 #include "wtf/text/StringBuilder.h"
37 
38 namespace blink {
39 
40 const char DOMFilePath::separator = '/';
41 const char DOMFilePath::root[] = "/";
42 
append(const String & base,const String & components)43 String DOMFilePath::append(const String& base, const String& components)
44 {
45     return ensureDirectoryPath(base) + components;
46 }
47 
ensureDirectoryPath(const String & path)48 String DOMFilePath::ensureDirectoryPath(const String& path)
49 {
50     if (!DOMFilePath::endsWithSeparator(path))
51         return path + DOMFilePath::separator;
52     return path;
53 }
54 
getName(const String & path)55 String DOMFilePath::getName(const String& path)
56 {
57     int index = path.reverseFind(DOMFilePath::separator);
58     if (index != -1)
59         return path.substring(index + 1);
60     return path;
61 }
62 
getDirectory(const String & path)63 String DOMFilePath::getDirectory(const String& path)
64 {
65     int index = path.reverseFind(DOMFilePath::separator);
66     if (!index)
67         return DOMFilePath::root;
68     if (index != -1)
69         return path.substring(0, index);
70     return ".";
71 }
72 
isParentOf(const String & parent,const String & mayBeChild)73 bool DOMFilePath::isParentOf(const String& parent, const String& mayBeChild)
74 {
75     ASSERT(DOMFilePath::isAbsolute(parent));
76     ASSERT(DOMFilePath::isAbsolute(mayBeChild));
77     if (parent == DOMFilePath::root && mayBeChild != DOMFilePath::root)
78         return true;
79     if (parent.length() >= mayBeChild.length() || !mayBeChild.startsWith(parent, false))
80         return false;
81     if (mayBeChild[parent.length()] != DOMFilePath::separator)
82         return false;
83     return true;
84 }
85 
removeExtraParentReferences(const String & path)86 String DOMFilePath::removeExtraParentReferences(const String& path)
87 {
88     ASSERT(DOMFilePath::isAbsolute(path));
89     Vector<String> components;
90     Vector<String> canonicalized;
91     path.split(DOMFilePath::separator, components);
92     for (size_t i = 0; i < components.size(); ++i) {
93         if (components[i] == ".")
94             continue;
95         if (components[i] == "..") {
96             if (canonicalized.size() > 0)
97                 canonicalized.removeLast();
98             continue;
99         }
100         canonicalized.append(components[i]);
101     }
102     if (canonicalized.isEmpty())
103         return DOMFilePath::root;
104     StringBuilder result;
105     for (size_t i = 0; i < canonicalized.size(); ++i) {
106         result.append(DOMFilePath::separator);
107         result.append(canonicalized[i]);
108     }
109     return result.toString();
110 }
111 
isValidPath(const String & path)112 bool DOMFilePath::isValidPath(const String& path)
113 {
114     if (path.isEmpty() || path == DOMFilePath::root)
115         return true;
116 
117     // Embedded NULs are not allowed.
118     if (path.find(static_cast<UChar>(0)) != WTF::kNotFound)
119         return false;
120 
121     // While not [yet] restricted by the spec, '\\' complicates implementation for Chromium.
122     if (path.find('\\') != WTF::kNotFound)
123         return false;
124 
125     // This method is only called on fully-evaluated absolute paths. Any sign of ".." or "." is likely an attempt to break out of the sandbox.
126     Vector<String> components;
127     path.split(DOMFilePath::separator, components);
128     for (size_t i = 0; i < components.size(); ++i) {
129         if (components[i] == ".")
130             return false;
131         if (components[i] == "..")
132             return false;
133     }
134     return true;
135 }
136 
isValidName(const String & name)137 bool DOMFilePath::isValidName(const String& name)
138 {
139     if (name.isEmpty())
140         return true;
141     // '/' is not allowed in name.
142     if (name.contains('/'))
143         return false;
144     return isValidPath(name);
145 }
146 
147 } // namespace blink
148