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

add "any" IP to is_local_ip, add TEST-NETs to is_private_ip #59

Merged
merged 4 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .github/workflows/ci-test-win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
strategy:
matrix:
os: [ windows-latest ]
node-version: [10.x, 12.x]
node-version: [12.x, 14.x]
fail-fast: false

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
name: Checkout code
with:
fetch-depth: 1
Expand All @@ -28,10 +28,11 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: npm install and test
run: |
npm install
npm run test
- name: Install
run: npm install

- name: Test
run: npm run test

env:
CI: true
8 changes: 4 additions & 4 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest ]
node-version: [10.x, 12.x, 14.x]
node-version: [12.x, 14.x]
fail-fast: false

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
name: Checkout code
with:
fetch-depth: 1
Expand All @@ -25,10 +25,10 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: npm install
- name: Install
run: npm install

- name: Run test suite
- name: Test
run: npm run test

env:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/coveralls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
with:
fetch-depth: 1

- name: Use Node.js 10
- name: Use Node.js 12
uses: actions/setup-node@master
with:
node-version: 10.x
node-version: 12.x

- name: install, run
run: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
node-version: [ 12.x ]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
name: Checkout code
with:
fetch-depth: 1
Expand All @@ -23,10 +23,10 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: npm install
- name: Install
run: npm install

- name: Lint using eslint
- name: Lint
run: npm run lint

env:
Expand Down
5 changes: 5 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

### 1.2.4 - 2021-01-14

- add "any" IP to is_local_ip
- add TEST-NET-[1-3] to is_private_ip

### 1.2.3 - 2020-12-19

- fix: restore the tests wrapping the resolveMX iterable
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ const re_ipv4 = {
private192: /^192\.168\./, // 192.168/16
// 172.16/16 .. 172.31/16
private172: /^172\.(1[6-9]|2[0-9]|3[01])\./, // 172.16/12

// RFC 5735
testnet1: /^192\.0\.2\./, // 192.0.2.0/24
testnet2: /^198\.51\.100\./, // 198.51.100.0/24
testnet3: /^203\.0\.113\./, // 203.0.113.0/24
}

exports.is_private_ipv4 = function (ip) {
Expand All @@ -114,6 +119,10 @@ exports.is_private_ipv4 = function (ip) {
if (re_ipv4.private192.test(ip)) return true;
if (re_ipv4.private172.test(ip)) return true;

if (re_ipv4.testnet1.test(ip)) return true;
if (re_ipv4.testnet2.test(ip)) return true;
if (re_ipv4.testnet3.test(ip)) return true;

return false;
}

Expand Down Expand Up @@ -143,6 +152,8 @@ exports.is_local_ip = function (ip) {
}

exports.is_local_ipv4 = function (ip) {
if ('0.0.0.0' === ip) return true; // RFC 5735

// 127/8 (loopback) # RFC 1122
if (re_ipv4.loopback.test(ip)) return true;

Expand All @@ -159,6 +170,7 @@ const re_ipv6 = {
}

exports.is_local_ipv6 = function (ip) {
if (ip === '::') return true; // RFC 5735
if (ip === '::1') return true; // RFC 4291

// 2 more IPv6 notations for ::1
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "haraka-net-utils",
"version": "1.2.3",
"version": "1.2.4",
"description": "haraka network utilities",
"main": "index.js",
"scripts": {
Expand Down
27 changes: 26 additions & 1 deletion test/net_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('same_ipv4_network', function () {
})

describe('is_ipv4_literal', function () {
it('3 ways ', function (done) {
it('3 ways', function (done) {
assert.equal(true, net_utils.is_ipv4_literal('[127.0.0.1]'));
assert.equal(false, net_utils.is_ipv4_literal('127.0.0.1'));
assert.equal(false, net_utils.is_ipv4_literal('test.host'));
Expand Down Expand Up @@ -124,6 +124,14 @@ describe('is_local_ip', function () {
it('239.0.0.1 (multicast; not currently considered rfc1918)', function (done) {
_is_local_ip(done, '239.0.0.1', false);
})

it('0.0.0.0', function (done) {
_is_local_ip(done, '0.0.0.0', true);
})

it('::', function (done) {
_is_local_ip(done, '::', true);
})
})

describe('is_private_ip', function () {
Expand Down Expand Up @@ -174,6 +182,18 @@ describe('is_private_ip', function () {
it('239.0.0.1 (multicast; not currently considered rfc1918)', function (done) {
_is_private_ip(done, '239.0.0.1', false);
})

it('192.0.2.1 TEST-NET-1', function (done) {
_is_private_ip(done, '192.0.2.1', true);
})

it('198.51.100.0 TEST-NET-2', function (done) {
_is_private_ip(done, '198.51.100.0', true);
})

it('203.0.113.0 TEST-NET-3', function (done) {
_is_private_ip(done, '203.0.113.0', true);
})
})

describe('get_public_ip', function () {
Expand Down Expand Up @@ -316,6 +336,11 @@ describe('is_private_ipv4', function () {
})

describe('is_local_ipv6', function () {
it('::', function (done) {
assert.equal(net_utils.is_local_ipv6('::'), true);
done();
})

it('::1', function (done) {
assert.equal(net_utils.is_local_ipv6('::1'), true);
assert.equal(net_utils.is_local_ipv6('0:0:0:0:0:0:0:1'), true);
Expand Down