Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
How to allow access by headers > user-agent

I need to allow requests from some origins or all requests if they are from a specific user-agent.

...
var whitelist = [URL1, URL2];
  corsOptions = {
    origin: function (origin, callback) {
      if (whitelist.indexOf(origin) !== -1 || origin.req.header['user-agent'] === 'SpecificUserAgent') {
        callback(null, true)
      } else {
        callback(new Error('Not allowed by CORS'))
      }
    }
  };
...

Being origin.req.header['user-agent'] === 'SpecificUserAgent' just a sample what I need.

Is there any way to do it? Tanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could write your own middleware on top of all the routes you'd like to protect, and parse user-agents just like you did.

However this method gives no safety at all as a user-agent can be spoofed easily.

app.use((req, res, next) => {
  if(!req.headers['user-agent'].match(/firefox/gi))
    return res.status(403).send({error: "Unauthorized browser"})
  
  next()
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!