-
Notifications
You must be signed in to change notification settings - Fork 56
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
fix: AWS API Gateway OpenAPI validators trigger AwsSolutions-APIG2 #1312
Changes from all commits
258e04b
7acd6c3
bae0eaf
7987952
d5dc875
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ | |
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import { readFileSync } from 'fs'; | ||
import { parse } from 'path'; | ||
import { CfnResource, Stack } from 'aws-cdk-lib'; | ||
import { CfnResource, Stack, Stage } from 'aws-cdk-lib'; | ||
import { CfnRequestValidator, CfnRestApi } from 'aws-cdk-lib/aws-apigateway'; | ||
import { parse as yamlparse } from 'yaml'; | ||
import { NagRuleCompliance, NagRules } from '../../nag-rules'; | ||
|
||
/** | ||
|
@@ -27,6 +29,35 @@ export default Object.defineProperty( | |
} | ||
} | ||
} | ||
if (node.bodyS3Location) { | ||
const assetOutdir = NagRules.resolveResourceFromInstrinsic( | ||
node, | ||
Stage.of(node)?.assetOutdir | ||
); | ||
const assetPath = NagRules.resolveResourceFromInstrinsic( | ||
node, | ||
node.getMetadata('aws:asset:path') | ||
); | ||
const specFile = yamlparse( | ||
readFileSync(assetOutdir + '/' + assetPath, 'utf-8') | ||
); | ||
Comment on lines
+41
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use the official OpenApi Library/Types for processing the spec |
||
if ('x-amazon-apigateway-request-validators' in specFile) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docs mentions that this can be done on a per method basis as well instead of just globally. Each method can also override the global config, that needs to be checked as well |
||
for (const prop in specFile[ | ||
'x-amazon-apigateway-request-validators' | ||
]) { | ||
if ( | ||
specFile['x-amazon-apigateway-request-validators'][prop] | ||
.validateRequestBody && | ||
specFile['x-amazon-apigateway-request-validators'][prop] | ||
.validateRequestParameters | ||
) { | ||
found = true; | ||
} else { | ||
found = false; | ||
} | ||
} | ||
} | ||
} | ||
if (!found) { | ||
return NagRuleCompliance.NON_COMPLIANT; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"title": "ReqValidators Sample", | ||
"version": "1.0.0" | ||
}, | ||
"schemes": [ | ||
"https" | ||
], | ||
"basePath": "/v1", | ||
"produces": [ | ||
"application/json" | ||
], | ||
"x-amazon-apigateway-request-validators" : { | ||
"all" : { | ||
"validateRequestBody" : true, | ||
"validateRequestParameters" : true | ||
}, | ||
"params-only" : { | ||
"validateRequestBody" : true, | ||
"validateRequestParameters" : true | ||
} | ||
}, | ||
"x-amazon-apigateway-request-validator" : "params-only", | ||
"paths": { | ||
"/validation": { | ||
"post": { | ||
"x-amazon-apigateway-request-validator" : "all", | ||
"parameters": [ | ||
{ | ||
"in": "header", | ||
"name": "h1", | ||
"required": true | ||
}, | ||
{ | ||
"in": "body", | ||
"name": "RequestBodyModel", | ||
"required": true, | ||
"schema": { | ||
"$ref": "#/definitions/RequestBodyModel" | ||
} | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"schema": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/Error" | ||
} | ||
}, | ||
"headers" : { | ||
"test-method-response-header" : { | ||
"type" : "string" | ||
} | ||
} | ||
} | ||
}, | ||
"security" : [{ | ||
"api_key" : [] | ||
}], | ||
"x-amazon-apigateway-auth" : { | ||
"type" : "none" | ||
}, | ||
"x-amazon-apigateway-integration" : { | ||
"type" : "http", | ||
"uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", | ||
"httpMethod" : "POST", | ||
"requestParameters": { | ||
"integration.request.header.custom_h1": "method.request.header.h1" | ||
}, | ||
"responses" : { | ||
"2\\d{2}" : { | ||
"statusCode" : "200" | ||
}, | ||
"default" : { | ||
"statusCode" : "400", | ||
"responseParameters" : { | ||
"method.response.header.test-method-response-header" : "'static value'" | ||
}, | ||
"responseTemplates" : { | ||
"application/json" : "json 400 response template", | ||
"application/xml" : "xml 400 response template" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"get": { | ||
"parameters": [ | ||
{ | ||
"name": "q1", | ||
"in": "query", | ||
"required": true | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"schema": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/Error" | ||
} | ||
}, | ||
"headers" : { | ||
"test-method-response-header" : { | ||
"type" : "string" | ||
} | ||
} | ||
} | ||
}, | ||
"security" : [{ | ||
"api_key" : [] | ||
}], | ||
"x-amazon-apigateway-auth" : { | ||
"type" : "none" | ||
}, | ||
"x-amazon-apigateway-integration" : { | ||
"type" : "http", | ||
"uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", | ||
"httpMethod" : "GET", | ||
"requestParameters": { | ||
"integration.request.querystring.type": "method.request.querystring.q1" | ||
}, | ||
"responses" : { | ||
"2\\d{2}" : { | ||
"statusCode" : "200" | ||
}, | ||
"default" : { | ||
"statusCode" : "400", | ||
"responseParameters" : { | ||
"method.response.header.test-method-response-header" : "'static value'" | ||
}, | ||
"responseTemplates" : { | ||
"application/json" : "json 400 response template", | ||
"application/xml" : "xml 400 response template" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"RequestBodyModel": { | ||
"type": "object", | ||
"properties": { | ||
"id": { "type": "integer" }, | ||
"type": { "type": "string", "enum": ["dog", "cat", "fish"] }, | ||
"name": { "type": "string" }, | ||
"price": { "type": "number", "minimum": 25, "maximum": 500 } | ||
}, | ||
"required": ["type", "name", "price"] | ||
}, | ||
"Error": { | ||
"type": "object", | ||
"properties": { | ||
|
||
} | ||
} | ||
} | ||
} |
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.
This doesn't capture the case where a user uses the L1
CfnRestApi
directly and uses the Body property