-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR suggestion: move isSafe check as a functional-contsraint
- Loading branch information
Showing
6 changed files
with
72 additions
and
35 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
64 changes: 64 additions & 0 deletions
64
packages/schema/lib/functional-constraints/AuthFieldisSafe.js
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,64 @@ | ||
'use strict'; | ||
|
||
const jsonschema = require('jsonschema'); | ||
|
||
const AUTH_FIELD_ID = '/AuthFieldSchema'; | ||
const AUTH_FIELDS_ID = '/AuthFieldsSchema'; | ||
|
||
const FORBIDDEN_KEYS = [ | ||
'access_token', | ||
'refresh_token', | ||
'api_key', | ||
'password', | ||
'secret', | ||
]; | ||
|
||
const checkAuthField = (field) => { | ||
const errors = []; | ||
|
||
if (FORBIDDEN_KEYS.includes(field.key) && field.isSafe === true) { | ||
errors.push( | ||
new jsonschema.ValidationError( | ||
`Cannot set isSafe=true for the sensitive key "${field.key}".`, | ||
field, | ||
'/AuthFieldSchema', | ||
'instance.key', | ||
'sensitive', | ||
'key', | ||
), | ||
); | ||
} | ||
|
||
return errors; | ||
}; | ||
|
||
module.exports = (definition, mainSchema) => { | ||
const errors = []; | ||
// Done to validate anti-examples declaratively defined in the schema | ||
if ([AUTH_FIELD_ID, AUTH_FIELDS_ID].includes(mainSchema.id)) { | ||
const definitions = Array.isArray(definition) ? definition : [definition]; | ||
|
||
definitions.forEach((field, index) => { | ||
checkAuthField(field).forEach((err) => { | ||
err.property = `instance[${index}]`; | ||
err.stack = err.stack.replace('instance.key', `instance[${index}].key`); | ||
errors.push(err); | ||
}); | ||
}); | ||
} | ||
|
||
// If there's no authentication or no fields, we have nothing to check | ||
if (!definition.authentication || !definition.authentication.fields) { | ||
return errors; | ||
} | ||
|
||
definition.authentication.fields.forEach((field, index) => { | ||
checkAuthField(field).forEach((err) => { | ||
err.property = `instance[${index}]`; | ||
err.stack = err.stack.replace('instance.key', `instance[${index}].key`); | ||
errors.push(err); | ||
}); | ||
}); | ||
|
||
return errors; | ||
}; |
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
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