cloudflare / cloudflared

Cloudflare Tunnel client (formerly Argo Tunnel)
https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/tunnel-guide
Apache License 2.0
8.79k stars 775 forks source link

cloudflared access curl integration feedback #223

Open Lekensteyn opened 4 years ago

Lekensteyn commented 4 years ago

The cloudflared access curl command (docs) can be used to obtain a valid authentication token and include the cf-access-token header with the curl command line. However there were some integration challenges when naively replacing an existing curl invocation by cloudflared access curl:

securitygeneration commented 3 years ago

@Lekensteyn makes some great points.

To add to this, I'm experiencing issues with cloudflared access curl <url> when there isn't an existing token for the URL.

In this case cloudflared tries to open the browser to authenticate the user, with the intent of redirecting the user to the requested URL after authentication. Except I've found that when using the curl command, cloudflared does not properly generate the return_url parameter. As an example, when running: cloudflared access curl https://protectedservice.domain.com/protected/path, cloudflared will open the following URL in the browser:

https://protectedservice.domain.com/cdn-cgi/access/cli?redirect_url=https%3A%2F%2Fprotectedservice.domain.com%3Ftoken%3D<token>%253D&token=<token>

Note the redirect_url parameter is effectively https://protectedservice.domain.com (and note the missing path). This results in an Invalid redirect URL error (because the URL without the path does not have a Cloudflare Access application policy associated with it):

image

In contrast when doing a login by running cloudflared access login https://protectedservice.domain.com/protected/path, cloudflared will open the following URL in the browser:

https://protectedservice.domain.com/cdn-cgi/access/cli?redirect_url=https%3A%2F%protectedservice.domain.com%2Fprotected%2Fpath%3Ftoken%3D<token>%253D&token=<token>

Note the redirect_url in this instance is https://protectedservice.domain.com/protected/path (which is correct), and correctly opens the authentication page (and returns a token upon successful authentication).

I, too, rely on the curl command to programmatically check whether the current token is still valid and results in access to the requested resource. Due to the issues outlined above, this fails, since the process halts in the browser with the Invalid redirect URL error. I thus have to resort to setting a timeout on the curl command, and then running the login command in order to properly retrieve a new token.