-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-304964: IPA-114: Errors (check for 401 and 403 codes for authorized endpoints) #639
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
tools/spectral/ipa/__tests__/IPA114AuthenticatedEndpointsHaveAuthErrors.test.js
This file contains hidden or 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,161 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-114-authenticated-endpoints-have-auth-errors', [ | ||
| { | ||
| name: 'valid authenticated endpoint with 401 and 403 responses', | ||
| document: { | ||
| paths: { | ||
| '/resources': { | ||
| get: { | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| 401: { description: 'Unauthorized' }, | ||
| 403: { description: 'Forbidden' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid authenticated endpoint missing both auth error responses', | ||
| document: { | ||
| paths: { | ||
| '/resources': { | ||
| get: { | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-114-authenticated-endpoints-have-auth-errors', | ||
| message: 'Authenticated endpoint must define a 401 response.', | ||
| path: ['paths', '/resources', 'get'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-114-authenticated-endpoints-have-auth-errors', | ||
| message: 'Authenticated endpoint must define a 403 response.', | ||
| path: ['paths', '/resources', 'get'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid authenticated endpoint missing 401 response', | ||
| document: { | ||
| paths: { | ||
| '/resources': { | ||
| get: { | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| 403: { description: 'Forbidden' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-114-authenticated-endpoints-have-auth-errors', | ||
| message: 'Authenticated endpoint must define a 401 response.', | ||
| path: ['paths', '/resources', 'get'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid authenticated endpoint missing 403 response', | ||
| document: { | ||
| paths: { | ||
| '/resources': { | ||
| get: { | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| 401: { description: 'Unauthorized' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-114-authenticated-endpoints-have-auth-errors', | ||
| message: 'Authenticated endpoint must define a 403 response.', | ||
| path: ['paths', '/resources', 'get'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'unauthenticated endpoint with empty security array', | ||
| document: { | ||
| paths: { | ||
| '/resources': { | ||
| get: { | ||
| security: [], | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'unauthenticated endpoint with /unauth in path', | ||
| document: { | ||
| paths: { | ||
| '/unauth/resources': { | ||
| get: { | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'unauthenticated endpoint with both /unauth and empty security', | ||
| document: { | ||
| paths: { | ||
| '/unauth/resources': { | ||
| get: { | ||
| security: [], | ||
| responses: { | ||
| 200: { description: 'Success' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'edge case with no responses object', | ||
| document: { | ||
| paths: { | ||
| '/resources': { | ||
| get: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-114-authenticated-endpoints-have-auth-errors', | ||
| message: 'Authenticated endpoint must define a 401 and 403 responses.', | ||
| path: ['paths', '/resources', 'get'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); |
This file contains hidden or 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 hidden or 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
82 changes: 82 additions & 0 deletions
82
tools/spectral/ipa/rulesets/functions/IPA114AuthenticatedEndpointsHaveAuthErrors.js
This file contains hidden or 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,82 @@ | ||
| import { hasException } from './utils/exceptions.js'; | ||
| import { | ||
| collectAdoption, | ||
| collectAndReturnViolation, | ||
| collectException, | ||
| handleInternalError, | ||
| } from './utils/collectionUtils.js'; | ||
|
|
||
| const RULE_NAME = 'xgen-IPA-114-authenticated-endpoints-have-auth-errors'; | ||
|
|
||
| /** | ||
| * Validates that authenticated endpoints have 401 and 403 responses defined | ||
| * | ||
| * Endpoints are considered authenticated unless: | ||
| * 1. They have explicit "security: []" set | ||
| * 2. They contain "/unauth" in the path | ||
| * | ||
| * @param {object} input - The operation object to check | ||
| * @param {object} _ - Rule options (unused) | ||
| * @param {object} context - The context object containing path and document information | ||
| */ | ||
| export default (input, _, { path }) => { | ||
| // Path components: [paths, pathName, methodName, ...] | ||
| const pathName = path[1]; | ||
|
|
||
| // Skip validation if the path contains 'unauth' | ||
| if (pathName.includes('/unauth/')) { | ||
| return; | ||
| } | ||
|
|
||
| // Skip validation if security is explicitly set to empty array | ||
| if (Array.isArray(input.security) && input.security.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| // Check for exception at operation level | ||
| if (hasException(input, RULE_NAME)) { | ||
| collectException(input, RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| const errors = checkViolationsAndReturnErrors(input.responses, path); | ||
| if (errors.length > 0) { | ||
| return collectAndReturnViolation(path, RULE_NAME, errors); | ||
| } | ||
|
|
||
| collectAdoption(path, RULE_NAME); | ||
| }; | ||
|
|
||
| function checkViolationsAndReturnErrors(responses, path) { | ||
| try { | ||
| const errors = []; | ||
|
|
||
| if (!responses) { | ||
| return [ | ||
| { | ||
| path, | ||
| message: `Authenticated endpoint must define a 401 and 403 responses.`, | ||
| }, | ||
| ]; | ||
| } | ||
| // Check for 401 Unauthorized response | ||
| if (!responses['401']) { | ||
| errors.push({ | ||
| path, | ||
| message: `Authenticated endpoint must define a 401 response.`, | ||
| }); | ||
| } | ||
|
|
||
| // Check for 403 Forbidden response | ||
| if (!responses['403']) { | ||
| errors.push({ | ||
| path, | ||
| message: `Authenticated endpoint must define a 403 response.`, | ||
| }); | ||
| } | ||
|
|
||
| return errors; | ||
| } catch (e) { | ||
| handleInternalError(RULE_NAME, path, e); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to add the methods explicitly here (similarly to a few of the IPA 117 rules), otherwise it will validate extensions as well. I can see in the results:
For:
paths./api/atlas/v2/alertConfigs/matchers/fieldNames.x-xgen-IPA-exceptionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, addressed 👍 507 violations 🥲
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:hidethepainharold: