-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.ts
More file actions
328 lines (301 loc) · 10.6 KB
/
Copy pathpaths.ts
File metadata and controls
328 lines (301 loc) · 10.6 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
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
import { createHash } from 'node:crypto';
import { lstat, mkdir, open, realpath } from 'node:fs/promises';
import type { Stats } from 'node:fs';
import { constants } from 'node:fs';
import path from 'node:path';
export interface PluginPathRoots {
readonly applicationData: string;
readonly data: string;
readonly cache: string;
readonly temp: string;
}
export interface PluginPaths {
readonly data: string;
readonly cache: string;
readonly temp: string;
readonly legacyData: readonly string[];
}
export interface CreatePluginPathsOptions {
readonly roots: PluginPathRoots;
readonly owner: string;
readonly legacyDataDirectories: readonly string[];
}
export interface PluginPathFileSystem {
lstat(candidate: string): Promise<Stats>;
realpath(candidate: string): Promise<string>;
mkdir(candidate: string, options: { mode: number }): Promise<string | undefined | void>;
openDirectory(candidate: string): Promise<PluginDirectoryHandle>;
}
export interface PluginDirectoryHandle {
readonly fd: number;
stat(): Promise<Stats>;
fchmod(mode: number): Promise<void>;
close(): Promise<void>;
}
const defaultFileSystem: PluginPathFileSystem = {
lstat,
realpath,
mkdir,
async openDirectory(candidate) {
const handle = await open(
candidate,
constants.O_RDONLY | constants.O_DIRECTORY | (constants.O_NOFOLLOW ?? 0),
);
return {
fd: handle.fd,
stat: () => handle.stat(),
fchmod: (mode) => handle.chmod(mode),
close: () => handle.close(),
};
},
};
export const PLUGIN_STORAGE_UNAVAILABLE = 'PLUGIN_STORAGE_UNAVAILABLE';
export class PluginStorageUnavailableError extends Error {
readonly code = PLUGIN_STORAGE_UNAVAILABLE;
constructor() {
super('Plugin storage unavailable');
this.name = 'PluginStorageUnavailableError';
}
}
export async function createPluginPaths({
roots,
owner,
legacyDataDirectories,
}: CreatePluginPathsOptions, fileSystem: PluginPathFileSystem = defaultFileSystem): Promise<PluginPaths> {
try {
const normalizedRoots = normalizeRoots(roots);
const ownerKey = encodeOwner(owner);
if (new Set(legacyDataDirectories).size !== legacyDataDirectories.length) {
throw new PluginStorageUnavailableError();
}
const legacyData = legacyDataDirectories.map((directory) => (
resolveLegacyDirectory(normalizedRoots.applicationData, directory)
));
const applicationIdentity = await observeStableDirectory(
normalizedRoots.applicationData,
fileSystem,
);
try {
for (const legacyDirectory of legacyData) {
const existing = await inspectPath(legacyDirectory, fileSystem);
if (existing) {
const legacyIdentity = await observeStableDirectory(
legacyDirectory,
fileSystem,
applicationIdentity.realPath,
);
await closeDirectory(legacyIdentity.handle);
}
}
} finally {
await closeDirectory(applicationIdentity.handle);
}
const data = await createPrivateOwnerDirectory(
normalizedRoots.applicationData, normalizedRoots.data, ownerKey, fileSystem, applicationIdentity.realPath,
);
const cache = await createPrivateOwnerDirectory(
normalizedRoots.applicationData, normalizedRoots.cache, ownerKey, fileSystem, applicationIdentity.realPath,
);
const temp = await createPrivateOwnerDirectory(
normalizedRoots.applicationData, normalizedRoots.temp, ownerKey, fileSystem, applicationIdentity.realPath,
);
for (const directory of [data, cache, temp]) {
const verified = await observeStableDirectory(directory, fileSystem, applicationIdentity.realPath);
await closeDirectory(verified.handle);
}
return Object.freeze({
data,
cache,
temp,
legacyData: Object.freeze(legacyData),
});
} catch (error) {
if (error instanceof PluginStorageUnavailableError) throw error;
throw new PluginStorageUnavailableError();
}
}
function normalizeRoots(roots: PluginPathRoots): PluginPathRoots {
const applicationData = normalizeAbsolute(roots.applicationData);
const normalized = {
applicationData,
data: normalizeAbsolute(roots.data),
cache: normalizeAbsolute(roots.cache),
temp: normalizeAbsolute(roots.temp),
};
for (const pluginRoot of [normalized.data, normalized.cache, normalized.temp]) {
if (!isStrictDescendant(applicationData, pluginRoot)) throw new PluginStorageUnavailableError();
}
return normalized;
}
function normalizeAbsolute(value: string): string {
if (typeof value !== 'string' || value.includes('\0') || !path.isAbsolute(value)) {
throw new PluginStorageUnavailableError();
}
return path.resolve(value);
}
function encodeOwner(owner: string): string {
if (typeof owner !== 'string' || owner.length === 0 || owner.includes('\0')) {
throw new PluginStorageUnavailableError();
}
return createHash('sha256').update(owner, 'utf8').digest('hex');
}
function resolveLegacyDirectory(applicationData: string, directory: string): string {
if (typeof directory !== 'string'
|| directory.length === 0
|| directory.includes('/')
|| directory.includes('\\')
|| directory.includes('\0')
|| directory === '.'
|| directory === '..'
|| path.isAbsolute(directory)
|| path.basename(directory) !== directory) {
throw new PluginStorageUnavailableError();
}
const resolved = path.resolve(applicationData, directory);
if (!isStrictDescendant(applicationData, resolved)) throw new PluginStorageUnavailableError();
return resolved;
}
async function createPrivateOwnerDirectory(
applicationData: string,
pluginRoot: string,
ownerKey: string,
fileSystem: PluginPathFileSystem,
applicationRealPath: string,
): Promise<string> {
await ensurePrivateDirectoryChain(
applicationData,
pluginRoot,
fileSystem,
applicationRealPath,
);
const ownerDirectory = path.join(pluginRoot, ownerKey);
await ensurePrivateDirectoryChain(
applicationData,
ownerDirectory,
fileSystem,
applicationRealPath,
);
return ownerDirectory;
}
async function ensurePrivateDirectoryChain(
applicationData: string,
target: string,
fileSystem: PluginPathFileSystem,
applicationRealPath: string,
): Promise<void> {
const relative = path.relative(applicationData, target);
if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) {
throw new PluginStorageUnavailableError();
}
let current = applicationData;
let parentIdentity = await observeStableDirectory(current, fileSystem, applicationRealPath);
try {
for (const part of relative.split(path.sep)) {
const parent = current;
current = path.join(current, part);
const entry = await inspectPath(current, fileSystem);
if (!entry) {
await assertPathStillReferences(parent, parentIdentity, fileSystem, applicationRealPath);
const parentBefore = await parentIdentity.handle.stat();
// Node has no mkdirat API (and Darwin cannot traverse directory FDs via /dev/fd).
// Keep the parent handle open, detect replacement immediately, and never path-clean on failure.
await fileSystem.mkdir(current, { mode: 0o700 });
const parentAfter = await parentIdentity.handle.stat();
assertSameIdentity(parentBefore, parentAfter);
await assertPathStillReferences(parent, parentIdentity, fileSystem, applicationRealPath);
}
const childIdentity = await observeStableDirectory(current, fileSystem, applicationRealPath);
try {
if (process.platform !== 'win32') {
const beforeChmod = await childIdentity.handle.stat();
await childIdentity.handle.fchmod(0o700);
const afterChmod = await childIdentity.handle.stat();
assertSameIdentity(beforeChmod, afterChmod);
}
await closeDirectory(parentIdentity.handle);
parentIdentity = childIdentity;
} catch (error) {
await closeDirectory(childIdentity.handle);
throw error;
}
}
} finally {
await closeDirectory(parentIdentity.handle);
}
}
interface DirectoryIdentity {
stats: Stats;
realPath: string;
handle: PluginDirectoryHandle;
}
async function observeStableDirectory(
candidate: string,
fileSystem: PluginPathFileSystem,
applicationRealPath?: string,
): Promise<DirectoryIdentity> {
const before = await fileSystem.lstat(candidate);
assertDirectory(before);
const handle = await fileSystem.openDirectory(candidate);
try {
const opened = await handle.stat();
assertDirectory(opened);
assertSameIdentity(before, opened);
const realPath = await fileSystem.realpath(candidate);
if (applicationRealPath && !isWithin(applicationRealPath, realPath)) {
throw new PluginStorageUnavailableError();
}
const after = await fileSystem.lstat(candidate);
assertDirectory(after);
assertSameIdentity(before, after);
const openedAfter = await handle.stat();
assertSameIdentity(opened, openedAfter);
return { stats: after, realPath, handle };
} catch (error) {
await closeDirectory(handle);
throw error;
}
}
async function assertPathStillReferences(
candidate: string,
expected: DirectoryIdentity,
fileSystem: PluginPathFileSystem,
applicationRealPath: string,
): Promise<void> {
const observed = await observeStableDirectory(candidate, fileSystem, applicationRealPath);
try {
assertSameIdentity(expected.stats, observed.stats);
} finally {
await closeDirectory(observed.handle);
}
}
async function closeDirectory(handle: PluginDirectoryHandle): Promise<void> {
try {
await handle.close();
} catch {
// The storage operation already fails closed; cleanup must not redirect it to a pathname fallback.
}
}
function assertDirectory(stats: Stats): void {
if (stats.isSymbolicLink() || !stats.isDirectory()) throw new PluginStorageUnavailableError();
}
function assertSameIdentity(before: Stats, after: Stats): void {
if (before.dev !== after.dev || before.ino !== after.ino) {
throw new PluginStorageUnavailableError();
}
}
async function inspectPath(candidate: string, fileSystem: PluginPathFileSystem) {
try {
return await fileSystem.lstat(candidate);
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return undefined;
throw error;
}
}
function isWithin(parent: string, candidate: string): boolean {
const relative = path.relative(parent, candidate);
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
}
function isStrictDescendant(parent: string, candidate: string): boolean {
const relative = path.relative(parent, candidate);
return relative.length > 0 && !relative.startsWith('..') && !path.isAbsolute(relative);
}