Skip to content

Commit c0010ed

Browse files
tniessenmarco-ippolito
authored andcommitted
fs,permission: make handling of buffers consistent
Commit 2000c26 added explicit handling of Buffers to fs.symlink, but not to fs.symlinkSync or fs.promises.symlink. This change adapts the latter two functions to behave like fs.symlink. Refs: #49156 Refs: #51212 PR-URL: #52348 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 69ebf18 commit c0010ed

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/fs.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,11 @@ function symlinkSync(target, path, type) {
17911791
if (permission.isEnabled()) {
17921792
// The permission model's security guarantees fall apart in the presence of
17931793
// relative symbolic links. Thus, we have to prevent their creation.
1794-
if (typeof target !== 'string' || !isAbsolute(toPathIfFileURL(target))) {
1794+
if (BufferIsBuffer(target)) {
1795+
if (!isAbsolute(BufferToString(target))) {
1796+
throw new ERR_ACCESS_DENIED('relative symbolic link target');
1797+
}
1798+
} else if (typeof target !== 'string' || !isAbsolute(toPathIfFileURL(target))) {
17951799
throw new ERR_ACCESS_DENIED('relative symbolic link target');
17961800
}
17971801
}

lib/internal/fs/promises.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
SymbolAsyncDispose,
1919
Uint8Array,
2020
FunctionPrototypeBind,
21+
uncurryThis,
2122
} = primordials;
2223

2324
const { fs: constants } = internalBinding('constants');
@@ -31,6 +32,8 @@ const {
3132

3233
const binding = internalBinding('fs');
3334
const { Buffer } = require('buffer');
35+
const { isBuffer: BufferIsBuffer } = Buffer;
36+
const BufferToString = uncurryThis(Buffer.prototype.toString);
3437

3538
const {
3639
codes: {
@@ -980,7 +983,11 @@ async function symlink(target, path, type_) {
980983
if (permission.isEnabled()) {
981984
// The permission model's security guarantees fall apart in the presence of
982985
// relative symbolic links. Thus, we have to prevent their creation.
983-
if (typeof target !== 'string' || !isAbsolute(toPathIfFileURL(target))) {
986+
if (BufferIsBuffer(target)) {
987+
if (!isAbsolute(BufferToString(target))) {
988+
throw new ERR_ACCESS_DENIED('relative symbolic link target');
989+
}
990+
} else if (typeof target !== 'string' || !isAbsolute(toPathIfFileURL(target))) {
984991
throw new ERR_ACCESS_DENIED('relative symbolic link target');
985992
}
986993
}

0 commit comments

Comments
 (0)