Skip to content

os: fix netmask format check condition in getCIDR function - #57324

Merged
nodejs-github-bot merged 2 commits into
nodejs:mainfrom
HBSPS:os-getCIDR
Apr 20, 2025
Merged

os: fix netmask format check condition in getCIDR function#57324
nodejs-github-bot merged 2 commits into
nodejs:mainfrom
HBSPS:os-getCIDR

Conversation

@HBSPS

@HBSPSHBSPS commented Mar 5, 2025

Copy link
Copy Markdown
Member

Modified to check the format of the netmask instead of just checking that each part of the netmask is even number.
(Ref: https://www.rfc-editor.org/rfc/rfc1878)

The result of the previous run was like this.

...
if((binary&1)!==0){returnnull;}
...
console.log(getCIDR('127.0.0.1','242.0.0.0','IPv4'));// 127.0.0.1/5// 242 = 11110010(2)

This does not satisfy the condition in netmask, but it passes the conditional statement and returns an invalid value.

I'm not sure if that conditional is necessary(if the correct value is always passed, etc), but I think it's doing the wrong thing, so I've modified it to look like this.

...
if(StringPrototypeIncludes(binary.toString(2),'01')){returnnull;}console.log(getCIDR('127.0.0.1','242.0.0.0','IPv4'));// null
...

I modified the netmask to check if it satisfies the condition of containing '01', such as 11110010, since a 0 cannot be followed by a 1 according to the rules of netmask.

The benchmark results for changing conditions are shown below.

 confidence improvement accuracy (*) (**) (***)
os/networkInterfaces.js n=10000 0.19 % ±1.17% ±1.56% ±2.03%
Be aware that when doing many comparisons the risk of a false-positive result increases.
In this case, there are 1 comparisons, you can thus expect the following amount of false-positive results:
0.05 false positives, when considering a 5% risk acceptance (*, **, ***),
0.01 false positives, when considering a 1% risk acceptance (**, ***),
0.00 false positives, when considering a 0.1% risk acceptance (***)

Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. os Issues and PRs related to the os subsystem. labels Mar 5, 2025
@codecov

codecovBot commented Mar 5, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.22%. Comparing base (db00f94) to head (6e529a4).
Report is 319 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #57324 +/- ##
==========================================
+ Coverage 90.20% 90.22% +0.01% 
==========================================
Files 630 630 Lines 185166 185203 +37 Branches 36227 36247 +20 ==========================================
+ Hits 167036 167102 +66 + Misses 11117 11044 -73 - Partials 7013 7057 +44 
Files with missing linesCoverage Δ
lib/internal/util.js96.05% <100.00%> (+0.24%)⬆️
lib/os.js98.59% <100.00%> (+1.27%)⬆️

... and 63 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.

@lpinca

Copy link
Copy Markdown
Member

Can you please add a test?

@HBSPS

HBSPS commented Mar 7, 2025

Copy link
Copy Markdown
MemberAuthor

Can you please add a test?

@lpinca This function is only being used by networkInterfaces, which is getting it from internalBinding.

Since networkInterfaces is not a pure function and returns a value based on the current state of the system, is there any way to do mocking or something in this regard?
(I'm not familiar with this kind of testing)

If you have any references or know how to do it, I'd appreciate it :)

functionnetworkInterfaces(){constdata=getInterfaceAddresses();constresult={};if(data===undefined)returnresult;for(leti=0;i<data.length;i+=7){constname=data[i];constentry={address: data[i+1],netmask: data[i+2],family: data[i+3],mac: data[i+4],internal: data[i+5],cidr: getCIDR(data[i+1],data[i+2],data[i+3]),};constscopeid=data[i+6];if(scopeid!==-1)entry.scopeid=scopeid;constexisting=result[name];if(existing!==undefined)ArrayPrototypePush(existing,entry);elseresult[name]=[entry];}returnresult;}

@lpinca

lpinca commented Mar 8, 2025

Copy link
Copy Markdown
Member

What do think about moving it to lib/internal/util.js or lib/internal/os/utils.js? The latter does not exist yet.

move to util/internal and add getCIDR function test file
to test the getCIDR function and added test code for the part
that checks the format of the subnetmask
@HBSPS

HBSPS commented Mar 9, 2025

Copy link
Copy Markdown
MemberAuthor

What do think about moving it to lib/internal/util.js or lib/internal/os/utils.js? The latter does not exist yet.

@lpinca Sounds like a good idea, I moved the getCIDR function to internal/util as you said and created a new test file to test that function.

@lpincalpinca added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 9, 2025
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 9, 2025
@nodejs-github-bot

This comment was marked as outdated.

@HBSPS

HBSPS commented Apr 2, 2025

Copy link
Copy Markdown
MemberAuthor

@lpinca PR seems to have stopped here, what should I do?

@lpincalpinca added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 19, 2025
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 19, 2025
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

nodejs-github-bot commented Apr 19, 2025

Copy link
Copy Markdown
Collaborator

@lpincalpinca added the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 20, 2025
@nodejs-github-botnodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Apr 20, 2025
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator
Commit Queue failed
- Loading data for nodejs/node/pull/57324
✔ Done loading data for nodejs/node/pull/57324
----------------------------------- PR info ------------------------------------
Title os: fix netmask format check condition in getCIDR function (#57324)
Author Wiyeong Seo <wiyeong79@gmail.com> (@HBSPS)
Branch HBSPS:os-getCIDR -> nodejs:main
Labels os, needs-ci
Commits 2
- os: fix netmask format check condition in getCIDR function
- test: move getCIDR function to internal/util and add test code
Committers 1
- HBSPS <wiyeong79@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
--------------------------------------------------------------------------------
ℹ This PR was created on Wed, 05 Mar 2025 02:44:05 GMT
✔ Approvals: 2
✔ - Luigi Pinca (@lpinca): https://github.com/nodejs/node/pull/57324#pullrequestreview-2669497839
✔ - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/57324#pullrequestreview-2780071018
✔ Last GitHub CI successful
ℹ Last Full PR CI on 2025-04-19T21:27:46Z: https://ci.nodejs.org/job/node-test-pull-request/66371/
- Querying data for job/node-test-pull-request/66371/
✔ Last Jenkins CI successful
--------------------------------------------------------------------------------
✔ No git cherry-pick in progress
✔ No git am in progress
✔ No git rebase in progress
--------------------------------------------------------------------------------
- Bringing origin/main up to date...
From https://github.com/nodejs/node
* branch main -> FETCH_HEAD
✔ origin/main is now up-to-date
- Downloading patch for 57324
From https://github.com/nodejs/node
* branch refs/pull/57324/merge -> FETCH_HEAD
✔ Fetched commits as 25842c5e35ef..6e529a46153e
--------------------------------------------------------------------------------
[main c2a5b7fda5] os: fix netmask format check condition in getCIDR function
Author: HBSPS <wiyeong79@gmail.com>
Date: Tue Mar 4 20:27:46 2025 -0600
1 file changed, 2 insertions(+), 1 deletion(-)
Auto-merging lib/internal/util.js
[main dd12864109] test: move getCIDR function to internal/util and add test code
Author: HBSPS <wiyeong79@gmail.com>
Date: Sun Mar 9 13:36:08 2025 -0500
3 files changed, 81 insertions(+), 56 deletions(-)
create mode 100644 test/parallel/test-internal-util-getCIDR.js
✔ Patches applied
There are 2 commits in the PR. Attempting autorebase.
Rebasing (2/4)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
os: fix netmask format check condition in getCIDR function

Modified to check the format of the netmask instead
of just checking that each part of the netmask is even

PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>

[detached HEAD 6933aa3823] os: fix netmask format check condition in getCIDR function
Author: HBSPS <wiyeong79@gmail.com>
Date: Tue Mar 4 20:27:46 2025 -0600
1 file changed, 2 insertions(+), 1 deletion(-)
Rebasing (3/4)
Rebasing (4/4)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
test: move getCIDR function to internal/util and add test code

move to util/internal and add getCIDR function test file
to test the getCIDR function and added test code for the part
that checks the format of the subnetmask

PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>

[detached HEAD a054662fbd] test: move getCIDR function to internal/util and add test code
Author: HBSPS <wiyeong79@gmail.com>
Date: Sun Mar 9 13:36:08 2025 -0500
3 files changed, 81 insertions(+), 56 deletions(-)
create mode 100644 test/parallel/test-internal-util-getCIDR.js
Successfully rebased and updated refs/heads/main.

ℹ Add commit-queue-squash label to land the PR as one commit, or commit-queue-rebase to land as separate commits.

https://github.com/nodejs/node/actions/runs/14556471756

@lpincalpinca added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Apr 20, 2025
@nodejs-github-botnodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 20, 2025
@nodejs-github-bot
nodejs-github-bot merged commit 7102ea1 into nodejs:mainApr 20, 2025
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 7102ea1

RafaelGSS pushed a commit that referenced this pull request May 1, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this pull request May 2, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request May 6, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request May 6, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this pull request May 14, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request May 16, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request May 17, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request May 18, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request May 19, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
marco-ippolito pushed a commit that referenced this pull request Jun 5, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
marco-ippolito pushed a commit that referenced this pull request Jun 5, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@marco-ippolitomarco-ippolito mentioned this pull request Jun 5, 2025
@ghostghost mentioned this pull request Jun 8, 2025
marco-ippolito pushed a commit that referenced this pull request Jun 10, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
marco-ippolito pushed a commit that referenced this pull request Jun 11, 2025
Modified to check the format of the netmask instead
of just checking that each part of the netmask is even
PR-URL: #57324
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit-queue-squashAdd this label to instruct the Commit Queue to squash all the PR commits into the first one.needs-ciPRs that need a full CI run.osIssues and PRs related to the os subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@HBSPS@lpinca@nodejs-github-bot@jasnell