Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
5e788ed
Harden k8s secrets and Terraform networking
KARLalpha4768 Oct 25, 2026
9a8113f
Fix Docker build: uvicorn path, C++ sources, drop Docker socket mount
KARLalpha4768 Oct 25, 2026
94964cb
Move Cesium token, API key and DB DSN to environment variables
KARLalpha4768 Oct 25, 2026
4ef8201
Fix HIGH correctness bugs CORR-01..05
KARLalpha4768 Oct 25, 2026
9b74d46
Lock down /api/nlp: API key auth, rate limit, input validation
KARLalpha4768 Oct 25, 2026
923abbf
fix(twin-ws): share one broadcaster task and rotate ECI to Earth-fixe…
KARLalpha4768 Oct 25, 2026
1fc27a9
fix(orbit): RA wrap and cos(dec) in angular rate, clamp asin, robust …
KARLalpha4768 Oct 25, 2026
7f8505d
fix(db): commit/rollback/close context manager and fixed parameterize…
KARLalpha4768 Oct 25, 2026
eabc0a8
chore: dedupe photometry source, add .dockerignore, run pytest in CI,…
KARLalpha4768 Oct 25, 2026
2dc57de
Merge fix/orbit-propagator into fix/audit-remediation
claude Nov 15, 2026
a0dc84c
Merge fix/correctness-high into fix/audit-remediation
claude Nov 15, 2026
84e6c25
Merge fix/db into fix/audit-remediation
claude Nov 15, 2026
98ef571
Merge fix/twin-ws into fix/audit-remediation
claude Nov 15, 2026
8b4e030
Merge fix/docker-build into fix/audit-remediation
claude Nov 15, 2026
17501dd
Merge fix/infra-hardening into fix/audit-remediation
claude Nov 15, 2026
b122d47
Merge fix/secrets-env into fix/audit-remediation
claude Nov 15, 2026
4183ad8
Merge fix/api-nlp-lockdown into fix/audit-remediation
claude Nov 15, 2026
9e6882e
Merge chore/build-test-hygiene into fix/audit-remediation
claude Nov 15, 2026
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
35 changes: 35 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.git
.gitignore
.env*

.venv/
venv/
env/
**/.venv/
**/venv/
**/node_modules/

**/__pycache__/
*.py[cod]
.pytest_cache/
.ruff_cache/
.coverage
coverage.xml
junit*.xml
htmlcov/
test-results/
tests/

data/*.db
*.sqlite
*.sqlite3
.codex-*

secrets/
**/secrets/
*.pem
*.key
*.p12
*.pfx

output/builds/build/
29 changes: 29 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SentinelForge Environment Configuration Example
# Copy this file to .env and populate with production values. Do NOT commit .env.

# Cesium Ion personal access token for 3D globe tiles and imagery
CESIUM_ION_TOKEN=replace_with_cesium_ion_token

# Internal API authentication key for SentinelForge REST endpoints
SF_API_KEY=replace_with_internal_api_key

# API key required in the X-API-Key header for /api/nlp
NLP_API_KEY=replace_with_nlp_api_key

# Google Gemini API key for natural language command interpretation
GEMINI_API_KEY=replace_with_gemini_api_key

# Comma-separated list of allowed CORS origins for serverless endpoints
ALLOWED_ORIGINS=https://example.invalid

# PostgreSQL / PostGIS database connection URL
DATABASE_URL=replace_with_database_connection_url

# PostgreSQL password for catalog store
POSTGIS_PASSWORD=replace_with_database_password

# Mutual TLS SSL private key content for Kafka transport
KAFKA_SSL_KEY=replace_with_kafka_private_key

# Optional path to PyTorch weights for thermospheric density MLP
DENSITY_MODEL_PATH=/path/to/density_mlp.pth
29 changes: 18 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install ruff
pip install pytest ruff

- name: Lint with ruff
- name: Lint repository for fatal errors
run: |
ruff check --select=E,F,W --ignore=E501,E402 \
main.py \
end_to_end_test.py \
output/builds/src/science/ \
output/builds/src/orbit_propagator.py \
output/builds/src/conjunction_screener.py \
output/builds/src/multi_sensor_fusion.py \
output/builds/src/observation_scheduler.py
ruff check . --select=E9,F63,F7,F82 \
--exclude=output/builds/src/hardware/ascom_hal.py

- name: Run test suite
env:
PYTHONIOENCODING: utf-8
run: pytest -q

- name: Run end-to-end pipeline test
env:
Expand Down Expand Up @@ -68,4 +67,12 @@ jobs:
- name: Verify CMakeLists.txt parses
run: |
cd output/builds/src
cmake -B build -DCMAKE_CUDA_ARCHITECTURES=87 2>&1 || echo "CMake parse check complete (CUDA toolkit not installed — expected)"
if cmake -B build -DCMAKE_CUDA_ARCHITECTURES=87 >cmake.log 2>&1; then
cat cmake.log
elif grep -Eq "Failed to find nvcc|Could not find nvcc|No CMAKE_CUDA_COMPILER" cmake.log; then
cat cmake.log
echo "CMake parse check complete (CUDA toolkit not installed — expected)"
else
cat cmake.log
exit 1
fi
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ fix*.ps1
data/real_frame.fits
data/synthetic_frame.raw
benchmarks/benchmark_results.json

# Environment variables and secrets
.env
.env.*
!.env.example
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ COPY . .
EXPOSE 8001
ENV PYTHONIOENCODING=utf-8

CMD ["uvicorn", "src.api.twin_ws:app", "--host", "0.0.0.0", "--port", "8001"]
ENV PYTHONPATH=/app/output/builds

CMD ["uvicorn", "src.api.twin_ws:app", "--app-dir", "/app/output/builds", "--host", "0.0.0.0", "--port", "8001"]
7 changes: 1 addition & 6 deletions agents/tier4_deploy/docker_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ def generate_dockerfile(task):
-shared -o libsentinel_gpu.so \\
-Xcompiler -fPIC

# Compile C++ drivers
# Compile the available C++ processing modules
RUN g++ -O3 -std=c++17 \\
src/camera_driver.cpp \\
src/mount_control.cpp \\
src/plate_solver.cpp \\
src/photometry.cpp \\
-shared -o libsentinel_hw.so \\
Expand Down Expand Up @@ -179,7 +177,6 @@ def generate_compose(task):
- WATCH_CONTAINER=sentinelforge-edge
- ALERT_ENDPOINT=${{ALERT_ENDPOINT}}
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./logs:/logs

updater:
Expand All @@ -191,8 +188,6 @@ def generate_compose(task):
- IMAGE={DEPLOY_SPEC["image_name"]}
- CHECK_INTERVAL=3600
- AUTO_ROLLBACK=true
volumes:
- /var/run/docker.sock:/var/run/docker.sock
'''

filepath.write_text(code, encoding="utf-8")
Expand Down
33 changes: 33 additions & 0 deletions api/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Vercel Serverless Function — Public Configuration Provider
export default async function handler(req, res) {
const origin = req.headers.origin;
const allowedOrigins = (process.env.ALLOWED_ORIGINS || 'https://sentinelforge.vercel.app')
.split(',')
.map(s => s.trim())
.filter(Boolean);

if (origin) {
if (allowedOrigins.includes(origin)) {
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Vary', 'Origin');
} else {
return res.status(403).json({ error: 'Origin not allowed' });
}
}

res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Cache-Control', 'no-store');

if (req.method === 'OPTIONS') {
return res.status(204).end();
}

if (req.method !== 'GET') {
return res.status(405).json({ error: 'GET only' });
}

return res.status(200).json({
cesiumIonToken: process.env.CESIUM_ION_TOKEN || ''
});
}
143 changes: 127 additions & 16 deletions api/nlp.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,106 @@
// Vercel Serverless Function — Gemini NLU for SentinelForge Terminal
import { timingSafeEqual } from 'node:crypto';

// Best-effort in-memory per-IP rate limit (20 req/min)
const ipHits = new Map();

function checkRateLimit(ip) {
const now = Date.now();
const windowMs = 60 * 1000;
const maxReqs = 20;
const hits = (ipHits.get(ip) || []).filter(t => now - t < windowMs);

if (hits.length >= maxReqs) {
return false;
}
hits.push(now);
ipHits.set(ip, hits);

if (ipHits.size > 2000) {
for (const [k, v] of ipHits.entries()) {
if (v.length === 0 || now - v[v.length - 1] > windowMs) {
ipHits.delete(k);
}
}
}
return true;
}

function validApiKey(provided, expected) {
if (typeof provided !== 'string' || typeof expected !== 'string') {
return false;
}
const providedBytes = Buffer.from(provided, 'utf8');
const expectedBytes = Buffer.from(expected, 'utf8');
return providedBytes.length === expectedBytes.length
&& timingSafeEqual(providedBytes, expectedBytes);
}

const ALLOWED_COMMANDS = new Set([
'ls', 'cat', 'cd', 'pwd', 'tree', 'find', 'head', 'grep', 'wc',
'python', 'pytest', 'git', 'make', 'nano', 'ssh', 'slingshot-pipeline',
'jetson_health', 'df', 'uptime', 'whoami', 'uname', 'date', 'echo',
'clear', 'help', '_reply'
]);

export default async function handler(req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
const origin = req.headers.origin;
const allowedOrigins = (process.env.ALLOWED_ORIGINS || 'https://sentinelforge.vercel.app')
.split(',')
.map(s => s.trim())
.filter(Boolean);

if (origin) {
if (allowedOrigins.includes(origin)) {
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Vary', 'Origin');
} else {
return res.status(403).json({ error: 'Origin not allowed' });
}
}

res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') return res.status(200).end();
if (req.method !== 'POST') return res.status(405).json({ error: 'POST only' });
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, X-API-Key');

const { input } = req.body || {};
if (!input || typeof input !== 'string' || input.length > 500)
if (req.method === 'OPTIONS') {
return res.status(204).end();
}

if (req.method !== 'POST') {
return res.status(405).json({ error: 'POST only' });
}

// Authentication is mandatory and fail-closed when the server is not configured.
const expectedApiKey = process.env.NLP_API_KEY;
if (!expectedApiKey) {
return res.status(503).json({ error: 'Service authentication is not configured' });
}
if (!validApiKey(req.headers['x-api-key'], expectedApiKey)) {
return res.status(401).json({ error: 'Unauthorized' });
}

// Best-effort per-IP rate limiting. Serverless instances do not share memory,
// so this bounds bursts per warm instance rather than providing a global quota.
const ip = req.headers['x-forwarded-for']?.split(',')[0]?.trim() || req.socket?.remoteAddress || 'unknown';
if (!checkRateLimit(ip)) {
return res.status(429).json({ error: 'Too many requests. Please wait a minute.' });
}

// Strict input validation
let input = req.body?.input;
if (!input || typeof input !== 'string') {
return res.status(400).json({ error: 'Invalid input' });
}
// Strip control characters (retain printable characters and whitespace)
input = input.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, '').trim();
if (input.length < 1 || input.length > 500) {
return res.status(400).json({ error: 'Invalid input length' });
}

const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) return res.status(500).json({ error: 'API key not configured' });
if (!apiKey) {
return res.status(500).json({ error: 'API key not configured' });
}

const systemPrompt = `You are a command interpreter for the SentinelForge space surveillance terminal.
The user types natural language. You must respond with ONLY a JSON object — no markdown, no explanation.
Expand Down Expand Up @@ -64,9 +153,10 @@ Examples:
"how's the gpu?" → {"cmd":"jetson_health","args":[],"narr":"Running GPU diagnostics..."}
"hello" → {"cmd":"_reply","text":"Hello! I'm the SentinelForge terminal. Ask me anything about the codebase, or tell me what to run."}`;

// Strict fixed model allow-list (cannot be used as an open proxy)
const models = ['gemini-2.5-flash', 'gemini-2.5-flash-lite', 'gemini-2.0-flash-lite'];
const reqBody = JSON.stringify({
contents: [{ parts: [{ text: `${systemPrompt}\n\nUser input: "${input}"` }] }],
contents: [{ parts: [{ text: `${systemPrompt}\n\nUser input: ${JSON.stringify(input)}` }] }],
generationConfig: {
temperature: 0.1,
maxOutputTokens: 256,
Expand All @@ -76,33 +166,54 @@ Examples:

for (const model of models) {
try {
// Pass API key via header, NEVER in URL query params
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`,
{ method: 'POST', headers: { 'Content-Type': 'application/json' }, body: reqBody }
`https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-goog-api-key': apiKey
},
body: reqBody
}
);

if (response.status === 503 || response.status === 429) {
console.log(`${model}: ${response.status}, trying next model...`);
continue; // Try next model
}

if (!response.ok) {
const errText = await response.text();
console.error(`${model} error:`, errText);
// Do not leak upstream error body to client
continue;
}

const data = await response.json();
const text = data?.candidates?.[0]?.content?.parts?.[0]?.text;
if (!text) continue;

const parsed = JSON.parse(text);
let parsed;
try {
parsed = JSON.parse(text);
} catch {
continue;
}

if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
return res.status(200).json({ cmd: '_reply', text: 'Command not recognized.' });
}

// Validate command is from allowed set
if (typeof parsed.cmd !== 'string' || !ALLOWED_COMMANDS.has(parsed.cmd)) {
return res.status(200).json({ cmd: '_reply', text: 'Command not recognized.' });
}

return res.status(200).json(parsed);
} catch (err) {
console.error(`${model} failed:`, err.message);
} catch {
continue;
}
}

// Generic failure response without leaking internal errors
return res.status(502).json({ error: 'All models unavailable. Try again in a moment.' });
}
Loading
Loading