Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
Is it safe to export user cookie from the server side and use it as an "API key" alternative?

First some context: Here's a typical authentication scenario:

  1. An express.js server with signed cookies support
  2. User logs in from the frontend and the backend sets the cookie in the response
  3. This cookie will be set on the user's browser
  4. From this point, whenever the user makes a request from the browser, the cookie will be attached to req.headers.cookie

Now that we've set the context, here's what I want to do: I want to build an API feature to the server, but instead of adding additional API key generation and management logic, I would like to use the existing cookie.

Basically I want to do this:

  1. User goes to an "API key" page, which will send a request to the server, and the server will be able to get the cookie from this very request, through the req.headers.cookie attribute.
  2. The server then renders the req.headers.cookie back to the user as a response
  3. The user takes that cookie string from the API key page (which is technically also accessible inside their browser "application" tab but I'm just making it easier so the user doesn't need to open the browser console) and uses it as an "API key" alternative.

For example, here's a sample route handler for the API Key page:

app.get("/api_keys", authenticate, (req, res) => {
  res.render("api_keys", {
    cookie: req.headers.cookie
  })
})

This will display the cookie string on the /api_keys page.

Then the user may be able to copy and paste the full cookie string into the following code:

fetch(API_URL, {
  headers: { cookie: <PASTE_COOKIE_HERE> }
}).then((response) => {
  console.log(response)
})

and it should work. And it does (I just checked).

My question is: Is this OK to secure APIs this way?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda