andyeick commited on
Commit
1979bf2
·
verified ·
1 Parent(s): a33cc5f

🛡️ Patching and Rebuilding the Document Processor APIIntroductionWelcome to the secure software supply chain automation notebook! As a Senior DevOps Engineer, our goal here is to take the vulnerable code base for the Document Processor API and fully remediate its critical dependencies before triggering a new, secure container build using Google Cloud Build.We have identified and patched high-severity vulnerabilities in h11, transformers, and setuptools. This notebook consolidates those fixes into an automated process.Step 1: Setup and Clone RepositoryWe first clone the original vulnerable project repository. The existing structure (like the app/ directory) is needed to complete the build process.# Clone the repository containing the original application code

Browse files

!git clone [https://github.com/olwordeaux/document-processor.git](https://github.com/olwordeaux/document-processor.git)

# Change into the project directory
%cd document-processor
Step 2: Create Patched pyproject.tomlThe original pyproject.toml contained insecure dependency versions. We will replace it entirely with a new version that explicitly pins the patched versions for known vulnerabilities:h11 (^0.16.0): Fixes a Critical HTTP Request Smuggling vulnerability (CVE-2025-43859).transformers (^4.53.0): Fixes multiple High-Severity ReDoS (Regular Expression Denial of Service) issues.setuptools (^78.1.1): Fixes a High-Severity Path Traversal vulnerability (CVE-2025-47273).# Use a HEREDOC to create the patched pyproject.toml file
cat <<EOF > pyproject.toml
[tool.poetry]
name = "document-processor-api"
version = "0.1.0"
description = "FastAPI service for PDF content extraction."
authors = ["Senior Software Engineer <se@example.com>"]
readme = "README.md"
packages = [{include = "app"}]

[tool.poetry.dependencies]
# Required Python version
python = "^3.12"

# Core Application Dependencies
fastapi = "^0.111.0"
uvicorn = {extras = ["standard"], version = "^0.30.1"}

# CRITICAL-SEVERITY PATCH: h11 upgraded to minimum 0.16.0 to fix Request Smuggling (CVE-2025-43859).
h11 = "^0.16.0"

# HIGH-SEVERITY PATCH: transformers upgraded to minimum 4.53.0 to fix ReDoS issues.
transformers = "^4.53.0"

# HIGH-SEVERITY PATCH: setuptools upgraded to minimum 78.1.1 to fix Path Traversal (CVE-2025-47273).
setuptools = "^78.1.1"

# Note: In a real project, other document-processing dependencies (like PyPDF2, Tesseract)
# would also be listed here.

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
EOF

echo "Successfully created patched pyproject.toml"
Step 3: Generate Secure Lock FileThe uv.lock file ensures that the build process will install the exact dependency versions resolved by uv, including all transitive dependencies. This guarantees a deterministic and secure build, preventing dependency confusion or unexpected version drift.# Install uv locally (if not already available)
!pip install uv

# Generate the uv.lock file based on the patched pyproject.toml
!uv lock
Step 4: Create the DockerfileThis Dockerfile is based on python:3.12-slim (a patched and maintained base image) and uses uv in deployment mode (--locked) to enforce installation strictly from the generated uv.lock file, thus ensuring only patched packages are installed.# Use a HEREDOC to create the Dockerfile
cat <<DOCKERFILE_EOF > Dockerfile
FROM python:3.12-slim

ARG PORT=8000
ENV ENV=production \
THREADS=4 \
PORT=\${PORT} \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

WORKDIR /src

# Create a system user for running the app securely
RUN addgroup --system app && adduser --system --group --no-create-home app

# Copy and install dependencies securely using uv
COPY --chown=app:app pyproject.toml uv.lock ./
RUN pip install uv && uv pip install --system --no-cache --locked -r uv.lock

# Copy the application code
COPY --chown=app:app app/ ./app

# Switch to the non-root user for execution
USER app:app

# Expose the port
EXPOSE \${PORT}

# Run the application using hypercorn (FastAPI compatible)
CMD ["hypercorn", "app.api:api", "--bind", "0.0.0.0:\${PORT}", "--workers", "1"]
DOCKERFILE_EOF

echo "Successfully created Dockerfile"
Step 5: Authenticate with Google CloudBefore we can trigger a build, we must authenticate this notebook session with your Google Cloud Project and set the necessary project context.🚨 Action Required: Replace YOUR_PROJECT_ID with your actual Google Cloud Project ID.# Log in to Google Cloud. A pop-up window will guide you through the authentication process.
!gcloud auth login

# Set the active Google Cloud project ID
# REPLACE 'YOUR_PROJECT_ID' with your actual Project ID
PROJECT_ID="YOUR_PROJECT_ID"
!gcloud config set project $PROJECT_ID
Step 6: Trigger the Secure BuildWe are now ready to submit the entire context (including the new pyproject.toml, uv.lock, and Dockerfile) to Google Cloud Build. This will create a secure container image tagged with the latest version in your project's Container Registry (gcr.io).# Define the image name
IMAGE_NAME="document-processor-secure"
TAG="gcr.io/$PROJECT_ID/$IMAGE_NAME:latest"

echo "Submitting build to GCB for image: $TAG"

# Trigger the build process
!gcloud builds submit --tag $TAG --timeout=10m

echo "Build submission complete. Check the Cloud Build dashboard for progress and logs."
Step 7: Next Steps (Post-Build)Once Google Cloud Build finishes (it may take a few minutes), your secure container image will be available in your project's Container Registry.To run and test the patched API, execute the following command in a secure environment with Docker installed:# Pull the newly built secure image
docker pull gcr.io/YOUR_PROJECT_ID/document-processor-secure:latest

# Run the secure container
docker run -d -p 8080:8000 gcr.io/YOUR_PROJECT_ID/document-processor-secure:latest
You can now verify the API's functionality and confirm, using vulnerability scanners, that the high-severity issues have been resolved by the patched dependencies.

Files changed (2) hide show
  1. README.md +8 -5
  2. index.html +158 -18
README.md CHANGED
@@ -1,10 +1,13 @@
1
  ---
2
- title: Securedoc Builder
3
- emoji: 👀
4
- colorFrom: red
5
- colorTo: purple
6
  sdk: static
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
  ---
2
+ title: SecureDoc Builder 🛡️
3
+ colorFrom: gray
4
+ colorTo: gray
5
+ emoji: 🐳
6
  sdk: static
7
  pinned: false
8
+ tags:
9
+ - deepsite-v3
10
  ---
11
 
12
+ # Welcome to your new DeepSite project!
13
+ This project was created with [DeepSite](https://deepsite.hf.co).
index.html CHANGED
@@ -1,19 +1,159 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>SecureDoc Builder</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script src="https://unpkg.com/feather-icons"></script>
9
+ <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
10
+ <style>
11
+ .gradient-bg {
12
+ background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 50%, #3b82f6 100%);
13
+ }
14
+ .code-block {
15
+ font-family: 'Courier New', monospace;
16
+ background-color: #1e293b;
17
+ color: #f8fafc;
18
+ border-radius: 0.5rem;
19
+ padding: 1.5rem;
20
+ overflow-x: auto;
21
+ }
22
+ .step-card {
23
+ transition: all 0.3s ease;
24
+ }
25
+ .step-card:hover {
26
+ transform: translateY(-5px);
27
+ box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
28
+ }
29
+ </style>
30
+ </head>
31
+ <body class="bg-gray-50">
32
+ <!-- Hero Section -->
33
+ <div class="gradient-bg text-white">
34
+ <div class="container mx-auto px-6 py-24">
35
+ <div class="flex flex-col md:flex-row items-center">
36
+ <div class="md:w-1/2 mb-10 md:mb-0">
37
+ <h1 class="text-4xl md:text-6xl font-bold mb-6">SecureDoc Builder 🛡️</h1>
38
+ <p class="text-xl md:text-2xl mb-8">Automated security patching for your document processing API</p>
39
+ <div class="flex space-x-4">
40
+ <button class="bg-white text-blue-600 px-6 py-3 rounded-lg font-semibold hover:bg-gray-100 transition">Get Started</button>
41
+ <button class="border border-white text-white px-6 py-3 rounded-lg font-semibold hover:bg-blue-700 transition">Learn More</button>
42
+ </div>
43
+ </div>
44
+ <div class="md:w-1/2">
45
+ <div class="code-block">
46
+ <span class="text-green-400"># Clone the repository</span><br>
47
+ <span class="text-white">!git clone https://github.com/olwordeaux/document-processor.git</span><br><br>
48
+ <span class="text-green-400"># Generate secure lock file</span><br>
49
+ <span class="text-white">!uv lock</span><br><br>
50
+ <span class="text-green-400"># Build secure container</span><br>
51
+ <span class="text-white">!gcloud builds submit --tag $TAG</span>
52
+ </div>
53
+ </div>
54
+ </div>
55
+ </div>
56
+ </div>
57
+
58
+ <!-- Steps Section -->
59
+ <div class="container mx-auto px-6 py-20">
60
+ <h2 class="text-3xl font-bold text-center mb-16">Secure Build Process</h2>
61
+ <div class="grid md:grid-cols-3 gap-8">
62
+ <!-- Step 1 -->
63
+ <div class="step-card bg-white p-8 rounded-xl shadow-lg">
64
+ <div class="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mb-6">
65
+ <span class="text-blue-600 text-2xl font-bold">1</span>
66
+ </div>
67
+ <h3 class="text-xl font-bold mb-4">Vulnerability Analysis</h3>
68
+ <p class="text-gray-600 mb-4">Identify critical security issues in dependencies like h11, transformers, and setuptools.</p>
69
+ <div class="flex items-center text-blue-600">
70
+ <i data-feather="alert-triangle" class="mr-2"></i>
71
+ <span>3 Critical Patches</span>
72
+ </div>
73
+ </div>
74
+
75
+ <!-- Step 2 -->
76
+ <div class="step-card bg-white p-8 rounded-xl shadow-lg">
77
+ <div class="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mb-6">
78
+ <span class="text-blue-600 text-2xl font-bold">2</span>
79
+ </div>
80
+ <h3 class="text-xl font-bold mb-4">Dependency Patching</h3>
81
+ <p class="text-gray-600 mb-4">Automatically update pyproject.toml with secure versions and generate lock file.</p>
82
+ <div class="flex items-center text-blue-600">
83
+ <i data-feather="lock" class="mr-2"></i>
84
+ <span>Secure Versions</span>
85
+ </div>
86
+ </div>
87
+
88
+ <!-- Step 3 -->
89
+ <div class="step-card bg-white p-8 rounded-xl shadow-lg">
90
+ <div class="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mb-6">
91
+ <span class="text-blue-600 text-2xl font-bold">3</span>
92
+ </div>
93
+ <h3 class="text-xl font-bold mb-4">Secure Container Build</h3>
94
+ <p class="text-gray-600 mb-4">Build and deploy a hardened container image with Google Cloud Build.</p>
95
+ <div class="flex items-center text-blue-600">
96
+ <i data-feather="package" class="mr-2"></i>
97
+ <span>Production Ready</span>
98
+ </div>
99
+ </div>
100
+ </div>
101
+ </div>
102
+
103
+ <!-- Code Example Section -->
104
+ <div class="bg-gray-100 py-20">
105
+ <div class="container mx-auto px-6">
106
+ <h2 class="text-3xl font-bold text-center mb-12">Secure Configuration</h2>
107
+ <div class="max-w-4xl mx-auto">
108
+ <div class="code-block mb-8">
109
+ <span class="text-green-400"># Patched pyproject.toml</span><br>
110
+ <span class="text-purple-400">[tool.poetry.dependencies]</span><br>
111
+ <span class="text-white">python = "^3.12"</span><br>
112
+ <span class="text-white">fastapi = "^0.111.0"</span><br>
113
+ <span class="text-yellow-400"># CRITICAL-SEVERITY PATCH</span><br>
114
+ <span class="text-white">h11 = "^0.16.0"</span><br>
115
+ <span class="text-yellow-400"># HIGH-SEVERITY PATCH</span><br>
116
+ <span class="text-white">transformers = "^4.53.0"</span><br>
117
+ <span class="text-white">setuptools = "^78.1.1"</span><br>
118
+ </div>
119
+ <p class="text-gray-600 text-center max-w-2xl mx-auto">
120
+ Our automated process ensures all dependencies are pinned to secure versions, preventing known vulnerabilities from reaching production.
121
+ </p>
122
+ </div>
123
+ </div>
124
+ </div>
125
+
126
+ <!-- CTA Section -->
127
+ <div class="gradient-bg text-white py-20">
128
+ <div class="container mx-auto px-6 text-center">
129
+ <h2 class="text-3xl md:text-4xl font-bold mb-6">Ready to Secure Your API?</h2>
130
+ <p class="text-xl mb-8 max-w-2xl mx-auto">Automate your security patches and build process with SecureDoc Builder today.</p>
131
+ <button class="bg-white text-blue-600 px-8 py-4 rounded-lg font-semibold text-lg hover:bg-gray-100 transition">Start Free Trial</button>
132
+ </div>
133
+ </div>
134
+
135
+ <!-- Footer -->
136
+ <footer class="bg-gray-900 text-white py-12">
137
+ <div class="container mx-auto px-6">
138
+ <div class="flex flex-col md:flex-row justify-between items-center">
139
+ <div class="mb-6 md:mb-0">
140
+ <h3 class="text-2xl font-bold">SecureDoc Builder 🛡️</h3>
141
+ <p class="text-gray-400 mt-2">Automated security for your document processing</p>
142
+ </div>
143
+ <div class="flex space-x-6">
144
+ <a href="#" class="hover:text-blue-400 transition"><i data-feather="github"></i></a>
145
+ <a href="#" class="hover:text-blue-400 transition"><i data-feather="twitter"></i></a>
146
+ <a href="#" class="hover:text-blue-400 transition"><i data-feather="linkedin"></i></a>
147
+ </div>
148
+ </div>
149
+ <div class="border-t border-gray-800 mt-8 pt-8 text-center text-gray-400">
150
+ <p>© 2023 SecureDoc Builder. All rights reserved.</p>
151
+ </div>
152
+ </div>
153
+ </footer>
154
+
155
+ <script>
156
+ feather.replace();
157
+ </script>
158
+ </body>
159
  </html>