-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generator for vulnerability identifiers (#2367)
Starting with CVE identifiers, but allowing to add further reference systems (e.g. GHSA) to the same class later on.
- Loading branch information
1 parent
7948474
commit 97ba9dd
Showing
4 changed files
with
48 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,7 @@ | ||
# Faker::VulnerabilityIdentifier | ||
|
||
Available since version next. | ||
|
||
```ruby | ||
Faker::VulnerabilityIdentifier.cve #=> "CVE-2021-1337" | ||
``` |
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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Faker | ||
class VulnerabilityIdentifier < Base | ||
class << self | ||
## | ||
# Produces a Common Vulnerabilities and Exposures (CVE) identifier. | ||
# | ||
# @param year [Integer] The year-part of the CVE identifier (defaults to the current year) | ||
# @return [String] | ||
# | ||
# @example | ||
# Faker::VulnerabilityIdentifier.cve #=> "CVE-2021-1337" | ||
# Faker::VulnerabilityIdentifier.cve(year: 1999) #=> "CVE-1999-0523" | ||
# | ||
# @faker.version next | ||
def cve(year: ::Date.today.year) | ||
index = rand_in_range(1, 99_999).to_s.rjust(4, '0') | ||
"CVE-#{year}-#{index}" | ||
end | ||
end | ||
end | ||
end |
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../test_helper' | ||
|
||
class TestFakerVulnerabilityIdentifier < Test::Unit::TestCase | ||
def setup | ||
@tester = Faker::VulnerabilityIdentifier | ||
end | ||
|
||
def test_cve_no_args | ||
assert @tester.cve.match(/^CVE-\d{4}-\d{4,}$/) | ||
end | ||
|
||
def test_cve_with_year | ||
assert @tester.cve(year: 2012).match(/^CVE-2012-\d{4,}$/) | ||
end | ||
end |