Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/node/unc.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
export function getUNCHostAllowlist(): string[] {
7
const allowlist = processUNCHostAllowlist();
8
if (allowlist) {
9
return Array.from(allowlist);
10
}
11
12
return [];
13
}
14
15
function processUNCHostAllowlist(): Set<string> {
16
17
// The property `process.uncHostAllowlist` is not available in official node.js
18
// releases, only in our own builds, so we have to probe for availability
19
20
return (process as any).uncHostAllowlist;
21
}
22
23
export function addUNCHostToAllowlist(allowedHost: string | string[]): void {
24
if (process.platform !== 'win32') {
25
return;
26
}
27
28
const allowlist = processUNCHostAllowlist();
29
if (allowlist) {
30
if (typeof allowedHost === 'string') {
31
allowlist.add(allowedHost.toLowerCase()); // UNC hosts are case-insensitive
32
} else {
33
for (const host of toSafeStringArray(allowedHost)) {
34
addUNCHostToAllowlist(host);
35
}
36
}
37
}
38
}
39
40
function toSafeStringArray(arg0: unknown): string[] {
41
const allowedUNCHosts = new Set<string>();
42
43
if (Array.isArray(arg0)) {
44
for (const host of arg0) {
45
if (typeof host === 'string') {
46
allowedUNCHosts.add(host);
47
}
48
}
49
}
50
51
return Array.from(allowedUNCHosts);
52
}
53
54
export function getUNCHost(maybeUNCPath: string | undefined | null): string | undefined {
55
if (typeof maybeUNCPath !== 'string') {
56
return undefined; // require a valid string
57
}
58
59
const uncRoots = [
60
'\\\\.\\UNC\\', // DOS Device paths (https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats)
61
'\\\\?\\UNC\\',
62
'\\\\' // standard UNC path
63
];
64
65
let host = undefined;
66
67
for (const uncRoot of uncRoots) {
68
const indexOfUNCRoot = maybeUNCPath.indexOf(uncRoot);
69
if (indexOfUNCRoot !== 0) {
70
continue; // not matching any of our expected UNC roots
71
}
72
73
const indexOfUNCPath = maybeUNCPath.indexOf('\\', uncRoot.length);
74
if (indexOfUNCPath === -1) {
75
continue; // no path component found
76
}
77
78
const hostCandidate = maybeUNCPath.substring(uncRoot.length, indexOfUNCPath);
79
if (hostCandidate) {
80
host = hostCandidate;
81
break;
82
}
83
}
84
85
return host;
86
}
87
88
export function disableUNCAccessRestrictions(): void {
89
if (process.platform !== 'win32') {
90
return;
91
}
92
93
(process as any).restrictUNCAccess = false;
94
}
95
96
export function isUNCAccessRestrictionsDisabled(): boolean {
97
if (process.platform !== 'win32') {
98
return true;
99
}
100
101
return (process as any).restrictUNCAccess === false;
102
}
103
104