diff --git a/api_deploy/converters.py b/api_deploy/converters.py index eb04fab..bedfc53 100644 --- a/api_deploy/converters.py +++ b/api_deploy/converters.py @@ -8,6 +8,7 @@ from api_deploy.config import Config from api_deploy.processors.abstract_processor import AbstractProcessor from api_deploy.processors.code_generator import CodeGenerator +from api_deploy.processors.security_headers_processor import SecurityHeadersProcessor from api_deploy.processors.strict_processor import StrictProcessor from api_deploy.schema import Schema, YamlDict @@ -27,6 +28,7 @@ def default(cls, config: Config): default_manager.register(ApiGatewayProcessor(config, **config['gateway'])) default_manager.register(CorsProcessor(config, headers=config['headers'], **config['cors'])) default_manager.register(CodeGenerator(config, **config['generator'])) + default_manager.register(SecurityHeadersProcessor(config)) return default_manager def register(self, processor: AbstractProcessor): diff --git a/api_deploy/processors/security_headers_processor.py b/api_deploy/processors/security_headers_processor.py new file mode 100644 index 0000000..a84dc43 --- /dev/null +++ b/api_deploy/processors/security_headers_processor.py @@ -0,0 +1,40 @@ +from api_deploy.config import Config +from api_deploy.processors.abstract_processor import AbstractProcessor +from api_deploy.schema import Schema + + +class SecurityHeadersProcessor(AbstractProcessor): + """Adds HSTS and nosniff to every backend response. API Gateway only returns headers mapped per response, + so the ones the load balancer sets never reach clients.""" + + security_headers = { + 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', + 'X-Content-Type-Options': 'nosniff', + } + + def __init__(self, config: Config, **kwargs) -> None: + pass + + def process(self, schema: Schema) -> Schema: + for path in schema['paths']: + for method in schema['paths'][path]: + endpoint = schema['paths'][path][method] + integration = endpoint.get('x-amazon-apigateway-integration', {}) + # CORS preflight mocks answer from API Gateway itself and never reach the backend. + if integration.get('type') != 'http': + continue + + method_responses = {str(status_code): response for status_code, response in endpoint['responses'].items()} + for integration_response in integration['responses'].values(): + method_response = method_responses.get(str(integration_response['statusCode'])) + # Without a declared method response for this status code there is nothing to attach the header to. + if method_response is None: + continue + + method_response_headers = method_response.setdefault('headers', {}) + response_parameters = integration_response.setdefault('responseParameters', {}) + for header_name, header_value in self.security_headers.items(): + method_response_headers[header_name] = {'schema': {'type': 'string'}} + response_parameters[f'method.response.header.{header_name}'] = f'\'{header_value}\'' + + return schema diff --git a/tests/openapi/complex_target.yml b/tests/openapi/complex_target.yml index cfb368e..5cefb62 100644 --- a/tests/openapi/complex_target.yml +++ b/tests/openapi/complex_target.yml @@ -34,6 +34,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': description: Internal Server Error content: @@ -61,6 +67,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -104,10 +116,14 @@ paths: statusCode: '500' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '200': statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -171,6 +187,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': description: Internal Server Error content: @@ -198,6 +220,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -241,10 +269,14 @@ paths: statusCode: '500' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '200': statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -364,6 +396,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '400': description: Bad Request content: @@ -390,6 +428,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '404': description: Not Found content: @@ -419,6 +463,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': description: Internal Server Error content: @@ -446,6 +496,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string x-amazon-apigateway-integration: type: http connectionId: 1234567890 @@ -458,18 +514,26 @@ paths: statusCode: '500' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '200': statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '400': statusCode: '400' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '404': statusCode: '404' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -592,6 +656,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '400': description: Bad Request content: @@ -618,6 +688,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '404': description: Not Found content: @@ -647,6 +723,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': description: Internal Server Error content: @@ -674,6 +756,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string x-amazon-apigateway-integration: type: http connectionId: 1234567890 @@ -686,18 +774,26 @@ paths: statusCode: '500' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '200': statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '400': statusCode: '400' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '404': statusCode: '404' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -818,6 +914,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '400': description: Bad Request content: @@ -844,6 +946,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '404': description: Not Found content: @@ -873,6 +981,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': description: Internal Server Error content: @@ -900,6 +1014,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string x-amazon-apigateway-integration: type: http connectionId: 1234567890 @@ -912,18 +1032,26 @@ paths: statusCode: '500' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '200': statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '400': statusCode: '400' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '404': statusCode: '404' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -1004,6 +1132,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -1053,6 +1187,8 @@ paths: method.response.header.Content-Type: integration.response.header.Content-Type method.response.header.Access-Control-Allow-Origin: '''foo.com''' method.response.header.Access-Control-Expose-Headers: '''Content-Type''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -1523,8 +1659,7 @@ components: comment: type: string nullable: true - example: Additional user generated info about - the transition + example: Additional user generated info about the transition _meta: type: object readOnly: true diff --git a/tests/openapi/dedup_target.yml b/tests/openapi/dedup_target.yml index 98acd54..cb215b5 100644 --- a/tests/openapi/dedup_target.yml +++ b/tests/openapi/dedup_target.yml @@ -36,6 +36,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '401': description: Unauthorized content: @@ -47,6 +53,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '403': description: Forbidden content: @@ -58,6 +70,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': description: Internal Server Error content: @@ -69,6 +87,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -112,18 +136,26 @@ paths: statusCode: '500' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '200': statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '401': statusCode: '401' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '403': statusCode: '403' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -182,6 +214,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': description: Internal Server Error content: @@ -193,6 +231,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -236,10 +280,14 @@ paths: statusCode: '500' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '200': statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -301,6 +349,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -348,6 +402,8 @@ paths: statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -409,6 +465,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -456,6 +518,8 @@ paths: statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -518,6 +582,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -567,6 +637,8 @@ paths: method.response.header.Content-Type: integration.response.header.Content-Type method.response.header.Access-Control-Allow-Origin: '''foo.com''' method.response.header.Access-Control-Expose-Headers: '''Content-Type''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp diff --git a/tests/openapi/external_ref_target.yml b/tests/openapi/external_ref_target.yml index 3d8a078..21c53ca 100644 --- a/tests/openapi/external_ref_target.yml +++ b/tests/openapi/external_ref_target.yml @@ -69,6 +69,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '401': description: Unauthorized content: @@ -130,6 +136,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '403': description: Forbidden content: @@ -191,6 +203,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': description: Internal Server Error content: @@ -252,6 +270,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -295,18 +319,26 @@ paths: statusCode: '500' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '200': statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '401': statusCode: '401' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '403': statusCode: '403' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -365,6 +397,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': description: Internal Server Error content: @@ -426,6 +464,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -469,10 +513,14 @@ paths: statusCode: '500' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '200': statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -534,6 +582,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -581,6 +635,8 @@ paths: statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -642,6 +698,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -689,6 +751,8 @@ paths: statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -752,6 +816,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -801,6 +871,8 @@ paths: method.response.header.Content-Type: integration.response.header.Content-Type method.response.header.Access-Control-Allow-Origin: '''foo.com''' method.response.header.Access-Control-Expose-Headers: '''Content-Type''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp diff --git a/tests/openapi/one_of_target.yml b/tests/openapi/one_of_target.yml index 0f9a9b2..6b43fd5 100644 --- a/tests/openapi/one_of_target.yml +++ b/tests/openapi/one_of_target.yml @@ -47,6 +47,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '400': description: Bad Request content: @@ -108,6 +114,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '404': description: Not Found content: @@ -169,6 +181,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': description: Internal Server Error content: @@ -230,6 +248,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string x-amazon-apigateway-request-validator: all parameters: - name: host @@ -274,18 +298,26 @@ paths: statusCode: '500' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '200': statusCode: '200' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '400': statusCode: '400' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" '404': statusCode: '404' responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp @@ -349,6 +381,12 @@ paths: schema: type: string example: foo.com + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string parameters: - name: host in: header @@ -398,6 +436,8 @@ paths: method.response.header.Content-Type: integration.response.header.Content-Type method.response.header.Access-Control-Allow-Origin: '''foo.com''' method.response.header.Access-Control-Expose-Headers: '''Content-Type''' + method.response.header.Strict-Transport-Security: "'max-age=31536000; includeSubDomains'" + method.response.header.X-Content-Type-Options: "'nosniff'" requestParameters: integration.request.header.User-Agent: context.identity.userAgent integration.request.header.X-Forwarded-For: context.identity.sourceIp diff --git a/tests/openapi/simple_target.yml b/tests/openapi/simple_target.yml index 4d4c1a1..c0087f6 100644 --- a/tests/openapi/simple_target.yml +++ b/tests/openapi/simple_target.yml @@ -252,6 +252,12 @@ paths: schema: example: x-ping type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string x-ping: schema: type: string @@ -266,6 +272,12 @@ paths: schema: example: foo.com type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '404': content: application/json: @@ -277,6 +289,12 @@ paths: schema: example: foo.com type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': content: application/json: @@ -288,6 +306,12 @@ paths: schema: example: foo.com type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string tags: - Health x-amazon-apigateway-integration: @@ -310,19 +334,31 @@ paths: responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' method.response.header.Access-Control-Expose-Headers: '''x-ping''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' method.response.header.x-ping: integration.response.header.x-ping statusCode: '200' '400': responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' statusCode: '400' '404': responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' statusCode: '404' default: responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' statusCode: '500' type: http uri: https://integration.com/-/ping @@ -415,6 +451,12 @@ paths: schema: example: x-ping type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string x-ping: schema: type: string @@ -429,6 +471,12 @@ paths: schema: example: foo.com type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '404': content: application/json: @@ -440,6 +488,12 @@ paths: schema: example: foo.com type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': content: application/json: @@ -451,6 +505,12 @@ paths: schema: example: foo.com type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string tags: - Health x-amazon-apigateway-integration: @@ -473,19 +533,31 @@ paths: responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' method.response.header.Access-Control-Expose-Headers: '''x-ping''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' method.response.header.x-ping: integration.response.header.x-ping statusCode: '200' '400': responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' statusCode: '400' '404': responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' statusCode: '404' default: responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' statusCode: '500' type: http uri: https://integration.com/-/ping @@ -546,6 +618,12 @@ paths: schema: example: x-ping type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string x-ping: schema: type: string @@ -560,6 +638,12 @@ paths: schema: example: foo.com type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '404': content: application/json: @@ -571,6 +655,12 @@ paths: schema: example: foo.com type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string '500': content: application/json: @@ -582,6 +672,12 @@ paths: schema: example: foo.com type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string tags: - Health x-amazon-apigateway-integration: @@ -604,19 +700,31 @@ paths: responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' method.response.header.Access-Control-Expose-Headers: '''x-ping''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' method.response.header.x-ping: integration.response.header.x-ping statusCode: '200' '400': responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' statusCode: '400' '404': responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' statusCode: '404' default: responseParameters: method.response.header.Access-Control-Allow-Origin: '''foo.com''' + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' statusCode: '500' type: http uri: https://integration.com/-/ping @@ -670,6 +778,12 @@ paths: schema: example: text/yaml type: string + Strict-Transport-Security: + schema: + type: string + X-Content-Type-Options: + schema: + type: string tags: - Static Files x-amazon-apigateway-integration: @@ -693,6 +807,9 @@ paths: method.response.header.Access-Control-Allow-Origin: '''foo.com''' method.response.header.Access-Control-Expose-Headers: '''Content-Type''' method.response.header.Content-Type: integration.response.header.Content-Type + method.response.header.Strict-Transport-Security: '''max-age=31536000; + includeSubDomains''' + method.response.header.X-Content-Type-Options: '''nosniff''' statusCode: '200' default: responseParameters: diff --git a/tests/unit/test_security_headers.py b/tests/unit/test_security_headers.py new file mode 100644 index 0000000..da7322e --- /dev/null +++ b/tests/unit/test_security_headers.py @@ -0,0 +1,81 @@ +from api_deploy.config import Config, ConfigFile +from api_deploy.processors.security_headers_processor import SecurityHeadersProcessor +from api_deploy.schema import Schema + + +def build_source(): + return Schema(''' +openapi: 3.0.1 +info: + title: Test + version: '1' +paths: + /foo: + get: + responses: + '200': + description: OK + '404': + description: Not found + x-amazon-apigateway-integration: + type: http + responses: + default: + statusCode: '500' + '200': + statusCode: '200' + responseParameters: + method.response.header.Access-Control-Allow-Origin: "'foo.com'" + '404': + statusCode: '404' + options: + responses: + '200': + description: '200' + x-amazon-apigateway-integration: + type: mock + responses: + default: + statusCode: '200' +''') + + +def process(source): + config = Config(ConfigFile(''), 'test') + return SecurityHeadersProcessor(config).process(source) + + +def test_every_backend_response_returns_hsts_and_nosniff(): + get_endpoint = process(build_source())['paths']['/foo']['get'] + + for status_code in ('200', '404'): + assert get_endpoint['responses'][status_code]['headers'].keys() == { + 'Strict-Transport-Security', + 'X-Content-Type-Options', + } + integration_response = get_endpoint['x-amazon-apigateway-integration']['responses'][status_code] + response_parameters = integration_response['responseParameters'] + hsts_mapping = response_parameters['method.response.header.Strict-Transport-Security'] + assert hsts_mapping == "'max-age=31536000; includeSubDomains'" + assert response_parameters['method.response.header.X-Content-Type-Options'] == "'nosniff'" + + +def test_existing_header_mappings_are_kept(): + get_endpoint = process(build_source())['paths']['/foo']['get'] + + response_parameters = get_endpoint['x-amazon-apigateway-integration']['responses']['200']['responseParameters'] + assert response_parameters['method.response.header.Access-Control-Allow-Origin'] == "'foo.com'" + + +def test_status_codes_the_method_does_not_declare_are_skipped(): + get_endpoint = process(build_source())['paths']['/foo']['get'] + + assert 'responseParameters' not in get_endpoint['x-amazon-apigateway-integration']['responses']['default'] + assert '500' not in get_endpoint['responses'] + + +def test_cors_preflight_mocks_are_left_alone(): + options_endpoint = process(build_source())['paths']['/foo']['options'] + + assert 'headers' not in options_endpoint['responses']['200'] + assert 'responseParameters' not in options_endpoint['x-amazon-apigateway-integration']['responses']['default']