summaryrefslogtreecommitdiffstats
path: root/webkit/fileapi/sandboxed_file_system_operation.cc
blob: 08569f94f384a2b45bfd10dbad239d41c72b81cb (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/fileapi/sandboxed_file_system_operation.h"

#include "net/url_request/url_request_context.h"
#include "webkit/fileapi/file_system_callback_dispatcher.h"
#include "webkit/fileapi/file_system_path_manager.h"
#include "webkit/fileapi/file_system_quota_manager.h"
#include "webkit/fileapi/sandboxed_file_system_context.h"

namespace fileapi {

SandboxedFileSystemOperation::SandboxedFileSystemOperation(
    FileSystemCallbackDispatcher* dispatcher,
    scoped_refptr<base::MessageLoopProxy> proxy,
    scoped_refptr<SandboxedFileSystemContext> file_system_context)
    : FileSystemOperation(dispatcher, proxy),
      file_system_context_(file_system_context),
      callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
  DCHECK(file_system_context_);
}

SandboxedFileSystemOperation::~SandboxedFileSystemOperation() {}

void SandboxedFileSystemOperation::OpenFileSystem(
    const GURL& origin_url, fileapi::FileSystemType type, bool create) {
#ifndef NDEBUG
  DCHECK(kOperationNone == pending_operation_);
  pending_operation_ = static_cast<FileSystemOperation::OperationType>(
      kOperationOpenFileSystem);
#endif

  file_system_context_->path_manager()->GetFileSystemRootPath(
      origin_url, type, create,
      callback_factory_.NewCallback(
          &SandboxedFileSystemOperation::DidGetRootPath));
}

void SandboxedFileSystemOperation::CreateFile(
    const FilePath& path, bool exclusive) {
  if (!VerifyFileSystemPathForWrite(path, true /* create */, 0)) {
    delete this;
    return;
  }
  FileSystemOperation::CreateFile(path, exclusive);
}

void SandboxedFileSystemOperation::CreateDirectory(
    const FilePath& path, bool exclusive, bool recursive) {
  if (!VerifyFileSystemPathForWrite(path, true /* create */, 0)) {
    delete this;
    return;
  }
  FileSystemOperation::CreateDirectory(path, exclusive, recursive);
}

void SandboxedFileSystemOperation::Copy(
    const FilePath& src_path, const FilePath& dest_path) {
  if (!VerifyFileSystemPathForRead(src_path) ||
      !VerifyFileSystemPathForWrite(dest_path, true /* create */,
                                    FileSystemQuotaManager::kUnknownSize)) {
    delete this;
    return;
  }
  FileSystemOperation::Copy(src_path, dest_path);
}

void SandboxedFileSystemOperation::Move(
    const FilePath& src_path, const FilePath& dest_path) {
  if (!VerifyFileSystemPathForRead(src_path) ||
      !VerifyFileSystemPathForWrite(dest_path, true /* create */,
                                    FileSystemQuotaManager::kUnknownSize)) {
    delete this;
    return;
  }
  FileSystemOperation::Move(src_path, dest_path);
}

void SandboxedFileSystemOperation::DirectoryExists(const FilePath& path) {
  if (!VerifyFileSystemPathForRead(path)) {
    delete this;
    return;
  }
  FileSystemOperation::DirectoryExists(path);
}

void SandboxedFileSystemOperation::FileExists(const FilePath& path) {
  if (!VerifyFileSystemPathForRead(path)) {
    delete this;
    return;
  }
  FileSystemOperation::FileExists(path);
}

void SandboxedFileSystemOperation::GetMetadata(const FilePath& path) {
  if (!VerifyFileSystemPathForRead(path)) {
    delete this;
    return;
  }
  FileSystemOperation::GetMetadata(path);
}

void SandboxedFileSystemOperation::ReadDirectory(const FilePath& path) {
  if (!VerifyFileSystemPathForRead(path)) {
    delete this;
    return;
  }
  FileSystemOperation::ReadDirectory(path);
}

void SandboxedFileSystemOperation::Remove(
    const FilePath& path, bool recursive) {
  if (!VerifyFileSystemPathForWrite(path, false /* create */, 0)) {
    delete this;
    return;
  }
  FileSystemOperation::Remove(path, recursive);
}

void SandboxedFileSystemOperation::Write(
    scoped_refptr<net::URLRequestContext> url_request_context,
    const FilePath& path, const GURL& blob_url, int64 offset) {
  if (!VerifyFileSystemPathForWrite(path, true /* create */,
                                    FileSystemQuotaManager::kUnknownSize)) {
    delete this;
    return;
  }
  FileSystemOperation::Write(url_request_context, path, blob_url, offset);
}

void SandboxedFileSystemOperation::Truncate(
    const FilePath& path, int64 length) {
  if (!VerifyFileSystemPathForWrite(path, false /* create */, 0)) {
    delete this;
    return;
  }
  FileSystemOperation::Truncate(path, length);
}

void SandboxedFileSystemOperation::TouchFile(
    const FilePath& path,
    const base::Time& last_access_time,
    const base::Time& last_modified_time) {
  if (!VerifyFileSystemPathForWrite(path, true /* create */, 0)) {
    delete this;
    return;
  }
  FileSystemOperation::TouchFile(path, last_access_time, last_modified_time);
}

void SandboxedFileSystemOperation::DidGetRootPath(
    bool success, const FilePath& path, const std::string& name) {
  DCHECK(success || path.empty());
  dispatcher()->DidOpenFileSystem(name, path);
  delete this;
}

bool SandboxedFileSystemOperation::VerifyFileSystemPathForRead(
    const FilePath& path) {
  // We may want do more checks, but for now it just checks if the given
  // |path| is under the valid FileSystem root path for this host context.
  if (!file_system_context_->path_manager()->CrackFileSystemPath(
          path, NULL, NULL, NULL)) {
    dispatcher()->DidFail(base::PLATFORM_FILE_ERROR_SECURITY);
    return false;
  }
  return true;
}

bool SandboxedFileSystemOperation::VerifyFileSystemPathForWrite(
    const FilePath& path, bool create, int64 growth) {
  GURL origin_url;
  FilePath virtual_path;
  if (!file_system_context_->path_manager()->CrackFileSystemPath(
          path, &origin_url, NULL, &virtual_path)) {
    dispatcher()->DidFail(base::PLATFORM_FILE_ERROR_SECURITY);
    return false;
  }
  // Any write access is disallowed on the root path.
  if (virtual_path.value().length() == 0 ||
      virtual_path.DirName().value() == virtual_path.value()) {
    dispatcher()->DidFail(base::PLATFORM_FILE_ERROR_SECURITY);
    return false;
  }
  if (create && file_system_context_->path_manager()->IsRestrictedFileName(
          path.BaseName())) {
    dispatcher()->DidFail(base::PLATFORM_FILE_ERROR_SECURITY);
    return false;
  }
  // TODO(kinuko): For operations with kUnknownSize we'll eventually
  // need to resolve what amount of size it's going to write.
  if (!file_system_context_->quota_manager()->CheckOriginQuota(
          origin_url, growth)) {
    dispatcher()->DidFail(base::PLATFORM_FILE_ERROR_NO_SPACE);
    return false;
  }
  return true;
}

}  // namespace fileapi