Skip to content

Commit 866bc0d

Browse files
feat: impl MVP of new authz API
This adds a new API for authorization, defined in `src/middleware/authz.js`, which is centered around two key functions: `authz` and `authzLevel`. Each returns a middleware function which applies the requested authorization checks. For `authz`, if the authorization checks fail, then the request fails. For `authzLevel`, if the authorization checks fail, then the request continues but without an authorization level being set on the request context. In addition to these top-level APIs, this introduces a set of pre-defined checks, plus two check combinators, which collectively will enable CVE Services endpoints to define the authorization checks they require, all in one place. This is intended to replace the combination of existing authorization middleware functions and ad-hoc authorization checks performed throughout a number of endpoints. This commit *does not* include any replacement of existing authorization checks, only the introduction of the new API. We also shim the method Set.prototype.intersection. The Set.prototype.intersection method was added to the Set type in Node.js version 22. Currently, CVE Services uses an older version of Node and so we need this shim to ensure the API runs. We are planning to upgrade to Node 24 soon, in which case this shim will become unecessary. This also includes initial, bare-bones tests for the new authz API. As we continue to work to integrate the new API into more endpoints, we'll expand the testing here to be more thorough. Mocha doesn't isolate tests in their own process, which means when the tests are running they're actually all sharing a singleton instance of the Express app. This is a problem for the authz testing specifically, because it modifies a piece of global state (`useNewAuthzApi`) to select at runtime whether to use the old or new versions of the authorization API. To deal with this, this commit also ensures that authz tests are isolated in their own, separate run of Mocha. Signed-off-by: Andrew Lilley Brinker <abrinker@mitre.org>
1 parent b6e184d commit 866bc0d

File tree

8 files changed

+1386
-181
lines changed

8 files changed

+1386
-181
lines changed

package-lock.json

Lines changed: 225 additions & 113 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"prompt-sync": "^4.2.0",
5555
"replace-in-file": "6.3.5",
5656
"replace-json-property": "^1.8.0",
57+
"set.prototype.intersection": "^1.1.8",
5758
"swagger-autogen": "^2.19.0",
5859
"swagger-ui-express": "^4.3.0",
5960
"uuid": "^8.3.2",
@@ -99,8 +100,8 @@
99100
"start:prd": "node src/swagger.js && NODE_ENV=production node src/scripts/updateOpenapiHost.js && NODE_ENV=production node src/index.js",
100101
"swagger-autogen": "node src/swagger.js",
101102
"test": "NODE_ENV=test mocha --recursive --exit || true",
102-
"test:integration": "NODE_ENV=test node-dev src/scripts/populate.mjs y; NODE_ENV=test MONGO_CONN_STRING=mongodb://docdb:27017 MONGO_DB_NAME=cve_test node-dev src/scripts/migrate.js; NODE_ENV=test mocha test/integration-tests --recursive --exit",
103-
"test:integration-local": "NODE_ENV=test node-dev src/scripts/populate.mjs y; NODE_ENV=test MONGO_CONN_STRING=mongodb://localhost:27017 MONGO_DB_NAME=cve_test node-dev src/scripts/migrate.js; NODE_ENV=test mocha test/integration-tests --recursive --exit",
103+
"test:integration": "NODE_ENV=test node-dev src/scripts/populate.mjs y; NODE_ENV=test MONGO_CONN_STRING=mongodb://docdb:27017 MONGO_DB_NAME=cve_test node-dev src/scripts/migrate.js; NODE_ENV=test mocha test/integration-tests --exclude test/integration-tests/cve/authzValidation.js --recursive --exit && NODE_ENV=test mocha test/integration-tests/cve/authzValidation.js --recursive --exit",
104+
"test:integration-local": "NODE_ENV=test node-dev src/scripts/populate.mjs y; NODE_ENV=test MONGO_CONN_STRING=mongodb://localhost:27017 MONGO_DB_NAME=cve_test node-dev src/scripts/migrate.js; NODE_ENV=test mocha test/integration-tests --exclude test/integration-tests/cve/authzValidation.js --recursive --exit && NODE_ENV=test mocha test/integration-tests/cve/authzValidation.js --recursive --exit",
104105
"test:unit-tests": "NODE_ENV=test mocha test/unit-tests --recursive --exit || true",
105106
"test:coverage": "NODE_ENV=test nyc --reporter=text mocha src/* --recursive --exit || true",
106107
"test:coverage-html": "NODE_ENV=test nyc --reporter=html mocha src/* --recursive --exit || true",

src/controller/cve.controller/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@ const mw = require('../../middleware/middleware')
88
const errorMsgs = require('../../middleware/errorMessages')
99
const controller = require('./cve.controller')
1010
const { body, param, query } = require('express-validator')
11-
const { parseGetParams, parsePostParams, parseError, validateCveCnaContainerJsonSchema, validateCveAdpContainerJsonSchema, validateRejectBody, validateUniqueEnglishEntry, validateDescription, validateDatePublic, validateTimelineDates, validatePURL } = require('./cve.middleware')
11+
const {
12+
parseGetParams,
13+
parsePostParams,
14+
parseError,
15+
validateCveCnaContainerJsonSchema,
16+
validateCveAdpContainerJsonSchema,
17+
validateRejectBody,
18+
validateUniqueEnglishEntry,
19+
validateDescription,
20+
validateDatePublic,
21+
validateTimelineDates,
22+
validatePURL
23+
} = require('./cve.middleware')
24+
const { authz, orgHasRole, OrgRoles } = require('../../middleware/authz')
1225
const getConstants = require('../../constants').getConstants
1326
const CONSTANTS = getConstants()
1427
const CHOICES = [CONSTANTS.CVE_STATES.REJECTED, CONSTANTS.CVE_STATES.PUBLISHED]
@@ -584,6 +597,10 @@ router.put('/cve/:id',
584597
}
585598
*/
586599
mw.validateUser,
600+
mw.authzApiSelector({
601+
oldAuthz: mw.onlySecretariat,
602+
newAuthz: authz(orgHasRole(OrgRoles.SECRETARIAT))
603+
}),
587604
mw.onlySecretariat,
588605
mw.trimJSONWhitespace,
589606
mw.validateCveJsonSchema,

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ app.use((req, res, next) => {
4646
res.status(404).json(error.notFound())
4747
})
4848

49+
// TODO(alilleybrinker): Remove this when done testing the new Authz API.
50+
app.set('useNewAuthzApi', false)
51+
4952
// Connect to MongoDB database
5053
const dbConnectionStr = dbUtils.getMongoConnectionString()
5154
const dbConnectionOptions = dbUtils.getMongoConnectionOptions()

0 commit comments

Comments
 (0)