Skip to content

child_process: validate strings in exec and spawn #56148

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

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

Conversation

puskin
Copy link
Contributor

@puskin puskin commented Dec 5, 2024

I went through the exec, execFile, spawn, execSync, execFileSync and spawnSync functions in child_process and edited all the functions to properly validate their string parameters

@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 Dec 5, 2024
@puskin puskin force-pushed the child-process-docs-and-validation branch from 358bd79 to 40a0a9f Compare December 5, 2024 17:12
Copy link

codecov bot commented Dec 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.16%. Comparing base (3c2da4b) to head (e67638f).
Report is 1084 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #56148      +/-   ##
==========================================
+ Coverage   87.99%   90.16%   +2.17%     
==========================================
  Files         656      630      -26     
  Lines      188999   186467    -2532     
  Branches    35981    36616     +635     
==========================================
+ Hits       166301   168131    +1830     
+ Misses      15865    11125    -4740     
- Partials     6833     7211     +378     
Files with missing lines Coverage Δ
lib/child_process.js 97.74% <100.00%> (+0.30%) ⬆️

... and 372 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

Can you please remove all the unrelated changes? It makes the PR hard to review. Please only include changes that are necessary to make the added test pass, and all the other changes should be made in a separate PR.

@puskin puskin force-pushed the child-process-docs-and-validation branch from 40a0a9f to 18ddc47 Compare December 9, 2024 07:55
@puskin puskin force-pushed the child-process-docs-and-validation branch from 18ddc47 to 6d02bc8 Compare December 9, 2024 11:21
@puskin puskin force-pushed the child-process-docs-and-validation branch from 6d02bc8 to 0a84382 Compare December 9, 2024 13:43
@aduh95 aduh95 added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Dec 10, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Dec 10, 2024
@nodejs-github-bot
Copy link
Collaborator

@puskin puskin changed the title child_process: improve docs and validate strings in exec and spawn child_process: validate strings in exec and spawn Dec 10, 2024
@nodejs-github-bot
Copy link
Collaborator

aduh95
aduh95 previously requested changes Dec 11, 2024
Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

It looks like the added test pass on main, meaning either the change in lib/ is only a refactor, or we are not adding sufficient coverage to avoid regression.

@puskin
Copy link
Contributor Author

puskin commented Dec 11, 2024

It looks like the added test pass on main, meaning either the change in lib/ is only a refactor, or we are not adding sufficient coverage to avoid regression.

it was mainly refactoring because, for example, when calling execFile, the validation was not done immediately but down the road when, at the very end, the method itself was calling the spawn function.
Tests are passing in main because the validation in spawn is already present; my change is mainly about doing it as soon as possible to reject as soon as possible in order to save some cycles and having a better stack trace

@aduh95
Copy link
Contributor

aduh95 commented Dec 11, 2024

in order to save some cycles and having a better stack trace

I don't think it's the right approach, we should optimize for the happy path, where there are no error thrown. IIUC, with this change we would be checking twice if the arguments are valid, so in order to save some cycles we should not land this.

@puskin puskin force-pushed the child-process-docs-and-validation branch from 0a84382 to a3b5b1a Compare December 11, 2024 11:19
@puskin
Copy link
Contributor Author

puskin commented Dec 11, 2024

in order to save some cycles and having a better stack trace

I don't think it's the right approach, we should optimize for the happy path, where there are no error thrown. IIUC, with this change we would be checking twice if the arguments are valid, so in order to save some cycles we should not land this.

gotcha. I went in that direction because I noticed that was the case already. With the latest push all the validation is done down the line and only once

@pmarchini pmarchini requested a review from aduh95 January 8, 2025 09:51
@aduh95
Copy link
Contributor

aduh95 commented Jan 8, 2025

The added tests are passing on latest main. If we want to move forward with this, we should split this into two PRs/commits, one that increases the coverage, one that refactors the implementation

@puskin puskin force-pushed the child-process-docs-and-validation branch from a3b5b1a to 80fcad1 Compare January 8, 2025 19:35
@aduh95
Copy link
Contributor

aduh95 commented Jan 8, 2025

The commit message of 80fcad1 should say it's a refactor, e.g. child_process: refactor string validation in exec and spawn.

The commit message of 2f43432 should be using test: subsystem, not child_process:, e.g. test: increase coverage of argument validation of cp methods.

IMO the order of commits should be reversed, tests should land first.

@puskin puskin force-pushed the child-process-docs-and-validation branch from 2f43432 to c07b9d8 Compare January 9, 2025 08:38
@puskin puskin force-pushed the child-process-docs-and-validation branch from c07b9d8 to 52c7bb1 Compare January 9, 2025 08:41
@aduh95 aduh95 dismissed their stale review January 13, 2025 21:50

Addressed

@puskin
Copy link
Contributor Author

puskin commented Jan 17, 2025

Is anything else needed here?

@jasnell jasnell added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 19, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 21, 2025
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator

@puskin puskin requested a review from ZYSzys January 26, 2025 20:11
@nodejs-github-bot
Copy link
Collaborator

@puskin
Copy link
Contributor Author

puskin commented Feb 21, 2025

can I please request a rerun of the pipeline?

@puskin
Copy link
Contributor Author

puskin commented Mar 21, 2025

anything else needed here?

Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

LGTM with the only the two obsolete validations removed.

Comment on lines 535 to 545
function validateStringParam(param, paramName) {
validateString(param, paramName);
validateArgumentNullCheck(param, paramName);

if (param.length === 0) {
throw new ERR_INVALID_ARG_VALUE(paramName, param, 'cannot be empty');
}
}

if (file.length === 0)
throw new ERR_INVALID_ARG_VALUE('file', file, 'cannot be empty');
function normalizeSpawnArguments(file, args, options) {
validateStringParam(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.

If we keep the changes as before, the compiler has to do less inlining.

Suggested change
function validateStringParam(param, paramName) {
validateString(param, paramName);
validateArgumentNullCheck(param, paramName);
if (param.length === 0) {
throw new ERR_INVALID_ARG_VALUE(paramName, param, 'cannot be empty');
}
}
if (file.length === 0)
throw new ERR_INVALID_ARG_VALUE('file', file, 'cannot be empty');
function normalizeSpawnArguments(file, args, options) {
validateStringParam(file, 'file');
function normalizeSpawnArguments(file, args, options) {
validateString(file, 'file');
validateArgumentNullCheck(file, 'file');
if (file.length === 0) {
throw new ERR_INVALID_ARG_VALUE(file, 'file', 'cannot be empty');
}

@puskin puskin force-pushed the child-process-docs-and-validation branch from 446cf6a to b6fcf31 Compare May 2, 2025 07:48
if (file.length === 0)
throw new ERR_INVALID_ARG_VALUE('file', file, 'cannot be empty');
if (file.length === 0) {
throw new ERR_INVALID_ARG_VALUE(file, 'file', 'cannot be empty');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new ERR_INVALID_ARG_VALUE(file, 'file', 'cannot be empty');
throw new ERR_INVALID_ARG_VALUE('file', file, 'cannot be empty');

@puskin puskin force-pushed the child-process-docs-and-validation branch from b6fcf31 to e67638f Compare May 2, 2025 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. child_process Issues and PRs related to the child_process subsystem. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants