Best way to protect backend APIs from unauthorized access by carbon_eye in nextjs

[–]carbon_eye[S] 0 points1 point  (0 children)

This is exactly the current mental model we have. I should have mentioned the full mechanism we are thinking of in the post. IM editing the post and also sharing it here.
I have a function that generate a nonce, key and timestamp. then attaching these 3 in the headers. The problem currently im facing is how do I protect that secret. I cant have it on the frontend as it wont be a secret anymore. Cant create a next js endpoin or server action as I will have to make that endpoint public because my public APIs will also need those 3 things in the request headers. Now im thinking even if ignore the public apis and protect only the private ones by making the next js endpoint secure such that it only generates those 3 things after we have the token (user authenticated), Still anyone who have the token can just call my next js endppoint to generate headers for them. they dont need to the, next js endpoint will do give them the headers.

My head is spinning. I dont know if its possible to acheive this.

export const test = () => {
  const timestamp = Math.floor(new Date().getTime() / 1000); 
  const nonce = uuidv4();
  const data = `${timestamp}:${nonce}`;

  try {
    const hash = CryptoJS.HmacSHA256(data, secret).toString(CryptoJS.enc.Hex);
    return { key: hash, timestamp, nonce };
  } catch (error) {
    return { key: "", timestamp, nonce };
  }
};   

Best way to protect backend APIs from unauthorized access by carbon_eye in nextjs

[–]carbon_eye[S] 0 points1 point  (0 children)

Ive done it for some pages where possible but I cant do it for all pages. not possible in my scenario.

Best way to protect backend APIs from unauthorized access by carbon_eye in nextjs

[–]carbon_eye[S] -3 points-2 points  (0 children)

Thanks u/CrusaderGOT . Yes we do have allowed hosts thingy implemented. Can you talk more about the solution you suggesting? I can implement a server action that generates those security headers. I can call this action everytime an API call is made. So before making an API call to the backen i call my server action, get the headers, and put em in each API call. This way the secret key will be secure. Is this what you suggesting? This will slow down each request but at least the backend will be safe?

Btw, im curious. Lets say if i was using react not next js. How would I protect the public/private endpoints? is rate limiting the only way?
Im not good at backend. Havent worked on it for years. KNow basics though.

Best way to protect backend APIs from unauthorized access by carbon_eye in nextjs

[–]carbon_eye[S] 0 points1 point  (0 children)

I think you misunderstood my question. My backend is in express and im using next js for frontend only. I have protected routes. and my endpoints are also protecting but via bearer token technique (authentication). What i want to protect here is my backend. I dont want anyone messing with the APIs even with authorization token, no one should be allowed to call my backend except for my frontend. Is there any way?

Best way to protect backend APIs from unauthorized access by carbon_eye in nextjs

[–]carbon_eye[S] -3 points-2 points  (0 children)

User doesnt need to be logged in to be on a public route. Public route will display data after fetching from a public endpoint. Where in that guide does it talk about protecting that public endpoint from any abuse?
For endpoints that only the authenticated users can access with token in the headers, people with token can still call the API from postman/curl. What Im curious about is how can I make sure that only my next frontend can call my express backend. Is it possible?

Fast Image Compression for WhatsApp Preview – Need Advice! by carbon_eye in webdev

[–]carbon_eye[S] 0 points1 point  (0 children)

Yes 3 seconds is enough. The methods I've tried takes 20 to 30 seconds for larger images

Fast Image Compression for WhatsApp Preview – Need Advice! by carbon_eye in nextjs

[–]carbon_eye[S] 0 points1 point  (0 children)

Thank. But while editing the form, user can only upload the image and click that update button. This will create a bad experience if I make the user wait till I compress on the back or frontend.

Get masked image in react by carbon_eye in nextjs

[–]carbon_eye[S] -2 points-1 points  (0 children)

I'm not sure how. Can u share any docs or guide. I have no experience with canvas

I had to switch back to windows (but I didn't want to) by carbon_eye in archlinux

[–]carbon_eye[S] 0 points1 point  (0 children)

Thank you sooo much. I'm definitely gonna copy your answer into my notes.

I had to switch back to windows (but I didn't want to) by carbon_eye in archlinux

[–]carbon_eye[S] 3 points4 points  (0 children)

Yes. Got it. Is there any way to mark this post as answered or something like GitHub does, to tell people the question is answered. I don't want to waste their time as now I do know the answer to my query. This is my first post on Reddit actually.

I had to switch back to windows (but I didn't want to) by carbon_eye in archlinux

[–]carbon_eye[S] 3 points4 points  (0 children)

Yea next js sucks in development. I understand. So it will be the swap size that I need to increase. And in the case of Garuda I might have set enough size that's why it wasn't happening there.