node icon indicating copy to clipboard operation
node copied to clipboard

child_process: validate strings in exec and spawn

Open puskin opened this issue 1 year ago • 14 comments

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

puskin avatar Dec 05 '24 16:12 puskin

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 90.16%. Comparing base (3c2da4b) to head (e67638f). Report is 1531 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%) :arrow_up:

... and 372 files with indirect coverage changes

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

codecov[bot] avatar Dec 05 '24 18:12 codecov[bot]

CI: https://ci.nodejs.org/job/node-test-pull-request/63969/

nodejs-github-bot avatar Dec 10 '24 00:12 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/63992/

nodejs-github-bot avatar Dec 10 '24 23:12 nodejs-github-bot

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

puskin avatar Dec 11 '24 10:12 puskin

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.

aduh95 avatar Dec 11 '24 10:12 aduh95

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

puskin avatar Dec 11 '24 11:12 puskin

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

aduh95 avatar Jan 08 '25 10:01 aduh95

The commit message of https://github.com/nodejs/node/pull/56148/commits/80fcad1c2e36e27809642087c6fda99e32442b18 should say it's a refactor, e.g. child_process: refactor string validation in exec and spawn.

The commit message of https://github.com/nodejs/node/pull/56148/commits/2f434320482e72d85d848c58ea482158b7a25c26 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.

aduh95 avatar Jan 08 '25 23:01 aduh95

Is anything else needed here?

puskin avatar Jan 17 '25 08:01 puskin

CI: https://ci.nodejs.org/job/node-test-pull-request/64589/

nodejs-github-bot avatar Jan 21 '25 20:01 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/64617/

nodejs-github-bot avatar Jan 22 '25 19:01 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/64998/

nodejs-github-bot avatar Feb 05 '25 06:02 nodejs-github-bot

can I please request a rerun of the pipeline?

puskin avatar Feb 21 '25 14:02 puskin

anything else needed here?

puskin avatar Mar 21 '25 15:03 puskin