With the above example after successful login I am redirected to Facebook.com
The lack of URL validation here introduces an arbitrary redirect vulnerability that can be abused for phishing attempts or as part of a complex attack chain.
How can I validate the state param to make sure this does not happen?
I tried a few ways following the documentation but nothing works so far.
But ideally you might have to generate an opaque value and pair it with the destination url in a local store. Then send this opaque value as state and use it in callback url code to redirect user to destination URL
I will provide detailed steps with an example.
You create a unique opaque value such as a uuid - 7e981003-1ddb-4732-a9cf-c97087c79fb5
Store it in client storage such as localstorage/sessionstorage along with your destination url - {β7e981003-1ddb-4732-a9cf-c97087c79fb5β: βhttps://www.facebook.com/β}
During callback use this value to lookup destination url from your client storage and redirect
By using this method, your destination URL will be something from your client storage. If someone tampers with the state, you will not find a match and can respond with an error and/or retry authorize again.