0d3cb498a3
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
92 lines
2.9 KiB
JavaScript
92 lines
2.9 KiB
JavaScript
const express = require('express');
|
|
const crypto = require('crypto');
|
|
const fs = require('fs');
|
|
const rateLimit = require('express-rate-limit');
|
|
|
|
const app = express();
|
|
app.use(express.json());
|
|
|
|
const chatRateLimiter = rateLimit({
|
|
windowMs: 60 * 1000,
|
|
max: 30,
|
|
standardHeaders: true,
|
|
legacyHeaders: false,
|
|
});
|
|
|
|
// Add signature validation configuration
|
|
const SIGNATURE_CONFIG = {
|
|
publicKeyPath: './public_key.pem',
|
|
signatureHeader: 'signature',
|
|
timestampHeader: 'timestamp',
|
|
clientIdHeader: 'client-id',
|
|
signatureValidityMs: 300000, // 5 minutes
|
|
signatureDataTemplate: 'promptfoo-app{{timestamp}}',
|
|
signatureAlgorithm: 'SHA256',
|
|
};
|
|
|
|
// Signature validation middleware
|
|
function validateSignature(req, res, next) {
|
|
try {
|
|
const signature = req.headers[SIGNATURE_CONFIG.signatureHeader];
|
|
const timestamp = req.headers[SIGNATURE_CONFIG.timestampHeader];
|
|
const clientId = req.headers[SIGNATURE_CONFIG.clientIdHeader];
|
|
|
|
// Check if all required headers are present
|
|
if (!signature || !timestamp || !clientId) {
|
|
console.warn('Request rejected: Missing signature headers');
|
|
return res.status(401).json({ error: 'Missing signature headers' });
|
|
}
|
|
|
|
// Check timestamp validity
|
|
const now = Date.now();
|
|
const requestTime = Number.parseInt(timestamp, 10);
|
|
|
|
if (Number.isNaN(requestTime) || now - requestTime > SIGNATURE_CONFIG.signatureValidityMs) {
|
|
console.warn('Request rejected: Signature expired or invalid timestamp');
|
|
return res.status(401).json({ error: 'Signature expired or invalid timestamp' });
|
|
}
|
|
|
|
// Generate signature data using the template
|
|
const signatureData = SIGNATURE_CONFIG.signatureDataTemplate.replace(
|
|
'{{timestamp}}',
|
|
timestamp,
|
|
);
|
|
|
|
// Verify signature
|
|
const publicKey = fs.readFileSync(SIGNATURE_CONFIG.publicKeyPath, 'utf8');
|
|
const verify = crypto.createVerify(SIGNATURE_CONFIG.signatureAlgorithm);
|
|
verify.update(signatureData);
|
|
const isValid = verify.verify(publicKey, signature, 'base64');
|
|
|
|
if (!isValid) {
|
|
console.warn('Request rejected: Invalid signature');
|
|
return res.status(401).json({ error: 'Invalid signature' });
|
|
}
|
|
|
|
console.log('Signature checks out... continuing');
|
|
next();
|
|
} catch (error) {
|
|
console.error('Error validating signature:', error);
|
|
return res.status(500).json({ error: 'Error validating signature' });
|
|
}
|
|
}
|
|
|
|
app.post('/chat', chatRateLimiter, validateSignature, async (req, res) => {
|
|
try {
|
|
return res.json({ message: 'hello' });
|
|
} catch (error) {
|
|
console.error('Error processing chat request:', error);
|
|
return res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
const PORT = process.env.PORT || 2345;
|
|
app.listen(PORT, (error) => {
|
|
if (error) {
|
|
console.error(`Failed to start server: ${error.message}`);
|
|
process.exit(1);
|
|
return;
|
|
}
|
|
console.info(`Server is running on port ${PORT}`);
|
|
});
|