workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

Unable to Authenticate Wrangler in any Remote Development Environment

Open Grunet opened this issue 3 years ago • 28 comments

https://developers.cloudflare.com/workers/wrangler/commands/#using-wrangler-login-on-a-remote-machine outlines how to authenticate the Wrangler CLI when using a remote development environment (e.g. Codespaces).

However it doesn't seem to work. The original shell always puts out this error message at the end

Received query string parameter doesn't match the one sent! Possible malicious activity somewhere.

I've seen this on Gitpod, Codespaces, Stackblitz, and Codesandbox so far so I don't think it's specific to any of them.

Is there an alternative form of authentication that could be used in the meantime? I haven't gotten api key-based authentication to work either

Grunet avatar Mar 08 '23 12:03 Grunet

My assumption is this has something to do with either Proof of Key Code Exchange (PKCE) and/or state for Cross Site Request Forgery (CSRF) protection, where after I authenticate via the UI, my local browser has something that running curl on the remote machine doesn't

Grunet avatar Mar 08 '23 12:03 Grunet

Yeah it's state https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/src/user/user.ts#L636

Grunet avatar Mar 08 '23 12:03 Grunet

Afaict the state is always a query parameter on the redirect URL from the Identity Provider (Cloudflare) and it matches what was sent initially by wrangler (the Referring Provider)

Grunet avatar Mar 08 '23 12:03 Grunet

Not sure what I did differently this time but following this code https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/src/user/auth-variables.ts#L14 I was able to do

export CLOUDFLARE_API_TOKEN="API token generated from the Edit Workers template"
npx wrangler whoami

And that appears to have worked

Still not clear to me why the OAuth2 flow isn't working

Grunet avatar Mar 08 '23 13:03 Grunet

*Oh sorry I split this off of an issue comment and Github didn't give me the template. Lmk if I should fix that up

Grunet avatar Mar 08 '23 13:03 Grunet

It sounds like you've got this working with API tokens? That's what we'd recommend for environments like Stackblitz and Codespaces. The remote machine authentication flow should work though (or should be removed from the docs), and so I'll leave this open to track investigating that.

penalosa avatar Apr 03 '23 09:04 penalosa

It sounds like you've got this working with API tokens?

Correct yeah, I think I found a comment on a closed issue that clued me in to trying that, and it's worked so far

Grunet avatar Apr 04 '23 02:04 Grunet

@Grunet
Thank you for writing this issue, I also was having the same issue until I found a simple way to communicate between the remote development environment and the local in browser account page of cloudflare

So how it works is basically you are doing the same steps no change in that,

  1. wrangler login
  2. https://dash.cloudflare.com/oauth2/auth?response_type=code&client_id=<Your-Client-Code>&state=<Your-State-Token>&code_challenge=<Your-Challenge-Token>&code_challenge_method=S256&redirect_uri=http%3A%2F%2Flocalhost%3A8976%2Foauth%2Fcallback&scope=account%3Aread+user%3Aread+workers%3Awrite+workers_kv%3Awrite+workers_routes%3Awrite+workers_scripts%3Awrite+workers_tail%3Aread+zone%3Aread+offline_access
  3. You open the secure link in the browser where you have cloudflare account logged in.
  4. Note : Cloudflare Auth after successful authorization will redirect you to the following query string redirect_uri=http%3A%2F%2Flocalhost%3A8976%2Foauth%2Fcallback&scope=account%3Aread+user%3Aread+workers%3Awrite+workers_kv%3Awrite+workers_routes%3Awrite+workers_scripts%3Awrite+workers_tail%3Aread+zone%3Aread+offline_access
  5. After a successful you will have be redirected to the page http://localhost:8976/oauth/callback?code=<Your-Auth-Code>&scope=account%3Aread%20user%3Aread%20workers%3Awrite%20workers_kv%3Awrite%20workers_routes%3Awrite%20workers_scripts%3Awrite%20workers_tail%3Aread%20zone%3Aread%20offline_access&state=<Your-State-Token>
  6. Well, to no surprise this page will not work in your current browser window as the origin is the remote development environment which is not accessible as localhost.
  7. What can be done next is pretty the main stream principle of using curl internally for testing APIs in development servers.
  8. So change the localhost to 127.0.0.1 or what ever your *inet* is there before subnet mask. [To get the details of internal networking use command ip addr, usually it is 127.0.0.1 if nothing is changed from defaults]
  9. Now your callback url should look like http://127.0.0.1:8976/oauth/callback?code=<Your-Auth-Code>&scope=account%3Aread%20user%3Aread%20workers%3Awrite%20workers_kv%3Awrite%20workers_routes%3Awrite%20workers_scripts%3Awrite%20workers_tail%3Aread%20zone%3Aread%20offline_access&state=<Your-State-Token>
  10. Now open a new bash terminal or powershell or Terminal(macOS terminal) and use the command curl "<your-callback-url>" the callback url is the one we made in the 10th point.
  11. Voila! you are now logged in. Remember to fill in the details in the boilerplate url schema

mishra-saksham avatar Oct 06 '23 05:10 mishra-saksham

hi @Grunet :) sounds like you've got things working and that @mishra-saksham is able to get the the remote machine authentication flow (https://developers.cloudflare.com/workers/wrangler/commands/#use-wrangler-login-on-a-remote-machine) going (thanks for the write up!). given this, i will close the issue now, thanks both!

lrapoport-cf avatar Jan 10 '24 18:01 lrapoport-cf

Thanks for sharing, I tried and curl could connect and send its GET request:

*   Trying 127.0.0.1:8976...
* connect to 127.0.0.1 port 8976 failed: Connection refused
*   Trying ::1:8976...
* Connected to localhost (::1) port 8976 (#0)
> GET /oauth/callback?code=[redacted]
> Host: localhost:8976
> User-Agent: curl/7.81.0
> Accept: */*

but wrangler login replied with:

▲ [WARNING] Received query string parameter doesn't match the one sent! Possible malicious activity somewhere.

when I ran curl with the url in quotes I got:

/home/[redacted]/node_modules/wrangler/wrangler-dist/cli.js:29573
            throw a;
            ^

SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (/home/[redacted]/node_modules/wrangler/wrangler-dist/cli.js:5121:19)
    at successSteps (/home/[redacted]node_modules/wrangler/wrangler-dist/cli.js:5092:27)
    at fullyReadBody (/home/[redacted]/node_modules/wrangler/wrangler-dist/cli.js:3590:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async specConsumeBody (/home/[redacted]/node_modules/wrangler/wrangler-dist/cli.js:5101:7)
    at async exchangeAuthCodeForAccessToken (/home/[redacted]/node_modules/wrangler/wrangler-dist/cli.js:120821:31)
    at async Server.<anonymous> (/home/[redacted]/node_modules/wrangler/wrangler-dist/cli.js:120983:30)

Node.js v20.12.1

wrangler version: ⛅️ wrangler 3.51.2

newsve avatar Apr 22 '24 06:04 newsve

API token generated from the Edit Workers template

where did u git "API token generated from the Edit Workers template" from?

thewaleed avatar Jul 24 '24 21:07 thewaleed

API token generated from the Edit Workers template

where did u git "API token generated from the Edit Workers template" from?

@thewaleed Go to My Profile > API Tokens > Create Token > Use Template for "Edit Cloudflare Workers"

Grunet avatar Jul 25 '24 21:07 Grunet

Had this same issue and resolved it by forwarding port 8976 to my local machine when ssh-ing to the remote environment.

tkalejaiye avatar Aug 25 '24 13:08 tkalejaiye

Still the same issue with 3.78.3 tried:

  • https://developers.cloudflare.com/workers/wrangler/commands/#use-wrangler-login-on-a-remote-machine
  • forwarding the port

faceslog avatar Sep 17 '24 19:09 faceslog

adding on to mishra's explanation, I had to change the host from localhost to [::1] instead of 127.0.0.1 in order for it to succeed. Using localhost gave me an invalid query string error, and using 127.0.0.1 curl failed to connect. It seems wrangler may be listening on ipv6 specifically in some cases and using locahost for that case somehow causes verification to fail, maybe the http host header is involved somehow? I don't know enough to speculate past that.

sugoidogo avatar Sep 26 '24 05:09 sugoidogo

Not sure what I did differently this time but following this code https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/src/user/auth-variables.ts#L14 I was able to do

export CLOUDFLARE_API_TOKEN="API token generated from the Edit Workers template"
npx wrangler whoami

And that appears to have worked

Still not clear to me why the OAuth2 flow isn't working

that is by far the most straight forward route, thanks! (using .devcontainer)

avelops avatar Sep 29 '24 20:09 avelops

Can we please reopen this issue? Headless login still is not directly supported and instead must rely on workarounds (those in this thread and the official docs), the workarounds in the official documentation do not work for all the people commenting in this issue (nor for me). And to top it all off, for me none of the following work:

  1. Official docs suggest to curl the localhost redirect -> Received query string parameter doesn't match the one sent! Possible malicious activity somewhere.
  2. export CLOUDFLARE_API_TOKEN="API token generated from the Edit Workers (or equivalent) template" -> wrangler in some cases treats me as logged in and in others does not. Did not actually allow me to complete uploading to cf pages
  3. curl http://127.0.0.1:8976/... or equivalent instead of localhost -> Failed to connect to 127.0.0.1 port 8976 after 0 ms: Connection refused
  4. curl http://[0:0:1]:8976/... or equivalent instead of localhost -> Received query string parameter doesn't match the one sent! Possible malicious activity somewhere.

Please can the wrangler team add better support for headless logins so that we don't need to follow these workarounds? I have never had a problem logging into the GCP CLI in a remote machine, and I don't think modifying wrangler to use a similar flow would be too difficult. Right now you essentially do not support headless logins with a stable interface that works across reasonably standard dev environment.

If you wanted to take the approach gcloud took, clientside you can add a login arg npx wrangler login --headless that generates a clickable link with a url param indicate the user is headless, and then waits for the user to paste in a token. Serverside you can read that headless arg and, when set, redirect to a page displaying the token (and whatever else you need wrangler to know to complete the login) for the user to copy back to wherever they are logging in (instead of a localhost url). That's how gcloud login works and it's perfectly functional, and not a huge change from what you have already.

fredxfred avatar Oct 26 '24 20:10 fredxfred

One possible cause could be bash/sh/etc interpreting special characters in the url, causing a modified url to be passed to curl instead of what is shown on the command line. I just signed in using the exact url successfully by putting quotes around it.

sugoidogo avatar Nov 08 '24 10:11 sugoidogo

One possible cause could be bash/sh/etc interpreting special characters in the url, causing a modified url to be passed to curl instead of what is shown on the command line. I just signed in using the exact url successfully by putting quotes around it.

This worked for me with Codespaces. Experience is shocking though.

b1m-dev avatar Nov 10 '24 11:11 b1m-dev

One possible cause could be bash/sh/etc interpreting special characters in the url, causing a modified url to be passed to curl instead of what is shown on the command line. I just signed in using the exact url successfully by putting quotes around it.

This is the way

juparave avatar Feb 18 '25 18:02 juparave