Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

app.js 2.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  1. const express = require('express');
  2. const { providers } = require('promptfoo');
  3. const crypto = require('crypto');
  4. const fs = require('fs');
  5. const app = express();
  6. app.use(express.json());
  7. // Add signature validation configuration
  8. const SIGNATURE_CONFIG = {
  9. publicKeyPath: './public_key.pem',
  10. signatureHeader: 'signature',
  11. timestampHeader: 'timestamp',
  12. clientIdHeader: 'client-id',
  13. signatureValidityMs: 300000, // 5 minutes
  14. signatureDataTemplate: 'promptfoo-app{{timestamp}}',
  15. signatureAlgorithm: 'SHA256',
  16. };
  17. // Signature validation middleware
  18. function validateSignature(req, res, next) {
  19. try {
  20. const signature = req.headers[SIGNATURE_CONFIG.signatureHeader];
  21. const timestamp = req.headers[SIGNATURE_CONFIG.timestampHeader];
  22. const clientId = req.headers[SIGNATURE_CONFIG.clientIdHeader];
  23. // Check if all required headers are present
  24. if (!signature || !timestamp || !clientId) {
  25. console.warn('Request rejected: Missing signature headers');
  26. return res.status(401).json({ error: 'Missing signature headers' });
  27. }
  28. // Check timestamp validity
  29. const now = Date.now();
  30. const requestTime = Number.parseInt(timestamp, 10);
  31. if (Number.isNaN(requestTime) || now - requestTime > SIGNATURE_CONFIG.signatureValidityMs) {
  32. console.warn('Request rejected: Signature expired or invalid timestamp');
  33. return res.status(401).json({ error: 'Signature expired or invalid timestamp' });
  34. }
  35. // Generate signature data using the template
  36. const signatureData = SIGNATURE_CONFIG.signatureDataTemplate.replace(
  37. '{{timestamp}}',
  38. timestamp,
  39. );
  40. // Verify signature
  41. const publicKey = fs.readFileSync(SIGNATURE_CONFIG.publicKeyPath, 'utf8');
  42. const verify = crypto.createVerify(SIGNATURE_CONFIG.signatureAlgorithm);
  43. verify.update(signatureData);
  44. const isValid = verify.verify(publicKey, signature, 'base64');
  45. if (!isValid) {
  46. console.warn('Request rejected: Invalid signature');
  47. return res.status(401).json({ error: 'Invalid signature' });
  48. }
  49. console.log('Signature checks out... continuing');
  50. next();
  51. } catch (error) {
  52. console.error('Error validating signature:', error);
  53. return res.status(500).json({ error: 'Error validating signature' });
  54. }
  55. }
  56. app.post('/chat', validateSignature, async (req, res) => {
  57. try {
  58. return res.json({ message: 'hello' });
  59. } catch (error) {
  60. console.error('Error processing chat request:', error);
  61. return res.status(500).json({ error: error.message });
  62. }
  63. });
  64. const PORT = process.env.PORT || 2345;
  65. app.listen(PORT, () => {
  66. console.info(`Server is running on port ${PORT}`);
  67. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...