Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from wikimedia/bug/T247770
Fix local IP address detection
- Loading branch information
Showing
4 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Regular expression to classify if a valid IPv4 or IPv6 address is local. | ||
* Note: does not validate that the string is a valid IPv4 or IPv6 address. | ||
* Note: assumes the local IP address uses the private class A address range. | ||
*/ | ||
const LOCAL_IP = /^::1$|^(?:::ffff:)?(?:10|127)\./; | ||
|
||
module.exports = { | ||
LOCAL_IP | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
var assert = require('../utils/assert.js'); | ||
var lib = require('../../lib/regex'); | ||
|
||
describe('Regular expressions', () => { | ||
describe('should recognize internal addresses', () => { | ||
it('IPv4: loopback address to the local host', () => { | ||
assert.deepEqual(lib.LOCAL_IP.test('127.0.0.1'), true); | ||
}); | ||
|
||
it('IPv4: private address, class A', () => { | ||
assert.deepEqual(lib.LOCAL_IP.test('10.0.0.1'), true); | ||
}); | ||
|
||
it('IPv6: loopback address to the local host', () => { | ||
assert.deepEqual(lib.LOCAL_IP.test('::1'), true); | ||
}); | ||
|
||
it('IPv6: IPv4 mapped address', () => { | ||
assert.deepEqual(lib.LOCAL_IP.test('::ffff:10.0.0.0'), true); | ||
}); | ||
}); | ||
|
||
describe('should recognize external addresses', () => { | ||
it('external IPv4 address', () => { | ||
assert.deepEqual(lib.LOCAL_IP.test('55.55.55.55'), false); | ||
}); | ||
|
||
// https://phabricator.wikimedia.org/T247770 | ||
it('external IPv4 address with a 10 inside', () => { | ||
assert.deepEqual(lib.LOCAL_IP.test('55.55.10.55'), false); | ||
}); | ||
|
||
// https://phabricator.wikimedia.org/T247770 | ||
it('external IPv4 address with a 127 inside', () => { | ||
assert.deepEqual(lib.LOCAL_IP.test('55.127.55.55'), false); | ||
}); | ||
}); | ||
}); |