Repository navigation
Description
Current Behavior
Generating an SSH key via POST /settings/user/keys with a name that fails the allowed-name check returns a cryptic internal error:
{ "code": "unexpected_error", "message": "e is not defined" }The name-validation path in generateSSHKey() creates the error as err, but then sets .code on a different variable e that doesn't exist:
packages/node_modules/@node-red/runtime/lib/storage/localfilesystem/projects/ssh/index.js, lines 103-109:
function generateSSHKey(username, options) {
options = options || {};
var name = options.name || "";
if (!/^[a-zA-Z0-9\-_]+$/.test(options.name)) {
var err = new Error("Invalid SSH Key name");
e.code = "invalid_key_name"; // line 107: `e` is not defined -> ReferenceError
return Promise.reject(err);
}e.code = ... throws ReferenceError: e is not defined (dereferencing an undeclared identifier throws in sloppy mode too — the global is only created for a plain e = value assignment, not a property assignment on it). So the intended Error("Invalid SSH Key name") is never rejected; the ReferenceError propagates instead, and picks up err.status = 400 in runtime/lib/api/settings.js (generateUserKey catch). rejectHandler then replies code: err.code || "unexpected_error", i.e. unexpected_error with the message e is not defined.
The surrounding code shows the intended pattern — the next error in the same file does it correctly:
if ( result ) {
var e = new Error("SSH Key name exists");
e.code = "key_exists";
throw e;
}Expected Behavior
400 { "code": "invalid_key_name", "message": "Invalid SSH Key name" }
Steps To Reproduce
With a Node-RED instance that has the projects feature enabled and admin auth configured:
curl -X POST http://localhost:1880/settings/user/keys \
-H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{"name":"my key with spaces"}'Response today:
{ "code": "unexpected_error", "message": "e is not defined" }Running the real function (extracted verbatim from ssh/index.js) confirms it:
const { generateSSHKey } = require("./_generatessh_extracted.js"); // verbatim generateSSHKey
try {
await generateSSHKey("admin", { name: "my key with spaces" });
} catch (err) {
console.log(err); // ReferenceError: e is not defined
console.log(err.code); // undefined
}Output:
[A] Invalid key name -> rejected with: "e is not defined"
err.code = undefined (expected: "invalid_key_name")
In the editor, the dialog's response handler (projectUserSettings.js) only has a branch for the unexpected_error code, so the user just gets the generic failure / a console message with e is not defined, and no hint that the name was the problem.
This goes back a while — the same var err/e.code mismatch is present in the 3.1.0 tag.
Suggested Fix
- var err = new Error("Invalid SSH Key name");
- e.code = "invalid_key_name";
- return Promise.reject(err);
+ var err = new Error("Invalid SSH Key name");
+ err.code = "invalid_key_name";
+ return Promise.reject(err);
Activity
Fix invalid SSH key name error code