monero-lws icon indicating copy to clipboard operation
monero-lws copied to clipboard

how to solve cors.

Open TechGoku opened this issue 2 years ago • 8 comments
trafficstars

Access to XMLHttpRequest at 'http://127.0.0.1:8443/login' from origin 'http://127.0.0.1:9110' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute. index.js:98 ❌ Error: Connection Failure

i have added --access-control-origin http://127.0.0.1:9110 . while running lws daemon . but still i am facing this issue .

TechGoku avatar Jan 04 '23 11:01 TechGoku

I think this may require a change to monero core, but I'd have to re-test this myself.

vtnerd avatar Jan 24 '23 15:01 vtnerd

Hi, any updates with CORS? Facing the same problem

wasabiwallet avatar Jul 06 '23 20:07 wasabiwallet

No updates - could you provide a minimal test suite? Presumably in Javascript ?

vtnerd avatar Jul 07 '23 13:07 vtnerd

To connect to the LWS API from websites with a different domain/IP address than the one where LWS is located, you need to include the "Access-Control-Allow-Origin" header in the response from the LWS server. Its value can be set to "*" to allow connections from any domain and IP address, or it can be set to the specific domain/IP address the user wants to grant access to. If this header is absent, browsers will display an error message in the console: "Access to resource has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource" (e.g., in Chrome).

wasabiwallet avatar Jul 07 '23 16:07 wasabiwallet

No updates - could you provide a minimal test suite? Presumably in Javascript ?

Hi VTNerd- This is still an issue. Save this as an html file, update the main address, secret view key and the LWS url and open it in your browser and click the button. There will be a CORS error in the console and network tab. If you use a CORS disable extension for your browser, you'll see that the request succeeds.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>LWS Fetch API POST Request Example</title>
  </head>
  <body>
    <button id="lwsButton">Make LWS POST Request</button>
    <script>
      const myButton = document.getElementById("lwsButton");
      myButton.addEventListener("click", async () => {
        const url = "http://localhost:8000/get_address_txs";
        const options = {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            address: "yourMainAddressHere",
            view_key: "yourSecretViewKeyHere",
          }),
        };
        try {
          const response = await fetch(url, options);
          const responseData = await response.json();
          console.log(responseData);
        } catch (error) {
          console.error(error);
        }
      });
    </script>
  </body>
</html>

CryptoGrampy avatar Sep 01 '23 03:09 CryptoGrampy

And CORS seems to work fine with Monerod (setting the access control config option). There are a number of 'browser compatible' nodes: https://monero.fail/?chain=monero&network=mainnet&cors=on .. There just seems to be something funky with LWS and CORS config. I have never been able to get it to work and have to proxy all of my calls.

CryptoGrampy avatar Sep 01 '23 03:09 CryptoGrampy

And CORS seems to work fine with Monerod (setting the access control config option).

LWS is using the same CORS code as monerod, so I don't understand what's preventing it from working.

vtnerd avatar Oct 19 '23 22:10 vtnerd

I'm running with --access-control-origin "*" and can report on this:

I don't have any issues with local stuff - my flask app is able to hit LWS without issue and JS on web pages is as well.

@CryptoGrampy 's sample code actually works for me - CORS is not an issue with simple ajax request.

In @TechGoku 's example, I'm guessing they are running mymonero-web-js (because of port 9110 being bound). If that is indeed the case, the issue is with the axios http client, this setting in an upstream MyMonero package. They hard coded this value. Flipping it to false manually in your node_modules is a temporary fix.

lalanza808 avatar Nov 12 '23 19:11 lalanza808