Skip to content

Commit

Permalink
Add generator for vulnerability identifiers (#2367)
Browse files Browse the repository at this point in the history
Starting with CVE identifiers, but allowing
to add further reference systems (e.g. GHSA) to the same class later on.
  • Loading branch information
NobodysNightmare authored Aug 8, 2022
1 parent 7948474 commit 97ba9dd
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -217,6 +217,7 @@ gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'mast
- [Faker::University](doc/default/university.md)
- [Faker::Vehicle](doc/default/vehicle.md)
- [Faker::Verbs](doc/default/verbs.md)
- [Faker::VulnerabilityIdentifier](doc/default/vulnerability_identifier.md)
- [Faker::WorldCup](doc/default/world_cup.md)

### Blockchain
7 changes: 7 additions & 0 deletions doc/default/vulnerability_identifier.md
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"
```
23 changes: 23 additions & 0 deletions lib/faker/default/vulnerability_identifier.rb
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
17 changes: 17 additions & 0 deletions test/faker/default/test_faker_vulnerability_identifier.rb
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

0 comments on commit 97ba9dd

Please sign in to comment.