Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fs: return rejected promise when mkdtemp lacks permissions
Previously, fs.promises.mkdtemp() would cause a TypeError "Method
Promise.prototype.then called on incompatible receiver undefined"
when called without write permissions. This happened because the
promise was never returned to JavaScript after being rejected.

This commit ensures SetReturnValue() is called before returning
from the function, so JavaScript receives a proper rejected promise
with an ERR_ACCESS_DENIED error that can be caught and handled.

Fixes: #59023
  • Loading branch information
sjwhole committed Jul 13, 2025
commit 56967616307c7613c45477925743f5e08f3bd2de
14 changes: 9 additions & 5 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3074,11 +3074,15 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {

if (argc > 2) { // mkdtemp(tmpl, encoding, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
env,
req_wrap_async,
permission::PermissionScope::kFileSystemWrite,
tmpl.ToStringView());
if (!env->permission()->is_granted(
env, permission::PermissionScope::kFileSystemWrite,
tmpl.ToStringView())) {
permission::Permission::AsyncThrowAccessDenied(
env, req_wrap_async, permission::PermissionScope::kFileSystemWrite,
tmpl.ToStringView());
req_wrap_async->SetReturnValue(args);
return;
}
FS_ASYNC_TRACE_BEGIN1(
UV_FS_MKDTEMP, req_wrap_async, "path", TRACE_STR_COPY(*tmpl))
AsyncCall(env, req_wrap_async, args, "mkdtemp", encoding, AfterStringPath,
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-permission-fs-promises-mkdtemp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Flags: --permission --allow-fs-read=*
'use strict';

const common = require('../common');
const assert = require('assert');
const fs = require('fs');

// This test verifies that fs.promises.mkdtemp() returns a proper rejected promise
// when permissions are denied, instead of throwing a TypeError
{
// Test with simple string path (reproduces the exact issue from GitHub)
assert.rejects(
fs.promises.mkdtemp('test'),
{
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}
).then(common.mustCall());
}