Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api_deploy/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down
40 changes: 40 additions & 0 deletions api_deploy/processors/security_headers_processor.py
Original file line number Diff line number Diff line change
@@ -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
Loading