9174331d2d
Newsletter nudge — was calling /api/study-account/profile (wrong endpoint, ignored subscription). Now correctly calls /api/study-account/preferences with PATCH. Security: Email regex — replaced the permissive [^\s@]+@[^\s@]+ pattern with a proper RFC-compliant regex in contact.js and downloads.js Avatar magic bytes — server now checks actual PNG/JPEG/GIF/WEBP header bytes, not just the data URL prefix Certificate rate limit — public /api/public/certificate/:token now has a 30 req/15min limiter Session absolute TTL — admin sessions now have a 30-day hard cap; a stolen token can no longer be kept alive indefinitely by passive reads Account lockout — 5 failed logins locks a study account for 1 hour CSP headers — Content-Security-Policy, X-Frame-Options, X-Content-Type-Options, and Referrer-Policy headers added globally Data integrity: Cascade delete — deleting a study account now also removes their certificates, community posts, comments, and progress file UX / reliability: Escape key on modals — all 3 modal groups (study index, notes, account) now close on Escape Display name min-length — empty spaces-only names rejected; if provided, must be ≥2 chars Note save rate limit — 30 saves/minute per user max Analytics fetch timeout — 5s AbortController so a hanging server doesn't block the browser indefinitely Email validation on signup — frontend catches bad email formats before hitting the server Cleanup: Deduplicated download forms — StudyDownloadForm and ResourceDownloadForm now share a single DownloadForm base; both are now thin wrappers
125 lines
4.6 KiB
JavaScript
125 lines
4.6 KiB
JavaScript
import { randomUUID } from 'node:crypto'
|
|
import rateLimit from 'express-rate-limit'
|
|
import { requireAdminAuth } from '../auth.js'
|
|
import { requireStudyAuth, normalizeStudySlug, isStudyUserEnrolled, getStudyTitleBySlug } from '../study-helpers.js'
|
|
import { state } from '../state.js'
|
|
import { loadUserProgress, queueStudyCertificatesWrite } from '../data.js'
|
|
|
|
const publicCertRateLimiter = rateLimit({
|
|
windowMs: 15 * 60 * 1000,
|
|
max: 30,
|
|
standardHeaders: true,
|
|
legacyHeaders: false,
|
|
message: { message: 'Too many certificate lookups. Please wait a moment.' },
|
|
})
|
|
|
|
/**
|
|
* Returns the list of required section IDs for a study (only released sections).
|
|
*/
|
|
function getRequiredSectionIds(studySlug) {
|
|
const content = state.cachedSiteContent
|
|
if (!content || !Array.isArray(content.studies)) return null
|
|
const study = content.studies.find(s => s?.slug === studySlug)
|
|
if (!study || !Array.isArray(study.sections)) return null
|
|
return study.sections
|
|
.filter(s => s?.status !== 'unreleased' && s?.id)
|
|
.map(s => s.id)
|
|
}
|
|
|
|
export function register(app) {
|
|
// GET — check eligibility and return existing certificate token if one exists
|
|
app.get('/api/study-certificate/:studySlug', requireStudyAuth, async (req, res) => {
|
|
const user = req.studyUser
|
|
const studySlug = normalizeStudySlug(req.params.studySlug)
|
|
if (!studySlug) { res.status(400).json({ message: 'Invalid study slug.' }); return }
|
|
|
|
if (!isStudyUserEnrolled(user, studySlug)) {
|
|
res.status(403).json({ message: 'Not enrolled in this study.' }); return
|
|
}
|
|
|
|
const requiredIds = getRequiredSectionIds(studySlug)
|
|
if (!requiredIds || requiredIds.length === 0) {
|
|
res.status(404).json({ message: 'Study sections not found.' }); return
|
|
}
|
|
|
|
const progress = await loadUserProgress(user.id)
|
|
const completedIds = progress.byStudy[studySlug]?.completedSectionIds ?? []
|
|
const eligible = requiredIds.every(id => completedIds.includes(id))
|
|
|
|
const existing = state.studyCertificates.find(c => c.userId === user.id && c.studySlug === studySlug)
|
|
|
|
res.json({
|
|
eligible,
|
|
studyTitle: getStudyTitleBySlug(studySlug) || studySlug,
|
|
completedCount: completedIds.filter(id => requiredIds.includes(id)).length,
|
|
totalCount: requiredIds.length,
|
|
token: existing?.token ?? null,
|
|
issuedAt: existing?.issuedAt ?? null,
|
|
})
|
|
})
|
|
|
|
// POST — issue/re-issue a certificate (must be eligible)
|
|
app.post('/api/study-certificate/:studySlug', requireStudyAuth, async (req, res) => {
|
|
const user = req.studyUser
|
|
const studySlug = normalizeStudySlug(req.params.studySlug)
|
|
if (!studySlug) { res.status(400).json({ message: 'Invalid study slug.' }); return }
|
|
|
|
if (!isStudyUserEnrolled(user, studySlug)) {
|
|
res.status(403).json({ message: 'Not enrolled in this study.' }); return
|
|
}
|
|
|
|
const requiredIds = getRequiredSectionIds(studySlug)
|
|
if (!requiredIds || requiredIds.length === 0) {
|
|
res.status(404).json({ message: 'Study sections not found.' }); return
|
|
}
|
|
|
|
const progress = await loadUserProgress(user.id)
|
|
const completedIds = progress.byStudy[studySlug]?.completedSectionIds ?? []
|
|
const eligible = requiredIds.every(id => completedIds.includes(id))
|
|
|
|
if (!eligible) {
|
|
res.status(403).json({ message: 'Complete all sections to earn your certificate.' }); return
|
|
}
|
|
|
|
// Find or create
|
|
let cert = state.studyCertificates.find(c => c.userId === user.id && c.studySlug === studySlug)
|
|
if (!cert) {
|
|
cert = {
|
|
id: randomUUID(),
|
|
token: randomUUID(),
|
|
userId: user.id,
|
|
studySlug,
|
|
studyTitle: getStudyTitleBySlug(studySlug) || studySlug,
|
|
displayName: user.displayName || user.username,
|
|
issuedAt: new Date().toISOString(),
|
|
}
|
|
state.studyCertificates.push(cert)
|
|
queueStudyCertificatesWrite()
|
|
}
|
|
|
|
res.json({ ok: true, token: cert.token, issuedAt: cert.issuedAt })
|
|
})
|
|
|
|
// GET — public certificate page data (no auth, by token)
|
|
app.get('/api/public/certificate/:token', publicCertRateLimiter, (req, res) => {
|
|
const { token } = req.params
|
|
if (!token || typeof token !== 'string' || token.length > 100) {
|
|
res.status(400).json({ message: 'Invalid token.' }); return
|
|
}
|
|
|
|
const cert = state.studyCertificates.find(c => c.token === token)
|
|
if (!cert) { res.status(404).json({ message: 'Certificate not found.' }); return }
|
|
|
|
res.json({
|
|
studyTitle: cert.studyTitle,
|
|
displayName: cert.displayName,
|
|
issuedAt: cert.issuedAt,
|
|
})
|
|
})
|
|
|
|
// Admin: list all certificates
|
|
app.get('/api/admin-study-certificates', requireAdminAuth, (_req, res) => {
|
|
res.json({ certificates: state.studyCertificates })
|
|
})
|
|
}
|