-
-
Notifications
You must be signed in to change notification settings - Fork 172
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
feat: Add IPv4 CIDR and IPv6 CIDR validations #367
Open
langscot
wants to merge
3
commits into
fabian-hiller:main
Choose a base branch
from
langscot:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ | ||
export * from './ipv4Cidr.ts'; |
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,33 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { ipv4Cidr } from './ipv4Cidr.ts'; | ||
|
||
describe('ipv4cidr', () => { | ||
test('should pass only IP v4 CIDR notation', () => { | ||
const validate = ipv4Cidr(); | ||
const value1 = '192.168.1.1/24'; | ||
expect(validate._parse(value1).output).toBe(value1); | ||
const value2 = '127.0.0.1/16'; | ||
expect(validate._parse(value2).output).toBe(value2); | ||
const value3 = '0.0.0.0/0'; | ||
expect(validate._parse(value3).output).toBe(value3); | ||
const value4 = '255.255.255.255/32'; | ||
expect(validate._parse(value4).output).toBe(value4); | ||
|
||
expect(validate._parse('').issues).toBeTruthy(); | ||
expect(validate._parse('1').issues).toBeTruthy(); | ||
expect(validate._parse('-1.0.0.0').issues).toBeTruthy(); | ||
expect(validate._parse('0..0.0.0').issues).toBeTruthy(); | ||
expect(validate._parse('1234.0.0.0').issues).toBeTruthy(); | ||
expect(validate._parse('256.256.256.256').issues).toBeTruthy(); | ||
expect(validate._parse('1.2.3').issues).toBeTruthy(); | ||
expect(validate._parse('192.168.1.1/01').issues).toBeTruthy(); | ||
expect(validate._parse('192.168.1.1/33').issues).toBeTruthy(); | ||
expect(validate._parse('192.168.1.1/-1').issues).toBeTruthy(); | ||
}); | ||
|
||
test('should return custom error message', () => { | ||
const error = 'Value is not in IP v4 CIDR notation!'; | ||
const validate = ipv4Cidr(error); | ||
expect(validate._parse('test').issues?.[0].message).toBe(error); | ||
}); | ||
}); |
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 @@ | ||
import { IPV4_CIDR_REGEX } from '../../regex.ts'; | ||
import type { BaseValidation, ErrorMessage } from '../../types/index.ts'; | ||
import { actionIssue, actionOutput } from '../../utils/index.ts'; | ||
|
||
/** | ||
* IPv4 CIDR validation type. | ||
*/ | ||
export type Ipv4CidrValidation<TInput extends string> = BaseValidation<TInput> & { | ||
/** | ||
* The validation type. | ||
*/ | ||
type: 'ipv4cidr'; | ||
/** | ||
* The IPv4 CIDR regex. | ||
*/ | ||
requirement: RegExp; | ||
}; | ||
|
||
/** | ||
* Creates a pipeline validation action that validates a [IPv4 CIDR](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#IPv4_CIDR_blocks) notation. | ||
* | ||
* @param message The error message. | ||
* | ||
* @returns A validation action. | ||
*/ | ||
export function ipv4Cidr<TInput extends string>( | ||
message: ErrorMessage = 'Invalid IPv4 CIDR' | ||
): Ipv4CidrValidation<TInput> { | ||
return { | ||
type: 'ipv4cidr', | ||
async: false, | ||
message, | ||
requirement: IPV4_CIDR_REGEX, | ||
_parse(input) { | ||
return !this.requirement.test(input) | ||
? actionIssue(this.type, this.message, input, this.requirement) | ||
: actionOutput(input); | ||
}, | ||
}; | ||
} |
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 @@ | ||
export * from './ipv6Cidr.ts'; |
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,36 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { ipv6Cidr } from './ipv6Cidr.ts'; | ||
|
||
describe('ipv6', () => { | ||
test('should pass only IP v6 CIDR notation', () => { | ||
const validate = ipv6Cidr(); | ||
const value1 = '2001:0db8:85a3:0000:0000:8a2e:0370:7334/0'; | ||
expect(validate._parse(value1).output).toBe(value1); | ||
const value2 = 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329/64'; | ||
expect(validate._parse(value2).output).toBe(value2); | ||
const value3 = 'fe80::1ff:fe23:4567:890a/128'; | ||
expect(validate._parse(value3).output).toBe(value3); | ||
const value4 = '2001:db8:85a3:8d3:1319:8a2e:370:7348/48'; | ||
expect(validate._parse(value4).output).toBe(value4); | ||
|
||
expect(validate._parse('').issues).toBeTruthy(); | ||
expect(validate._parse('1').issues).toBeTruthy(); | ||
expect(validate._parse('1.2.3').issues).toBeTruthy(); | ||
expect(validate._parse('192.168.1.1').issues).toBeTruthy(); | ||
expect(validate._parse('0.0.0.0').issues).toBeTruthy(); | ||
expect( | ||
validate._parse('test:test:test:test:test:test:test:test').issues | ||
).toBeTruthy(); | ||
expect(validate._parse('2001:db8:85a3:8d3:1319:8a2e:370:7348/129').issues).toBeTruthy() | ||
expect(validate._parse('2001:db8:85a3:8d3:1319:8a2e:370:7348/01').issues).toBeTruthy() | ||
expect(validate._parse('2001:db8:85a3:8d3:1319:8a2e:370:7348').issues).toBeTruthy() | ||
expect(validate._parse('2001:db8:85a3:8d3:1319:8a2e:370:7348/-1').issues).toBeTruthy() | ||
expect(validate._parse('2001:db8:85a3:8d3:1319:8a2e:370:7348/test').issues).toBeTruthy() | ||
}); | ||
|
||
test('should return custom error message', () => { | ||
const error = 'Value is not in IP v6 CIDR notation!'; | ||
const validate = ipv6Cidr(error); | ||
expect(validate._parse('test').issues?.[0].message).toBe(error); | ||
}); | ||
}); |
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 @@ | ||
import { IPV6_CIDR_REGEX } from '../../regex.ts'; | ||
import type { BaseValidation, ErrorMessage } from '../../types/index.ts'; | ||
import { actionIssue, actionOutput } from '../../utils/index.ts'; | ||
|
||
/** | ||
* IPv6 CIDR validation type. | ||
*/ | ||
export type Ipv6CidrValidation<TInput extends string> = BaseValidation<TInput> & { | ||
/** | ||
* The validation type. | ||
*/ | ||
type: 'ipv6cidr'; | ||
/** | ||
* The IPv6 CIDR regex. | ||
*/ | ||
requirement: RegExp; | ||
}; | ||
|
||
/** | ||
* Creates a pipeline validation action that validates a [IPv6 CIDR](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#IPv6_CIDR_blocks) notation. | ||
* | ||
* @param message The error message. | ||
* | ||
* @returns A validation action. | ||
*/ | ||
export function ipv6Cidr<TInput extends string>( | ||
message: ErrorMessage = 'Invalid IPv6 CIDR' | ||
): Ipv6CidrValidation<TInput> { | ||
return { | ||
type: 'ipv6cidr', | ||
async: false, | ||
message, | ||
requirement: IPV6_CIDR_REGEX, | ||
_parse(input) { | ||
return !this.requirement.test(input) | ||
? actionIssue(this.type, this.message, input, this.requirement) | ||
: actionOutput(input); | ||
}, | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't fully understand why the
s*
is needed here. What does it do?Additionally, are we sure that both redos and no-super-linear-backtracking are false positives?
I haven't had the time to try https://2bdenny.github.io/ReScue/ & https://github.com/doyensec/regexploit, but if one of them shows a vulnerability too, we have 3 online and at least one offline tool showing it's vulnerable, which tells us it's certainly vulnerable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will look into the vulnerabilities further. I’m not sure I understood them correctly, thanks for pointing this out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kurtextrem thanks for the review, i agree with you. We should not use Regex which doesn't pass those regex exploits checking tools.