broadfield-dev commited on
Commit
e34feda
·
verified ·
1 Parent(s): be9835b

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +35 -7
index.js CHANGED
@@ -6,15 +6,38 @@ const { decodeFromImageBuffer } = require('./decoder'); // Import our logic
6
  const app = express();
7
  const PORT = process.env.PORT || 7860;
8
 
9
- // Looser CORS for testing, tighten this in a real production environment
10
- app.use(cors());
11
 
12
- // --- 1. HEALTH CHECK ENDPOINT ---
13
- // You can visit this in your browser to see if the server is running.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  app.get('/', (req, res) => {
15
  res.status(200).json({
16
  status: 'ok',
17
- message: 'Secure Decoder API is running.'
 
 
18
  });
19
  });
20
 
@@ -23,9 +46,9 @@ const upload = multer({
23
  limits: { fileSize: 5 * 1024 * 1024 }, // 5MB limit
24
  });
25
 
26
- // --- 2. DECODE API ENDPOINT ---
27
  app.post('/api/decode', upload.single('authImage'), async (req, res) => {
28
- console.log("Received a request to /api/decode");
29
 
30
  const privateKey = process.env.PLUGIN_PRIVATE_KEY;
31
  if (!privateKey) {
@@ -49,6 +72,11 @@ app.post('/api/decode', upload.single('authImage'), async (req, res) => {
49
 
50
  app.listen(PORT, () => {
51
  console.log(`Secure decoder API listening on port ${PORT}`);
 
 
 
 
 
52
  if (!process.env.PLUGIN_PRIVATE_KEY) {
53
  console.warn("WARNING: PLUGIN_PRIVATE_KEY environment variable is not set. The /api/decode endpoint will fail.");
54
  } else {
 
6
  const app = express();
7
  const PORT = process.env.PORT || 7860;
8
 
9
+ // --- 1. SECURE CORS CONFIGURATION ---
 
10
 
11
+ // Define the list of allowed client origins from an environment variable.
12
+ // This allows you to change the allowed origins without changing the code.
13
+ // The variable should be a comma-separated list of URLs.
14
+ const allowedOrigins = process.env.ALLOWED_ORIGINS ? process.env.ALLOWED_ORIGINS.split(',') : [];
15
+
16
+ const corsOptions = {
17
+ origin: (origin, callback) => {
18
+ // 'origin' will be undefined for server-to-server requests or curl.
19
+ // Allow requests with no origin OR if the origin is in our whitelist.
20
+ if (!origin || allowedOrigins.indexOf(origin) !== -1) {
21
+ callback(null, true);
22
+ } else {
23
+ // If the origin is not in the whitelist, reject the request.
24
+ callback(new Error(`Not allowed by CORS. Origin: ${origin}`));
25
+ }
26
+ },
27
+ methods: ['GET', 'POST'], // Allow only specific methods
28
+ };
29
+
30
+ // Use the configured CORS middleware
31
+ app.use(cors(corsOptions));
32
+
33
+
34
+ // --- 2. HEALTH CHECK ENDPOINT ---
35
  app.get('/', (req, res) => {
36
  res.status(200).json({
37
  status: 'ok',
38
+ message: 'Secure Decoder API is running.',
39
+ // Also report which origins are allowed, for easy debugging.
40
+ allowed_origins: allowedOrigins.length > 0 ? allowedOrigins : "None configured (check ALLOWED_ORIGINS secret)."
41
  });
42
  });
43
 
 
46
  limits: { fileSize: 5 * 1024 * 1024 }, // 5MB limit
47
  });
48
 
49
+ // --- 3. DECODE API ENDPOINT ---
50
  app.post('/api/decode', upload.single('authImage'), async (req, res) => {
51
+ console.log(`Received a request to /api/decode from origin: ${req.get('origin') || 'unknown'}`);
52
 
53
  const privateKey = process.env.PLUGIN_PRIVATE_KEY;
54
  if (!privateKey) {
 
72
 
73
  app.listen(PORT, () => {
74
  console.log(`Secure decoder API listening on port ${PORT}`);
75
+ if (allowedOrigins.length > 0) {
76
+ console.log(`CORS whitelist enabled for the following origins: ${allowedOrigins.join(', ')}`);
77
+ } else {
78
+ console.warn("WARNING: No CORS origins are whitelisted. Set the ALLOWED_ORIGINS secret. All cross-origin requests will be blocked.");
79
+ }
80
  if (!process.env.PLUGIN_PRIVATE_KEY) {
81
  console.warn("WARNING: PLUGIN_PRIVATE_KEY environment variable is not set. The /api/decode endpoint will fail.");
82
  } else {