Skip to content
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

API endpoint #56

Open
skontar opened this issue Apr 17, 2024 · 7 comments
Open

API endpoint #56

skontar opened this issue Apr 17, 2024 · 7 comments

Comments

@skontar
Copy link
Collaborator

skontar commented Apr 17, 2024

It would be a good idea to have another address which can be queried with a vector and just return a JSON with a result, maybe also copy of provided vector, something like:
https://redhatproductsecurity.github.io/cvss-v4-calculator/api/#CVSS:4.0/AV:A/AC:H/AT:N/PR:N/UI:N/VC:N/VI:L/VA:N/SC:N/SI:N/SA:N)

{
    "vector": "CVSS:4.0/AV:A/AC:H/AT:N/PR:N/UI:N/VC:N/VI:L/VA:N/SC:N/SI:N/SA:N",
    "score": 2.3,
    "severity": "low",
}
@pandatix
Copy link
Contributor

May I suggest another structure for the API call : /api/v1/compute?vector=<cvss_v4_vector_here>
This enable versionning API (good practice) and is more HTML5/REST-full compliant so will enable better software integration.

@skontar
Copy link
Collaborator Author

skontar commented Apr 17, 2024

Sure thing. This makes perfect sense.

@nickleali
Copy link

Appreciate you looking into this!

Would it be useful to have the nomenclature of the JSON match with the CVSS v4.0 schema for consistency?
https://www.first.org/cvss/cvss-v4.0.json

{
"vectorString": "CVSS:4.0/AV:A/AC:H/AT:N/PR:N/UI:N/VC:N/VI:L/VA:N/SC:N/SI:N/SA:N",
"baseScore": 2.3,
"baseSeverity": "low",
}

And potentially adapt the score and severity based on Threat or Environmental metrics?

@n3rada
Copy link
Contributor

n3rada commented Jul 27, 2024

Hi @nickleali, if my PR #59 is accepted, It will certainly facilitate the implementation of an API. For example, it could be done with this kind of server.js express file:

const express = require('express');
const app = express();
const port = 3000;

const { CVSS40, Vector } = require('./cvss40');

app.get('/api/v1/compute', (req, res) => {
  const vectorString = req.query.vector;

  if (!vectorString) {
    return res.status(400).json({ error: 'Vector parameter is required' });
  }

  try {
    // Create a new Vector instance with the provided string
    const vector = new Vector(vectorString);

    // Pass the Vector instance to the CVSS40 constructor
    const cvssInstance = new CVSS40(vector);

    // Respond with the CVSS instance (including the computed score and severity)
    res.json({
      score: cvssInstance.score,
      severity: cvssInstance.severity,
      equivalentClasses: vector.equivalentClasses,
      raw: vector.raw,
    });
  } catch (error) {
    console.error('Error processing CVSS vector:', error);
    res.status(400).json({ error: 'Invalid CVSS vector', details: error.message });
  }
});

app.listen(port, () => {
  console.log(`CVSS API listening at http://localhost:${port}`);
});

Then, you can request:

http://localhost:3000/api/v1/compute?vector=CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:L/VA:N/SC:N/SI:N/SA:H/E:A

In order to retrieve the json:

{
   "score":9.2,
   "severity":"Critical",
   "equivalentClasses":"001100",
   "raw":"CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:L/VA:N/SC:N/SI:N/SA:H/E:A"
}

This is just an example, and the implementation needs to be discussed with the people in charge, as I don't know what they prefer for deploying the site. Of course, you'd have to add flow limitation, entry verification, etc.

@nickleali
Copy link

I'm certainly not someone who should be doing code review, but something like this would work great as an API to the calculator.

I will have to check about how this is deployed on the FIRST infrastructure for a hosted version.

@n3rada
Copy link
Contributor

n3rada commented Aug 6, 2024

I hope someone who can will read it tho' 😊

@skontar
Copy link
Collaborator Author

skontar commented Aug 7, 2024

@n3rada I am sorry I missed the PR. I will ask my colleague who is much better at Javascript to have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants