Home > Software engineering >  Fix WhatsApp infinite redirection starting with double slashes
Fix WhatsApp infinite redirection starting with double slashes

Time:03-28

I have a web server with routes such as:

router.get("/inicio", async (req, res) => {
  // logic ...
  return res.render("template");
}

and a route for everything else:

app.get("*", async (req, res) => {
  await authentication.getLocalInfo(req);
  return res.redirect("/404");
});

When I share my website on WhatsApp with `https://emocoes.org/inicio", the server logs the following requests in quick succession, with about 70 MB of text in the server logs for this near-infinite loop:

2022-03-06T10:26:16.176Z - //inicio 
2022-03-06T10:26:16.334Z - //inicio/404 
2022-03-06T10:26:16.492Z - //inicio/404/404 
2022-03-06T10:26:16.652Z - //inicio/404/404/404 
2022-03-06T10:26:16.801Z - //inicio/404/404/404/404 
2022-03-06T10:26:17.027Z - //inicio/404/404/404/404/404 
2022-03-06T10:26:17.191Z - //inicio/404/404/404/404/404/404 
2022-03-06T10:26:17.329Z - //inicio/404/404/404/404/404/404/404 
2022-03-06T10:26:17.520Z - //inicio/404/404/404/404/404/404/404/404 
2022-03-06T10:26:17.789Z - //inicio/404/404/404/404/404/404/404/404/404 
2022-03-06T10:26:17.955Z - //inicio/404/404/404/404/404/404/404/404/404/404 
2022-03-06T10:26:18.093Z - //inicio/404/404/404/404/404/404/404/404/404/404/404 
...

Another example: /diadopai should redirect to /registo but instead redirects to /diadopai/registo, which does not exist, and so on.

How can I make WhatsApp request the right route, or avoid this near-infinite loop?

CodePudding user response:

Your redirects are not absolute, and are using the current path as the base. Try this instead:

return res.redirect(req.protocol   "://"   req.headers.host   "/404");

Also, you should create a route to handle /404 otherwise it will infinetly loop back on itself. This should display an error page etc and should not redirect anywhere new.

  • Related