```js interface Env { MY_RATE_LIMITER: any; } export default { async fetch(req, env): Promi

interface Env
{
    MY_RATE_LIMITER: any;
}

export default {
    async fetch(req, env): Promise<Response>
    {
        // https://developers.cloudflare.com/workers/runtime-apis/bindings/rate-limit/

        const ipAddress = req.headers.get("cf-connecting-ip") || "";
        const { success } = await env.MY_RATE_LIMITER.limit({ key: ipAddress })
        if (!success)
            return new Response(`429 Failure – rate limit exceeded for ${ipAddress}`, { status: 429 });

        const login = req.headers.get("x-login");
        const password = req.headers.get("x-password");

        if (!login || !password)
            return new Response("", { status: 403 });
        
        const isValidUser = await validateUser(login, password);
        if (!isValidUser)
            return new Response("", { status: 403 });

        //const origin = new Request(req.url, req);
        //origin.headers.delete("x-login");
        //origin.headers.delete('cf-workers-preview-token');
        //return fetch(origin);
        return fetch(req)
    },
} satisfies ExportedHandler;

async function validateUser(login: string, password: string): Promise<boolean>
{
    const response = await fetch("https://tunnel.xxx.com/authentication",
        {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({ login, password })
        });

    // The server returns 200 for valid authentication, 403 for invalid
    return response.status === 200;
}

i have a WAF rule to
block
POST
requests,
await fetch("https://tunnel.xxx.com/authentication",
works on cloudflare
debugging locally with
npx wrangler dev --remote
on this same request i get
(403) Forbidden
as response
Was this page helpful?