Skip to content

Security Guide

The canonical security policy is maintained in the root SECURITY.md.

Protecting documentation routes

OpenAPI JSON/YAML and Swagger UI are ordinary HTTP-triggered functions. A route registered with auth_level=func.AuthLevel.ANONYMOUS is public after deployment unless another platform layer restricts it. The specification reveals endpoint shapes, request and response schemas, and security scheme names, so anonymous documentation is a convenient local-development choice but should be deliberate in production.

OpenAPI security declarations and security_schemes describe how clients authenticate to documented API operations. They do not protect the openapi.json, openapi.yaml, or docs routes that serve the specification and UI. Protect those HTTP routes separately with one of these patterns.

1. Omit documentation routes in production

Azure Functions registers decorators when function_app.py is imported. Put the route definitions behind an app setting so no documentation functions are registered unless the setting is explicitly enabled:

import os

import azure.functions as func

from azure_functions_openapi import get_openapi_json, get_openapi_yaml, render_swagger_ui

app = func.FunctionApp()

if os.getenv("ENABLE_API_DOCS") == "true":

    @app.route(route="openapi.json", methods=["GET"], auth_level=func.AuthLevel.ANONYMOUS)
    def openapi_json(req: func.HttpRequest) -> func.HttpResponse:
        return func.HttpResponse(
            get_openapi_json(title="My API", app=app),
            mimetype="application/json",
        )

    @app.route(route="openapi.yaml", methods=["GET"], auth_level=func.AuthLevel.ANONYMOUS)
    def openapi_yaml(req: func.HttpRequest) -> func.HttpResponse:
        return func.HttpResponse(
            get_openapi_yaml(title="My API", app=app),
            mimetype="application/x-yaml",
        )

    @app.route(route="docs", methods=["GET"], auth_level=func.AuthLevel.ANONYMOUS)
    def swagger_ui(req: func.HttpRequest) -> func.HttpResponse:
        return render_swagger_ui(openapi_url="/api/openapi.json")

Set ENABLE_API_DOCS=true only in environments where these routes should exist. Any other value, including an unset setting, omits them at import time.

2. Require an Azure Functions key for direct spec access

Functions authentication can protect direct JSON/YAML downloads, but it does not by itself produce a working protected Swagger UI. Loading /api/docs with an x-functions-key header or ?code= authenticates only the page request. render_swagger_ui() makes a separate browser request to openapi_url; it does not forward the page's header or query parameter. A function-scoped key for the docs function may also be unauthorized for the JSON function.

Protect a specification route when direct, non-interactive access is needed:

@app.route(route="openapi.json", methods=["GET"], auth_level=func.AuthLevel.FUNCTION)
def openapi_json(req: func.HttpRequest) -> func.HttpResponse:
    return func.HttpResponse(
        get_openapi_json(title="My API", app=app),
        mimetype="application/json",
    )

Call that route with an x-functions-key header or a ?code= query parameter. Passing a key in openapi_url, such as /api/openapi.json?code=<host-key>, can make Swagger UI fetch the protected specification, but is discouraged except in a trusted internal environment: the key appears in the generated page and may be recorded in browser history and logs. Prefer conditional registration (pattern 1) or protect the UI and spec together with cookie/session-based edge authentication (pattern 3). An anonymous specification route is another option only when it is conditionally registered in trusted environments and its exposure is intentional.

3. Enforce access at the platform edge

For identity-aware or policy-based access, keep the documentation endpoints behind Azure API Management, Microsoft Entra ID through App Service Authentication (Easy Auth), or equivalent ingress controls. Configure the edge to require the intended identity or policy for /api/docs, /api/openapi.json, and /api/openapi.yaml, and prevent a direct Function App URL from bypassing that control (for example, with access restrictions or private networking).

Swagger UI Try-it-out behavior

render_swagger_ui() enables Try-it-out and allows all supported submit methods: GET, POST, PUT, DELETE, PATCH, HEAD, and OPTIONS. Anyone who can open the UI can therefore initiate browser-originated calls to documented API operations. API authentication always applies. CORS applies only to cross-origin requests and is not access control: when Swagger UI and the API share the Function App origin, Try-it-out is same-origin and no CORS check is performed. Exposing Swagger UI does not bypass API authentication, but a CORS policy must not be relied on to restrict who can call the API.

Operation ID Sanitization

Operation IDs are sanitized to: - Remove dangerous characters - Keep only alphanumeric characters and underscores - Ensure they start with a letter (prefixed with op_ if needed)

Parameter Validation

Parameters are validated to ensure: - Required fields (name, in) are present - Each parameter is a valid dictionary - Parameter structure follows OpenAPI specification

Tag Validation

Tags are validated to: - Ensure they are strings - Remove whitespace - Prevent empty tags

Caching

Caching should be handled at the application or platform level. Per-request generation is acceptable for a small registry or an infrequently requested documentation endpoint. For a larger registry or a frequently requested endpoint, generate the serialized spec after imports complete (or on the first request), store it in a module global, and return that immutable string on later requests. Invalidate it only if the application deliberately registers operations after import. The library does not cache because it cannot know when an application's registry is complete.

_OPENAPI_JSON = get_openapi_json(title="Sample API", app=app)


def openapi_json(req):
    return func.HttpResponse(_OPENAPI_JSON, mimetype="application/json")

Use platform response caching only when its access controls and invalidation policy match the sensitivity of the generated document. See Architecture for measured costs and regression budgets.

Security Best Practices

For Developers

  1. Always validate input: Use the built-in validation functions
  2. Sanitize user data: Let the library handle sanitization
  3. Use HTTPS: Ensure your Azure Functions use HTTPS
  4. Monitor logs: Check error logs for security issues
  5. Keep dependencies updated: Regularly update the library

For Deployment

  1. Environment Variables: Use secure environment variables for configuration
  2. Network Security: Configure proper network security groups
  3. Access Control: Implement proper authentication and authorization
  4. Regular Updates: Keep the runtime and dependencies updated

For API Design

  1. Minimal Information: Don't expose sensitive information in OpenAPI specs
  2. Proper Authentication: Document authentication requirements
  3. Rate Limiting: Implement rate limiting for your APIs
  4. Input Validation: Validate all inputs at the application level
  5. Error Messages: Don't expose internal details in error messages

Security Configuration

Custom CSP Policy

You can provide a custom CSP policy:

from azure_functions_openapi.swagger_ui import render_swagger_ui

custom_csp = "default-src 'self'; script-src 'self'"
response = render_swagger_ui(custom_csp=custom_csp)

Security Headers

Security headers are automatically added to all responses. You can customize them by modifying the render_swagger_ui function.

Logging

Security events are logged with appropriate levels: - INFO: Normal operations - WARNING: Security warnings (e.g., invalid input) - ERROR: Security errors (e.g., validation failures)

Security Automation

CI/CD Scanning

The repository uses automated security scanning:

  • Bandit for Python static analysis
  • Semgrep for broader rule coverage

Security scans run in GitHub Actions via .github/workflows/security.yml. For local parity, run make security. - Dependabot for dependency updates

SBOM Generation

Software Bill of Materials (SBOM) generation is supported via CI workflows to track dependency inventory and reduce supply chain risk.

Security Headers Validation

Validate that security headers are present on Swagger UI responses:

  • X-Content-Type-Options
  • X-Frame-Options
  • Referrer-Policy
  • Content-Security-Policy

Automated checks should be included in tests or smoke checks for deployments.

Data Privacy and Compliance

When processing user data:

  • Avoid storing sensitive data in logs
  • Minimize data collection and retention
  • Use encryption in transit and at rest
  • Follow applicable privacy regulations for your region

Incident Response

Follow your organization's standard security runbooks for incidents and disclosures.

Reporting Security Issues

If you discover a security vulnerability, please:

  1. Do not create a public issue
  2. Email security concerns to: yeongseon.choe@gmail.com
  3. Include detailed information about the vulnerability
  4. Allow time for the issue to be addressed before disclosure

Security Updates

Security updates are released as: - Patch versions (e.g., 1.0.1) for critical security fixes - Minor versions (e.g., 1.1.0) for security improvements - Major versions (e.g., 2.0.0) for breaking security changes

Always update to the latest version to ensure you have the latest security fixes.