Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process: allow URL instances as paths to executables #49031

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

LiviaMedeiros
Copy link
Contributor

Allow URL instances to be passed as file parameters and execPath options. For example:

import { execFile } from 'node:child_process';

execFile(
  new URL('../../bin/executable', import.meta.url),
  ['argv1'],
  { cwd: new URL('../dir/subdir', import.meta.url) },
  () => { ... }
);

@nodejs-github-bot nodejs-github-bot added child_process Issues and PRs related to the child_process subsystem. needs-ci PRs that need a full CI run. labels Aug 5, 2023
Comment on lines 266 to 267
[new URL('file:///C:/Windows/system32/cmd.exe'), ['/d', '/c', 'cd']] :
[new URL('file:///bin/pwd'), []];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why these changes? Are we sure these absolute paths are safe assumptions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure and I'd like to see if it's not the case on any platform covered by CI first.

Then it will most likely be moved to separate test-child-process-urlfile.mjs test.

If there is a concise&robust way to get absolute url or absolute path to pwd or any other convenient good-for-test executable, or if we can create one in fixtures, I'd like to know.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would guess the "where"/"which" commands should generally be available, even outside of CI since tests should work locally too.

@LiviaMedeiros LiviaMedeiros added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 6, 2023
@github-actions github-actions bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Aug 6, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 6, 2023

Failed to start CI
- Validating Jenkins credentials
✘  Jenkins credentials invalid
https://github.com/nodejs/node/actions/runs/5774680936

@@ -1040,6 +1056,10 @@ metacharacters may be used to trigger arbitrary command execution.**
<!-- YAML
added: v0.11.12
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/49031
description: The `command` parameter can be a WHATWG `URL` object using
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The word object hints towards an actual javascript object with URL class properties, but there isn't any test to check towards it.

Suggested change
description: The `command` parameter can be a WHATWG `URL` object using
description: The `command` parameter can be a WHATWG `URL` instance using

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isURL() is duck-typed:

node/lib/internal/url.js

Lines 761 to 763 in d150316

function isURL(self) {
return Boolean(self?.href && self.protocol && self.auth === undefined && self.path === undefined);
}

so it probably makes sense? The same term is used in other entries here as well.

}

{
const stdout = cp.spawnSync(...pwdCommandAndOptions).stdout;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test where the legacy url class is used? We need to make sure it does exactly what we want it to be.

const pwdFullPath = `${cp.execSync(whichCommand)}`.trim();
const pwdUrl = pathToFileURL(pwdFullPath);
const pwdCommandAndOptions = isWindows ?
[pwdUrl, ['/d', '/c', 'cd'], { cwd: pathToFileURL(cwd) }] :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test where javascript object representation of the URL is used?

@@ -545,8 +545,7 @@ function copyProcessEnvToEnv(env, name, optionEnv) {
}

function normalizeSpawnArguments(file, args, options) {
validateString(file, 'file');
validateArgumentNullCheck(file, 'file');
file = getValidatedPath(file, 'file');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validatePath inside getValidatedPath also accepts buffers. Can you add a test for it as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I guess it should be restricted to string or stringified URL, at least for now.

@anonrig
Copy link
Member

anonrig commented Aug 6, 2023

cc @nodejs/url


throws(
() => cp.execFileSync(...pwdCommandAndOptions),
{ code: 'ERR_INVALID_ARG_TYPE' },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you be specific, or at least add comment about what this line is testing? It is really cryptic atm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests for specific errors, added brief comments.

@aduh95 aduh95 added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. labels Aug 8, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 10, 2023
@nodejs-github-bot

This comment was marked as outdated.

@LiviaMedeiros LiviaMedeiros force-pushed the childprocess-file-url branch from 7ca5ecf to 71a2fef Compare August 10, 2023 17:31
@nodejs-github-bot
Copy link
Collaborator

@LiviaMedeiros LiviaMedeiros added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 7, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 7, 2023
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@LiviaMedeiros LiviaMedeiros added the review wanted PRs that need reviews. label Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
child_process Issues and PRs related to the child_process subsystem. needs-ci PRs that need a full CI run. review wanted PRs that need reviews.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants