Skip to content

Commit f80a7b7

Browse files
committed
permission: do not create symlinks if target is relative
The permission model's security guarantees fall apart in the presence of relative symbolic links. When an application attempts to create a relative symlink, the permission model currently resolves the relative path into an absolute path based on the process's current working directory, checks whether the process has the relevant permissions, and then creates the symlink using the absolute target path. This behavior is plainly incorrect for two reasons: 1. The target path should never be resolved relative to the current working directory. If anything, it should be resolved relative to the symlink's location. (Of course, there is one insane exception to this rule: on Windows, each process has a current working directory per drive, and symlinks can be created with a target path relative to the current working directory of a specific drive. In that case, the relative path will be resolved relative to the current working directory for the respective drive, and the symlink will be created on disk with the resulting absolute path. Other relative symlinks will be stored as-is.) 2. Silently creating an absolute symlink when the user requested a relative symlink is wrong. The user may (or may not) rely on the symlink being relative. For example, npm heavily relies on relative symbolic links such that node_modules directories can be moved around without breaking. Because we don't know the user's intentions, we don't know if creating an absolute symlink instead of a relative symlink is acceptable. This patch prevents the faulty behavior by not (incorrectly) resolving relative symlink targets when the permission model is enabled, and by instead simply refusing the create any relative symlinks.
1 parent 23d65e7 commit f80a7b7

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

lib/fs.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const {
5858
} = constants;
5959

6060
const pathModule = require('path');
61+
const { isAbsolute } = pathModule;
6162
const { isArrayBufferView } = require('internal/util/types');
6263

6364
const binding = internalBinding('fs');
@@ -68,6 +69,7 @@ const { Buffer } = require('buffer');
6869
const {
6970
aggregateTwoErrors,
7071
codes: {
72+
ERR_ACCESS_DENIED,
7173
ERR_FS_FILE_TOO_LARGE,
7274
ERR_INVALID_ARG_VALUE,
7375
},
@@ -143,6 +145,8 @@ const {
143145
} = require('internal/validators');
144146
const { readFileSyncUtf8 } = require('internal/fs/read/utf8');
145147

148+
const permission = require('internal/process/permission');
149+
146150
let truncateWarn = true;
147151
let fs;
148152

@@ -1762,6 +1766,15 @@ function symlink(target, path, type_, callback_) {
17621766
const type = (typeof type_ === 'string' ? type_ : null);
17631767
const callback = makeCallback(arguments[arguments.length - 1]);
17641768

1769+
if (permission.isEnabled()) {
1770+
// The permission model's security guarantees fall apart in the presence of
1771+
// relative symbolic links. Thus, we have to prevent their creation.
1772+
if (!isAbsolute(toPathIfFileURL(target))) {
1773+
callback(new ERR_ACCESS_DENIED('relative symbolic link target'));
1774+
return;
1775+
}
1776+
}
1777+
17651778
target = getValidatedPath(target, 'target');
17661779
path = getValidatedPath(path);
17671780

@@ -1818,6 +1831,15 @@ function symlinkSync(target, path, type) {
18181831
type = 'dir';
18191832
}
18201833
}
1834+
1835+
if (permission.isEnabled()) {
1836+
// The permission model's security guarantees fall apart in the presence of
1837+
// relative symbolic links. Thus, we have to prevent their creation.
1838+
if (!isAbsolute(toPathIfFileURL(target))) {
1839+
throw new ERR_ACCESS_DENIED('relative symbolic link target');
1840+
}
1841+
}
1842+
18211843
target = getValidatedPath(target, 'target');
18221844
path = getValidatedPath(path);
18231845
const flags = stringToSymlinkType(type);

lib/internal/fs/promises.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const { Buffer } = require('buffer');
3333

3434
const {
3535
codes: {
36+
ERR_ACCESS_DENIED,
3637
ERR_FS_FILE_TOO_LARGE,
3738
ERR_INVALID_ARG_VALUE,
3839
ERR_INVALID_STATE,
@@ -84,6 +85,8 @@ const {
8485
validateString,
8586
} = require('internal/validators');
8687
const pathModule = require('path');
88+
const { isAbsolute } = pathModule;
89+
const { toPathIfFileURL } = require('internal/url');
8790
const {
8891
kEmptyObject,
8992
lazyDOMException,
@@ -96,6 +99,8 @@ const nonNativeWatcher = require('internal/fs/recursive_watch');
9699
const { isIterable } = require('internal/streams/utils');
97100
const assert = require('internal/assert');
98101

102+
const permission = require('internal/process/permission');
103+
99104
const kHandle = Symbol('kHandle');
100105
const kFd = Symbol('kFd');
101106
const kRefs = Symbol('kRefs');
@@ -877,6 +882,15 @@ async function symlink(target, path, type_) {
877882
type = 'file';
878883
}
879884
}
885+
886+
if (permission.isEnabled()) {
887+
// The permission model's security guarantees fall apart in the presence of
888+
// relative symbolic links. Thus, we have to prevent their creation.
889+
if (!isAbsolute(toPathIfFileURL(target))) {
890+
throw new ERR_ACCESS_DENIED('relative symbolic link target');
891+
}
892+
}
893+
880894
target = getValidatedPath(target, 'target');
881895
path = getValidatedPath(path);
882896
return binding.symlink(preprocessSymlinkDestination(target, type, path),
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Flags: --experimental-permission --allow-fs-read=* --allow-fs-write=*
2+
'use strict';
3+
4+
const common = require('../common');
5+
common.skipIfWorker();
6+
7+
const assert = require('assert');
8+
const { symlinkSync, symlink, promises: { symlink: symlinkAsync } } = require('fs');
9+
10+
const error = {
11+
code: 'ERR_ACCESS_DENIED',
12+
message: /relative symbolic link target/,
13+
};
14+
15+
for (const target of ['a', './b/c', '../d', 'e/../f', 'C:drive-relative', 'ntfs:alternate']) {
16+
for (const path of [__filename, __dirname, process.execPath]) {
17+
assert.throws(() => symlinkSync(target, path), error);
18+
symlink(target, path, common.mustCall((err) => {
19+
assert(err);
20+
assert.strictEqual(err.code, error.code);
21+
assert.match(err.message, error.message);
22+
}));
23+
assert.rejects(() => symlinkAsync(target, path), error);
24+
}
25+
}

0 commit comments

Comments
 (0)