Next.js Discord

Discord Forum

Preflight on Serverside

Answered
B33fb0n3 posted this in #help-forum
Open in Discord
Avatar
Hey, when the browser makes a request to an external endpoint the browser automatically creates the preflight (see attached). Only with that I can get the external data.

When I am doing the fetch now serverside, there is no browser that can send this preflight, so I do it myself (copied the "preflight") and used it inside before the fetch. But then the preflight will result in an error: https://pastebin.com/tzpn4FWE

Why and how to resolve it?
Image
Image
Answered by joulev
let's just put it this way. your server is probably in a data centre which means it shares the same subnet with many other servers, on which the malicious requests are made. hence discord blocks the ip range. your personal ip address is not part of any blocked ip ranges, but a server in us-east for example is likely to fall inside the blocked ip range. this is a pretty common anti-spam tactic.
View full answer

44 Replies

Avatar
@James4u Why would you need preflight when you make a fetch in the server side?
Avatar
The external API is configured so that it only returns data if I have previously sent a preflight. If I send a request (from my nextjs server) without the preflight, the request is rejected due to CORS.
The (blocked) result itself looks like this: https://pastebin.com/FeptVEtD

However: it's a publically accessable API (as you saw in my screenshots -> I got status 200 instead of 403)
Avatar
@James4u well, If I am not wrong, public APIs wouldn't require preflights - I assume the external API is explicitly designed to only accept requests that mimic browser behavior
Avatar
hmm if a server can be configured like that, that could be a simple explaination.. 🤔
How to mimic browser behavior from the server?
Avatar
@James4u How to mimic browser behavior from the server?
Avatar
@B33fb0n3 <@970430299014041630> How to mimic browser behavior from the server?
Avatar
well actually I also meant the samething you did to make a preflight
but if you copy pasted and it didn't work, I have no clue
Avatar
@James4u well actually I also meant the samething you did to make a preflight
Avatar
Yea, inside the browser that happens automatically.

When running the app locally everything works like expected. So localhost seems to be allowed. I thought about pushing that to a server to have it be localhost, but when I make my server available then it won’t be localhost anymore, right?
Avatar
@James4u it won't be
Avatar
yea.. I hate that...
Avatar
Forest yellowjacket
What is the API? Do you have the docs?
Avatar
@joulev server side requests never get rejected due to CORS. CORS is a browser-only thing.
Avatar
hmm ok. Why are my server side requests are rejected with 403 and when using my browser with the same details the request runs thought with status 200?
Avatar
@B33fb0n3 hmm ok. Why are my server side requests are rejected with 403 and when using my browser with the same details the request runs thought with status 200?
Avatar
403 is not cors – cors are request failures without a status.

extremely likely you don't have the necessary cookies attached to the request.
unless the 403 response has the json saying "no preflight" in which case i'll need more information, starting with what backend is this.
Avatar
@joulev 403 is not cors – cors are request failures without a status. extremely likely you don't have the necessary cookies attached to the request.
Avatar
I checked the request again to make sure all necessary cookies are attached and it looks like the correct ones are attached (when I copy the request parameters to my browser it works fine).

Also when I am on localhost and sending it though the server the request will also respond with data (status 200). Once deployed the requests resulting in 403 errors

The backend is the discord backend. The api is not officially documented from discord. However the community made their own docs: https://github.com/discord-userdoccers/discord-userdoccers?tab=readme-ov-file#discord-unofficial-user-api-documentation
Avatar
Forest yellowjacket
What is the specific doc you are reading to use the profile endpoint?
Tried to find, but could not find it
Avatar
Forest yellowjacket
This may be completely stupid, but your request is to a /profile?
Avatar
@Forest yellowjacket This may be completely stupid, but your request is to a /profile?
Avatar
yes, I am requesting to /users/${userId}/profile
Avatar
it does not make sense to send preflights from nodejs because preflight/cors is only applicable in the browser context
check for that to make sure u dont exceed them
Image
Image
if u fetch on the server, all headers that are usually send by a browser will not be included
so cloudflare or even discord probably is checking for that and maybe interpreting your request in a more strict way
u can always change headers on the server to mimic browser behaviour
so basically that will bypass any endpoint that doesnt require any keys and only works using the headers
u can automate this using puppeteer to request the endpoint, get the response cookies or whatever, and use that further for your serverside requests
and then just renew them once the cookie is invalid again
Avatar
@B33fb0n3 oh yea that makes more sense. Wouldn't my personal IP address (that I am using locally using localhost) also be blocked from them?
Avatar
let's just put it this way. your server is probably in a data centre which means it shares the same subnet with many other servers, on which the malicious requests are made. hence discord blocks the ip range. your personal ip address is not part of any blocked ip ranges, but a server in us-east for example is likely to fall inside the blocked ip range. this is a pretty common anti-spam tactic.
Answer
Avatar
@gin discord usually includes ratelimit headers in response
Avatar
yes they would include some of them. I think I don't exceeded them as the error is a 403 forbidden instead of a 429 rate limiting. Maybe I read the error message wrong [(see here)](https://pastebin.com/FeptVEtD)
Avatar
@gin did u solve the issue?
Avatar
I am working on it.. when deploying it on my vps and running next build and next start the types (inside the tsx) are broken. But I am on it 👍
Avatar
Deploying it on my own VPS (with it's own ip) worked. Thanks! 🙂