Security Overviewο
Security is a fundamental aspect of ostructβs design. This guide covers API key management, file access control, data handling policies, and security best practices for production deployments.
Warning
Data Privacy: All content sent to ostruct may be transmitted to external AI services:
Template files (
--file alias path): Content is included in prompts sent to OpenAICode Interpreter files (
--file ci:alias path): Files are uploaded to OpenAI for executionFile Search files (
--file fs:alias path): Files are uploaded to OpenAI for vector search
Review your data sensitivity before processing confidential information with any tool routing option.
Note
For quick security configuration, see the CLI Reference section on Path Security.
Security Architectureο
ostruct implements a multi-layered security model:
API Key Management - Secure handling of OpenAI credentials
File Access Control - Path validation and directory restrictions
Data Upload Controls - Tool-specific file routing with explicit user control
MCP Security - Validation and approval for external server connections
Runtime Security - Symlink resolution and path traversal prevention
API Key Managementο
Secure Credential Handlingο
ostruct never logs or stores API keys. Credentials are handled through:
Environment Variables (Recommended):
export OPENAI_API_KEY="your-api-key-here"
ostruct run template.j2 schema.json
.env Files (Convenient for Development):
# Create .env file in your project directory
echo 'OPENAI_API_KEY=your-api-key-here' > .env
ostruct run template.j2 schema.json
Note
ostruct automatically loads .env files from the current directory. Environment variables take precedence over .env file values.
Command Line (Development Only):
ostruct run template.j2 schema.json --api-key "your-api-key"
Warning
CLI API Keys: Avoid using --api-key in production as it may be visible in process lists or shell history.
Configuration Files:
# ostruct.yaml
api:
key: "${OPENAI_API_KEY}" # Environment variable substitution
Best Practicesο
β Do: - Use environment variables for API keys - Rotate API keys regularly - Use dedicated API keys for different environments - Set usage limits in OpenAI dashboard - Monitor API usage and costs
β Donβt: - Commit API keys to version control - Share API keys in plain text - Use production keys in development - Log or print API keys in applications
Environment-Specific Keysο
# Development
export OPENAI_API_KEY="sk-dev-..."
# Staging
export OPENAI_API_KEY="sk-staging-..."
# Production
export OPENAI_API_KEY="sk-prod-..."
File Access Controlο
SecurityManager Architectureο
All file operations in ostruct go through a centralized SecurityManager located at src/ostruct/cli/security/security_manager.py. This provides:
Path Normalization: Resolves relative paths and symlinks safely
Directory Validation: Ensures files are within allowed directories
Symlink Protection: Prevents directory traversal attacks
Case-Sensitive Handling: Platform-appropriate path handling
Allowed Directoriesο
By default, ostruct restricts file access to the current working directory. Expand access with:
Single Directory:
ostruct run template.j2 schema.json --allow /data --file config /data/config.yaml
Multiple Directories:
ostruct run template.j2 schema.json \
--allow /data \
--allow /configs \
--allow /tmp/workspace \
--file config /data/input.csv
From File:
# allowed_dirs.txt
/data
/configs
/tmp/workspace
ostruct run template.j2 schema.json --allow-list allowed_dirs.txt
Base Directory Controlο
Set a base directory to restrict all relative path operations:
# All relative paths resolve within /project
ostruct run template.j2 schema.json \
--path-security strict --allow /project \
--file config config.yaml \
--file config data/input.csv
Security Validation Processο
For every file access, ostruct:
Normalizes the path (resolves
.,.., symlinks)Validates the path is within allowed directories
Checks file existence and permissions
Resolves symlinks with depth and loop protection
Provides the validated absolute path to the application
Path Traversal Preventionο
ostruct prevents common path traversal attacks:
# These are blocked by SecurityManager
ostruct run template.j2 schema.json --file config "../../../etc/passwd"
ostruct run template.j2 schema.json --file config "config/../../../sensitive.txt"
# Use allowed directories for legitimate access outside project
ostruct run template.j2 schema.json --allow /etc --file config /etc/config.yaml
Path Security Warningsο
When ostruct accesses files outside your project directory, it shows helpful security notices with actionable guidance:
π Security Notice: Accessing downloaded file 'document.pdf' from /Users/you/Downloads
outside the current project directory.
To allow this directory: --allow '/Users/you/Downloads'
To allow this file only: --allow-file '/Users/you/Downloads/document.pdf'
To disable warnings: --path-security permissive
Warning Features:
Contextual Messages: Identifies file types (document, data file, downloaded file)
Actionable Guidance: Shows exact CLI flags to resolve the warning
Deduplication: Each file triggers only one warning per session
Thread-Safe: Safe for concurrent file access scenarios
Configuration Options:
# ostruct.yaml
security:
path_security: warn # Options: permissive, warn, strict
suppress_path_warnings: false # Disable repeated warnings
warning_summary: true # Show summary at end of execution
Troubleshooting Security Warnings:
Problem |
Solution |
|---|---|
βAccessing file outside project directoryβ |
Use |
Need access to specific file only |
Use |
Working with external files regularly |
Use |
Files are in project but still warned |
Check if files are symlinks to external locations |
URL Validation & Remote Attachmentsο
ostruct supports remote HTTP/HTTPS URLs as attachments (e.g. --file ud:deck https://β¦/pitch.pdf).
To keep you safe the following rules apply by default:
Only HTTPS URLs are allowed. PlainβHTTP or other schemes (
ftp:,file:,javascript:β¦) are rejected.Private-network addresses (RFC-1918, loopback, link-local) are blocked to prevent SSRF style attacks.
A quick ``HEAD`` probe is executed during ``βdry-run`` to catch broken links early; unreachable URLs are shown with β in the plan printer.
If a URL violates these rules ostruct raises InsecureURLRejected.
Tuning URL securityο
--allow-insecure-url URL Allow a specific non-HTTPS or private URL (repeatable).
--strict-urls / --no-strict-urls Globally enable/disable URL validation (default: strict).
Warning
Disabling strict URL checks may expose your environment to SSRF or credential-leak risks. Prefer whitelisting with --allow-insecure-url.
User-Data (Vision Model) Uploadsο
Attachments routed to the user-data target are sent verbatim to vision-enabled models (e.g. GPT-4o) and are not included in template text. Security rules:
Only PDF files are currently accepted by OpenAI.
Hard limit : 512 MB β larger files raise an error before upload.
Warning threshold : 50 MB β ostruct logs an informational message.
Accessing
.contentin templates is blocked and raisesTemplateBinaryError.
If a run includes user-data files but the chosen model lacks vision support ostruct aborts with UserDataNotSupportedError.
Data Upload and Tool Securityο
File Search Data Handlingο
Important
Future-Proof Policy: Files may be uploaded to external services, depending on the backend provider. The current implementation uploads files to OpenAIβs File Search service for vector processing.
What happens to your files: - Files are uploaded to vector stores for semantic search - Content is processed and indexed for retrieval - Files are accessible during the session for search operations - Cleanup removes files and vector stores after completion (when enabled)
Security considerations: - Review data sensitivity before uploading documents - Consider redacting sensitive information from documents - Use cleanup options to remove data after processing - Monitor your OpenAI usage dashboard for uploaded files
Code Interpreter Data Handlingο
Important
Data Upload: Files are uploaded to OpenAIβs Code Interpreter environment for Python execution and analysis.
What happens to your files: - Files are uploaded to an isolated execution environment - Code can read, process, and analyze the files - Generated outputs (charts, results) can be downloaded - Cleanup removes uploaded files after execution (when enabled)
Security considerations: - Avoid uploading confidential datasets - Review generated outputs before sharing - Use cleanup options to manage storage quotas - Consider data anonymization for sensitive datasets
Web Search Data Handlingο
Important
Search Query Privacy: When using --enable-tool web-search, search queries may be sent to external search services via OpenAI. These queries can be derived from your prompts and template content.
What happens during web search: - Search queries are generated based on your prompt and template content - Queries are sent to external search services through OpenAIβs web search tool - Search results are retrieved and processed by the model - No files are uploaded, but prompt content may influence search queries
API Key and Authentication:
- Web search uses your existing OPENAI_API_KEY - no separate authentication required
- The same API key that powers other ostruct features also handles web search requests
- No additional API keys or service subscriptions needed beyond your OpenAI account
Rate Limits and Quotas: - Web search requests count toward your standard OpenAI API rate limits (RPM/TPM) - No separate rate limits are imposed specifically on the web search tool - Existing ostruct retry logic and error handling applies to web search operations - Monitor your OpenAI dashboard for usage tracking across all features including web search
Security considerations:
- Avoid sensitive information in prompts when using web search
- Review template content for potentially sensitive keywords or data
- Consider using --disable-tool web-search for sensitive prompts
- Be aware that search queries may be logged by search providers
- Web search is automatically disabled for Azure OpenAI endpoints
Best practices: - Use generic terms rather than specific internal project names - Avoid including personal information, credentials, or proprietary data in prompts - Test with public information first to understand search behavior - Consider the query implications of your template variables
Opt-in requirement:
Web search is always opt-in and requires explicit use of the --enable-tool web-search flag. This ensures users are aware when external search services may be accessed.
Template File Securityο
Template files (--file alias path) are processed differently than Code Interpreter and File Search files:
Files remain on your local system (not uploaded as file objects)
Content is read locally and included in template rendering
Template content is sent to OpenAI servers as part of the prompt text
Consider data sensitivity when including file content in templates
Tool Routing Security Matrixο
Flag |
Security Level |
Data Handling |
|---|---|---|
|
Medium Security |
Content sent in prompt to OpenAI |
|
Medium Security |
Uploaded to OpenAI for execution |
|
Medium Security |
Uploaded to OpenAI for vector search |
Cleanup and Data Retentionο
Enable cleanup to minimize data retention:
# Enable cleanup (default: true)
ostruct run template.j2 schema.json \
--file ci:data data.csv \
--ci-cleanup
ostruct run template.j2 schema.json \
--file fs:docs docs.pdf \
--fs-cleanup
MCP Server Securityο
Model Context Protocol (MCP) servers extend ostruct with external capabilities, requiring additional security considerations.
Server Validationο
ostruct validates MCP connections:
URL Validation: Ensures proper HTTPS URLs for remote servers
Certificate Validation: Verifies SSL certificates for secure connections
Timeout Controls: Prevents hanging connections
Error Handling: Graceful failure for unreachable servers
Example secure connection:
ostruct run template.j2 schema.json \
--mcp-server "deepwiki@https://mcp.deepwiki.com/sse" \
--mcp-headers '{"Authorization": "Bearer your-token"}'
Tool Restrictionsο
Restrict which tools MCP servers can use:
# Allow only specific tools
ostruct run template.j2 schema.json \
--mcp-server "research@https://mcp.example.com" \
--mcp-allowed-tools "research:search,summarize"
Approval Controlsο
# Require approval for tool usage (CLI requires 'never')
ostruct run template.j2 schema.json \
--mcp-server "external@https://mcp.example.com" \
--mcp-require-approval never
Authenticationο
Secure MCP server authentication:
# Bearer token authentication
ostruct run template.j2 schema.json \
--mcp-server "secure@https://mcp.example.com" \
--mcp-headers '{"Authorization": "Bearer token123"}'
# API key authentication
ostruct run template.j2 schema.json \
--mcp-server "api@https://mcp.example.com" \
--mcp-headers '{"X-API-Key": "key123"}'
Third-Party Security Reviewο
Before connecting to MCP servers:
Review server documentation for data handling policies
Verify HTTPS and certificate validity
Understand what data may be sent to the server
Check authentication requirements
Test with non-sensitive data first
Threat Model and Risk Assessmentο
Data Classificationο
Classify your data before processing:
Public Data β - Public documentation - Open source code - Marketing materials - Published research
Internal Data β οΈ - Configuration files (review for secrets before including in templates) - Development code (review for credentials before including in templates) - Business documents (assess sensitivity before including in prompts) - Log files (may contain sensitive information - review before processing)
Confidential Data β - Customer PII - Financial records - Authentication credentials - Trade secrets
Restricted Data π« - Government classified information - Healthcare PHI/PII - Payment card data - Legal privileged information
Common Threats and Mitigationsο
Path Traversal Attacks - Threat: Malicious paths accessing unauthorized files - Mitigation: SecurityManager validation, allowed directories
Credential Exposure - Threat: API keys in logs, processes, or version control - Mitigation: Environment variables, secure handling
Data Exfiltration - Threat: Sensitive data uploaded to external services - Mitigation: Tool routing control, data classification
Injection Attacks - Threat: Malicious content in templates or file names - Mitigation: Template validation, path sanitization
MCP Server Compromise - Threat: Malicious or compromised external servers - Mitigation: HTTPS validation, tool restrictions, approval controls
Production Security Checklistο
Pre-Deployment Security Reviewο
β‘ API keys stored in environment variables
β‘ No hardcoded credentials in templates or configs
β‘ Allowed directories properly configured
β‘ Base directory set for path restriction
β‘ File routing reviewed for data sensitivity
β‘ Cleanup enabled for uploaded files
β‘ MCP servers reviewed and validated
β‘ Data classification completed
β‘ Security policies documented
Runtime Security Monitoringο
β‘ API usage monitoring enabled
β‘ File access logging reviewed
β‘ Upload cleanup verified
β‘ Error handling for security failures
β‘ Regular security assessment scheduled
Incident Responseο
If security issues occur:
Immediate Actions: - Rotate compromised API keys - Remove uploaded sensitive data - Disconnect compromised MCP servers - Review logs for unauthorized access
Investigation: - Identify scope of data exposure - Review file access logs - Check API usage patterns - Assess impact on downstream systems
Recovery: - Implement additional controls - Update security documentation - Train team on new procedures - Monitor for recurring issues
Security Configuration Examplesο
Development Environmentο
# Development: Relaxed but secure
export OPENAI_API_KEY="sk-dev-..."
ostruct run template.j2 schema.json \
--path-security strict --allow ./project \
--allow ./test_data \
--file config config.yaml \
--file ci:data test_data.csv \
--ci-cleanup \
--fs-cleanup
Staging Environmentο
# Staging: Production-like security
export OPENAI_API_KEY="sk-staging-..."
ostruct run template.j2 schema.json \
--path-security strict --allow /app \
--allow /app/data \
--allow /app/configs \
--allow-list /app/allowed_dirs.txt \
--file config configs/app.yaml \
--ci-cleanup \
--fs-cleanup \
--verbose
Production Environmentο
# Production: Maximum security
export OPENAI_API_KEY="sk-prod-..."
ostruct run template.j2 schema.json \
--path-security strict --allow /prod/app \
--allow-list /prod/security/allowed_dirs.txt \
--file config configs/production.yaml \
--ci-cleanup \
--fs-cleanup \
--timeout 300
CI/CD Pipeline Securityο
# .github/workflows/secure-analysis.yml
steps:
- name: Secure Analysis
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
ostruct run analysis.j2 schema.json \
--path-security strict --allow ${{ github.workspace }} \
--allow ${{ github.workspace }}/data \
--file config config.yaml \
--file ci:data data/metrics.csv \
--ci-cleanup \
--fs-cleanup \
--output-file results.json
Security Resourcesο
Documentationο
CLI Reference - Complete CLI security options
Quick Start Guide - Security-aware examples
CI/CD and Container Deployment - Secure CI/CD integration
Code Referencesο
src/ostruct/cli/security/security_manager.py- Main security validationsrc/ostruct/cli/security/allowed_checker.py- Directory validationsrc/ostruct/cli/security/symlink_resolver.py- Symlink safetysrc/ostruct/cli/security/normalization.py- Path normalization
External Resourcesο
OpenAI API Documentation (available on the OpenAI Platform)
Getting Security Helpο
If you discover security issues:
For ostruct vulnerabilities: Report to the project maintainers
For OpenAI API issues: Contact OpenAI support
For MCP server issues: Contact the server provider
For general security questions: Consult your security team
Remember: Security is a shared responsibility between ostruct, service providers, and your implementation.