Skip to content

Commit

Permalink
Add: Allow to set a start index for NVD APIs
Browse files Browse the repository at this point in the history
Add a start_index argument for querying results on NVD APIs (CVEs, CVE
changes and CPEs). This will allow to use pagination via user requests.
  • Loading branch information
bjoernricks committed Dec 1, 2023
1 parent fea521e commit 79de35d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pontos/nvd/cpe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async def query_cpes(args: Namespace) -> None:
keywords=args.keywords,
cpe_match_string=args.cpe_match_string,
request_results=args.number,
start_index=args.start,
)
async for cpe in response:
print(cpe)
Expand Down Expand Up @@ -69,6 +70,12 @@ def cpes_main() -> None:
parser.add_argument(
"--number", "-n", metavar="N", help="Request only N CPEs", type=int
)
parser.add_argument(
"--start",
"-s",
help="Index of the first CPE to request.",
type=int,
)

main(parser, query_cpes)

Expand Down
4 changes: 3 additions & 1 deletion pontos/nvd/cpe/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def cpes(
keywords: Optional[Union[List[str], str]] = None,
match_criteria_id: Optional[str] = None,
request_results: Optional[int] = None,
start_index: int = 0,
) -> NVDResults[CPE]:
"""
Get all CPEs for the provided arguments
Expand All @@ -159,6 +160,8 @@ def cpes(
string identified by its UUID.
request_results: Number of CPEs to download. Set to None (default)
to download all available CPEs.
start_index: Index of the first CPE to be returned. Useful only for
paginated requests that should not start at the first page.
Returns:
A NVDResponse for CPEs
Expand Down Expand Up @@ -202,7 +205,6 @@ def cpes(
if match_criteria_id:
params["matchCriteriaId"] = match_criteria_id

start_index = 0
results_per_page = (
request_results
if request_results and request_results < MAX_CPES_PER_PAGE
Expand Down
7 changes: 7 additions & 0 deletions pontos/nvd/cve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ async def query_cves(args: Namespace) -> None:
cvss_v3_vector=args.cvss_v3_vector,
source_identifier=args.source_identifier,
request_results=args.number,
start_index=args.start,
):
print(cve)

Expand Down Expand Up @@ -72,6 +73,12 @@ def cves_main() -> None:
parser.add_argument(
"--number", "-n", metavar="N", help="Request only N CVEs", type=int
)
parser.add_argument(
"--start",
"-s",
help="Index of the first CVE to request.",
type=int,
)

main(parser, query_cves)

Expand Down
4 changes: 3 additions & 1 deletion pontos/nvd/cve/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def cves(
has_kev: Optional[bool] = None,
has_oval: Optional[bool] = None,
request_results: Optional[int] = None,
start_index: int = 0,
) -> NVDResults[CVE]:
"""
Get all CVEs for the provided arguments
Expand Down Expand Up @@ -171,6 +172,8 @@ def cves(
transitioned to the Center for Internet Security (CIS).
request_results: Number of CVEs to download. Set to None (default)
to download all available CVEs.
start_index: Index of the first CVE to be returned. Useful only for
paginated requests that should not start at the first page.
Returns:
A NVDResponse for CVEs
Expand Down Expand Up @@ -249,7 +252,6 @@ def cves(
if has_oval:
params["hasOval"] = ""

start_index = 0
results_per_page = (
request_results
if request_results and request_results < MAX_CVES_PER_PAGE
Expand Down
7 changes: 7 additions & 0 deletions pontos/nvd/cve_changes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async def query_changes(args: Namespace) -> None:
cve_id=args.cve_id,
event_name=args.event_name,
request_results=args.number,
start_index=args.start,
):
print(cve)

Expand All @@ -30,6 +31,12 @@ def parse_args() -> Namespace:
parser.add_argument(
"--number", "-n", metavar="N", help="Request only N CPEs", type=int
)
parser.add_argument(
"--start",
"-s",
help="Index of the first CPE to request.",
type=int,
)
return parser.parse_args()


Expand Down
5 changes: 4 additions & 1 deletion pontos/nvd/cve_changes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def changes(
cve_id: Optional[str] = None,
event_name: Optional[Union[EventName, str]] = None,
request_results: Optional[int] = None,
start_index: int = 0,
) -> NVDResults[CVEChange]:
"""
Get all CVEs for the provided arguments
Expand All @@ -98,6 +99,9 @@ def changes(
event_name: Return all CVE changes with this event name.
request_results: Number of CVEs changes to download. Set to None
(default) to download all available CPEs.
start_index: Index of the first CVE change to be returned. Useful
only for paginated requests that should not start at the first
page.
Returns:
A NVDResponse for CVE changes
Expand Down Expand Up @@ -142,7 +146,6 @@ def changes(
if event_name:
params["eventName"] = event_name

start_index: int = 0
results_per_page = (
request_results
if request_results and request_results < MAX_CVE_CHANGES_PER_PAGE
Expand Down

0 comments on commit 79de35d

Please sign in to comment.