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

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

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

Conversation

HBSPS
Copy link
Contributor

@HBSPS HBSPS commented Mar 5, 2025

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) {
  return null;
}
...

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')) {
  return null;
}

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-bot nodejs-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
Copy link

codecov bot commented Mar 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.22%. Comparing base (db00f94) to head (6e529a4).
Report is 39 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 lines Coverage Δ
lib/internal/util.js 96.05% <100.00%> (+0.24%) ⬆️
lib/os.js 98.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
Member

lpinca commented Mar 5, 2025

Can you please add a test?

@HBSPS
Copy link
Contributor Author

HBSPS commented Mar 7, 2025

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 :)

function networkInterfaces() {
  const data = getInterfaceAddresses();
  const result = {};

  if (data === undefined)
    return result;
  for (let i = 0; i < data.length; i += 7) {
    const name = data[i];
    const entry = {
      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]),
    };
    const scopeid = data[i + 6];
    if (scopeid !== -1)
      entry.scopeid = scopeid;

    const existing = result[name];
    if (existing !== undefined)
      ArrayPrototypePush(existing, entry);
    else
      result[name] = [entry];
  }

  return result;
}

@lpinca
Copy link
Member

lpinca commented Mar 8, 2025

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
Copy link
Contributor Author

HBSPS commented Mar 9, 2025

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.

@lpinca lpinca added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 9, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 9, 2025
@nodejs-github-bot
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. os Issues and PRs related to the os subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants