forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: Truncating IPs using geo activity (prebid#12107)
* 11395 Truncating IPs using geo activity * refactor * returning null for invalid ip --------- Co-authored-by: Marcin Komorski <marcinkomorski@Marcins-MacBook-Pro.local>
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 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
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,52 @@ | ||
export function scrubIPv4(ip) { | ||
if (!ip) { | ||
return null; | ||
} | ||
|
||
const ones = 24; | ||
|
||
let ipParts = ip.split('.').map(Number) | ||
|
||
if (ipParts.length != 4) { | ||
return null; | ||
} | ||
|
||
let mask = []; | ||
for (let i = 0; i < 4; i++) { | ||
let n = Math.max(0, Math.min(8, ones - (i * 8))); | ||
mask.push((0xff << (8 - n)) & 0xff); | ||
} | ||
|
||
let maskedIP = ipParts.map((part, i) => part & mask[i]); | ||
|
||
return maskedIP.join('.'); | ||
} | ||
|
||
export function scrubIPv6(ip) { | ||
if (!ip) { | ||
return null; | ||
} | ||
|
||
const ones = 64; | ||
|
||
let ipParts = ip.split(':').map(part => parseInt(part, 16)); | ||
|
||
ipParts = ipParts.map(part => isNaN(part) ? 0 : part); | ||
while (ipParts.length < 8) { | ||
ipParts.push(0); | ||
} | ||
|
||
if (ipParts.length != 8) { | ||
return null; | ||
} | ||
|
||
let mask = []; | ||
for (let i = 0; i < 8; i++) { | ||
let n = Math.max(0, Math.min(16, ones - (i * 16))); | ||
mask.push((0xffff << (16 - n)) & 0xffff); | ||
} | ||
|
||
let maskedIP = ipParts.map((part, i) => part & mask[i]); | ||
|
||
return maskedIP.map(part => part.toString(16)).join(':'); | ||
} |
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,58 @@ | ||
import { scrubIPv4, scrubIPv6 } from '../../../../src/utils/ipUtils' | ||
|
||
describe('ipUtils', () => { | ||
describe('ipv4', () => { | ||
it('should mask ip v4', () => { | ||
let input = '192.168.1.1'; | ||
let output = scrubIPv4(input); | ||
expect(output).to.deep.equal('192.168.1.0'); | ||
input = '192.168.255.255'; | ||
output = scrubIPv4(input); | ||
expect(output).to.deep.equal('192.168.255.0'); | ||
}); | ||
|
||
it('should return null for null input', () => { | ||
let input = null; | ||
let output = scrubIPv4(input); | ||
expect(output).to.deep.equal(null); | ||
}); | ||
|
||
it('should convert invalid format to null', () => { | ||
let invalidIp = '192.130.2'; | ||
let output = scrubIPv4(invalidIp); | ||
expect(output).to.deep.equal(null); | ||
}); | ||
|
||
it('should convert invalid format to null', () => { | ||
let invalidIp = '2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF'; | ||
let output = scrubIPv4(invalidIp); | ||
expect(output).to.deep.equal(null); | ||
}); | ||
}); | ||
|
||
describe('ipv6', () => { | ||
it('should mask ip v6', () => { | ||
let input = '2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF'; | ||
let output = scrubIPv6(input); | ||
expect(output).to.deep.equal('2001:db8:3333:4444:0:0:0:0'); | ||
}); | ||
|
||
it('should return null for null input', () => { | ||
let input = null; | ||
let output = scrubIPv6(input); | ||
expect(output).to.deep.equal(null); | ||
}); | ||
|
||
it('should convert invalid format to null', () => { | ||
let invalidIp = '2001:db8:3333:4444:CCCC:DDDD:EEEE'; | ||
let output = scrubIPv4(invalidIp); | ||
expect(output).to.deep.equal(null); | ||
}); | ||
|
||
it('should convert invalid format to null', () => { | ||
let invalidIp = 'invalid'; | ||
let output = scrubIPv4(invalidIp); | ||
expect(output).to.deep.equal(null); | ||
}); | ||
}); | ||
}) |