tractatus/scripts/fix-csp-major-html.js
TheFlow 0e6be3eaf1 refactor: remove website code and fix critical startup crashes (Phase 8)
CRITICAL FIX: Server would CRASH ON STARTUP (multiple import errors)

REMOVED (2 scripts):
1. scripts/framework-watchdog.js
   - Monitored .claude/session-state.json (OUR Claude Code setup)
   - Monitored .claude/token-checkpoints.json (OUR file structure)
   - Implementers won't have our .claude/ directory

2. scripts/init-db.js
   - Created website collections: blog_posts, media_inquiries, case_submissions
   - Created website collections: resources, moderation_queue, users, citations
   - Created website collections: translations, koha_donations
   - Next steps referenced deleted scripts (npm run seed:admin)

REWRITTEN (2 files):

src/models/index.js (29 lines → 27 lines)
- REMOVED imports: Document, BlogPost, MediaInquiry, CaseSubmission, Resource
- REMOVED imports: ModerationQueue, User (all deleted in Phase 2)
- KEPT imports: AuditLog, DeliberationSession, GovernanceLog, GovernanceRule
- KEPT imports: Precedent, Project, SessionState, VariableValue, VerificationLog
- Result: Only framework models exported

src/server.js (284 lines → 163 lines, 43% reduction)
- REMOVED: Imports to deleted middleware (csrf-protection, response-sanitization)
- REMOVED: Stripe webhook handling (/api/koha/webhook)
- REMOVED: Static file caching (for deleted public/ directory)
- REMOVED: Static file serving (public/ deleted in Phase 6)
- REMOVED: CSRF token endpoint
- REMOVED: Website homepage with "auth, documents, blog, admin" references
- REMOVED: Instruction sync (scripts/sync-instructions-to-db.js reference)
- REMOVED: Hardcoded log path (${process.env.HOME}/var/log/tractatus/...)
- REMOVED: Website-specific security middleware
- KEPT: Security headers, rate limiting, CORS, body parsers
- KEPT: API routes, governance services, MongoDB connections
- RESULT: Clean framework-only server

RESULT: Repository can now start without crashes, all imports resolve

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 22:17:02 +13:00

97 lines
4.9 KiB
JavaScript

#!/usr/bin/env node
/**
* Fix CSP violations in major HTML files (index.html, architecture.html)
*/
const fs = require('fs');
const path = require('path');
const replacements = [
// Layout
{ from: ' style="min-height: 64px;"', to: ' class="min-h-16"' },
// Text shadows
{ from: ' style="text-shadow: 0 2px 4px rgba(0,0,0,0.1);"', to: ' class="text-shadow-md"' },
{ from: ' style="text-shadow: 0 1px 2px rgba(0,0,0,0.1);"', to: ' class="text-shadow-sm"' },
// Gradients
{ from: ' style="background: linear-gradient(135deg, #64ffda 0%, #448aff 50%, #0ea5e9 100%);"', to: ' class="bg-gradient-tractatus"' },
{ from: ' style="background: var(--gradient-btn-validator);"', to: ' class="bg-gradient-service-validator"' },
{ from: ' style="background: var(--gradient-btn-instruction);"', to: ' class="bg-gradient-service-instruction"' },
{ from: ' style="background: var(--gradient-btn-deliberation);"', to: ' class="bg-gradient-service-deliberation"' },
{ from: ' style="background: linear-gradient(135deg, #10b981 0%, #059669 100%);"', to: ' class="bg-gradient-service-boundary"' },
{ from: ' style="background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);"', to: ' class="bg-gradient-service-instruction"' },
{ from: ' style="background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%);"', to: ' class="bg-gradient-service-validator"' },
{ from: ' style="background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);"', to: ' class="bg-gradient-service-pressure"' },
{ from: ' style="background: linear-gradient(135deg, #ec4899 0%, #db2777 100%);"', to: ' class="bg-gradient-service-metacognitive"' },
{ from: ' style="background: linear-gradient(135deg, #14b8a6 0%, #0d9488 100%);"', to: ' class="bg-gradient-service-deliberation"' },
// Border colors
{ from: ' style="border-left-color: var(--service-validator-light);"', to: ' class="border-l-service-validator"' },
{ from: ' style="border-left-color: var(--service-instruction-light);"', to: ' class="border-l-service-instruction"' },
{ from: ' style="border-left-color: var(--service-deliberation-light);"', to: ' class="border-l-service-deliberation"' },
{ from: ' style="border-left-color: #10b981;"', to: ' class="border-l-service-boundary"' },
{ from: ' style="border-left-color: #6366f1;"', to: ' class="border-l-service-instruction"' },
{ from: ' style="border-left-color: #8b5cf6;"', to: ' class="border-l-service-validator"' },
{ from: ' style="border-left-color: #f59e0b;"', to: ' class="border-l-service-pressure"' },
{ from: ' style="border-left-color: #ec4899;"', to: ' class="border-l-service-metacognitive"' },
{ from: ' style="border-left-color: #14b8a6;"', to: ' class="border-l-service-deliberation"' },
// Text colors
{ from: ' style="color: var(--tractatus-core-end);"', to: ' class="text-tractatus-link"' },
{ from: ' style="color: var(--service-validator-light);"', to: ' class="text-service-validator"' },
{ from: ' style="color: var(--service-instruction-light);"', to: ' class="text-service-instruction"' },
{ from: ' style="color: var(--service-deliberation-light);"', to: ' class="text-service-deliberation"' },
{ from: ' style="color: var(--service-validator-dark);"', to: ' class="text-service-validator"' },
{ from: ' style="color: var(--service-instruction-dark);"', to: ' class="text-service-instruction"' },
{ from: ' style="color: var(--service-deliberation-dark);"', to: ' class="text-service-deliberation"' },
// Badges
{ from: ' style="color: #065f46; background-color: #d1fae5;"', to: ' class="badge-boundary"' },
{ from: ' style="color: #3730a3; background-color: #e0e7ff;"', to: ' class="badge-instruction"' },
{ from: ' style="color: #581c87; background-color: #f3e8ff;"', to: ' class="badge-validator"' },
{ from: ' style="color: #92400e; background-color: #fef3c7;"', to: ' class="badge-pressure"' },
{ from: ' style="color: #831843; background-color: #fce7f3;"', to: ' class="badge-metacognitive"' },
{ from: ' style="color: #134e4a; background-color: #ccfbf1;"', to: ' class="badge-deliberation"' }
];
const files = [
'public/index.html',
'public/architecture.html'
];
let totalFixed = 0;
files.forEach(file => {
const filePath = path.join(__dirname, '..', file);
console.log(`\nProcessing ${file}...`);
if (!fs.existsSync(filePath)) {
console.log(` ✗ File not found`);
return;
}
let content = fs.readFileSync(filePath, 'utf8');
let fileFixed = 0;
replacements.forEach(({ from, to }) => {
const occurrences = (content.match(new RegExp(from.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g')) || []).length;
if (occurrences > 0) {
content = content.split(from).join(to);
fileFixed += occurrences;
}
});
if (fileFixed > 0) {
fs.writeFileSync(filePath, content);
totalFixed += fileFixed;
console.log(` ✓ Fixed ${fileFixed} violation(s)`);
} else {
console.log(` • No violations found`);
}
});
console.log(`\n✓ Total fixes applied: ${totalFixed}\n`);