Add Trusted Origins to CSRF Handler - #2301
Conversation
|
Worked for me as well, using Traefik as a reverse proxy. Thanks! |
How did you get to get this working with Traefik? Is gophish served as a subdomain? |
|
Yep, I took the following steps:
Afterwards, I deploy using Docker Swarm with a Traefik reverse proxy. My docker compose file looks like this: version: '3.2'
services:
gophish:
container_name: gophish
# Use custom registry
image: private.registry/gophish
restart: unless-stopped
networks:
- proxy
deploy:
placement:
constraints: [node.hostname == phish]
labels:
- "traefik.enable=true"
- "traefik.docker.network=proxy"
# Regular
- "traefik.http.routers.gophish.entrypoints=http"
- "traefik.http.routers.gophish.rule=Host(`gophish.domain.com`)"
- "traefik.http.middlewares.gophish-https-redirect.redirectscheme.scheme=https"
- "traefik.http.routers.gophish.middlewares=gophish-https-redirect"
- "traefik.http.routers.gophish-secure.entrypoints=https"
- "traefik.http.routers.gophish-secure.rule=Host(`gophish.domain.com`)"
- "traefik.http.routers.gophish-secure.tls=true"
- "traefik.http.routers.gophish-secure.tls.certresolver=http"
- "traefik.http.routers.gophish-secure.service=gophish"
- "traefik.http.services.gophish.loadbalancer.server.port=80"
# Admin
- "traefik.http.routers.gophish-admin.entrypoints=http"
- "traefik.http.routers.gophish-admin.rule=Host(`admin.gophish.domain.com`)"
- "traefik.http.middlewares.gophish-admin-https-redirect.redirectscheme.scheme=https"
- "traefik.http.routers.gophish-admin.middlewares=gophish-admin-https-redirect"
- "traefik.http.routers.gophish-admin-secure.entrypoints=https"
- "traefik.http.routers.gophish-admin-secure.rule=Host(`admin.gophish.domain.com`)"
- "traefik.http.routers.gophish-admin-secure.tls=true"
- "traefik.http.routers.gophish-admin-secure.tls.certresolver=http"
- "traefik.http.routers.gophish-admin-secure.service=gophish-admin"
- "traefik.http.services.gophish-admin.loadbalancer.server.port=3333"
networks:
proxy:
external:
name: proxy |
|
I took the following steps:
I'm using an Apache reverse proxy: issue created about this No TLS configured for any of the endpoints. Still getting: "Forbidden - CSRF token invalid" Thanks |
Are you sure you specified just the domain part? From the linked issue, it seems you specified the protocol as well. From the for _, trustedOrigin := range cs.opts.TrustedOrigins {
if referer.Host == trustedOrigin {
valid = true
break
}
} |
|
Hi @mcab , I did tried it without the https:// , just -> admin.domain.com . I even tried the "master domain", without admin, just domain.com. Still the same error. I realized that with the referer wasn't arriving in the logs of gophish, so I even changed my apache configuration: After in Apache configuration: Still the same error. How can I do some extra analysis in the gophish side? (extra logs) Thanks |
|
@tarrinho I'm not sure if you'll be able to get extra verbosity in the gophish logs, but you could insert a webproxy to output all requests + headers. Or run a tcpdump or tshark session perhaps? |
|
I almost forked the repo to do this myself; glad I found this first. I'd really like to have this feature. It would make my nginx ingress controller almost trivially-easy to support. Currently, I have to use Please update And (You don't need to check for the And finally, here's how to transform your comma-separated environment variable into a proper array in |
The problem has to do with Apache!!! I just added a nginx in the communication flow with the following conf and it worked: But I wanted to do this without the nginx in the equation! Any ideas on the Apache conf? |
|
@tarrinho I'm running into the same issue. Where did you add this? |
|
I added a Nginx server in front of the docker. I mean, your browser will connect to the Nginx server that will proxy to the docker port. With this setup, it worked for me. I had an Apache previously doing this proxying, but I didn't manage to get it to work. |
can you please share your Nginx rule ? |
|
Will this one be merged anytime? |
|
@mcab While I wait for this one to be merged, I pulled all the latest gophish code then added in your changes, configured and all worked flawlessly. Thank you so much for this!!! |
|
I'm trying to use gophish in a k8s cluster and i'm finding it hard to figure out how to make it work with an nginx ingress in front I hope this PR makes it easier 🙏 Are there any plans on merging it and update the docker image ? PS: if anyone made it to make it working with a nginx ingress in front please post the solution. 🙏 |
|
Thanks all for your work on this and patience in getting it merged in. @mcab what are your thoughts on the comment from @tekkamanendless (#2301 (comment)) with the three small modifications? |
|
I've tested the PR with ngrok and it works well:
amending
and we no longer get the CSRF error. Of interest wildcards don't work (*.ngork.io), but I think that's to be expected. |
Happy to get to this by the end of tomorrow. 👍 |
|
Ah. Codespaces plus vigilant mode on my account is making those commits show up as Unverified, but it incorporates the feedback from @tekkamanendless (thank you, by the way!) This should be ready to merge. |
Enables the user to add addresses that they expect incoming connections to come from. Helpful in cases where TLS termination is handled by a load balancer upstream, rather than the application itself. This does not support wildcard hosts.
If admin_server.trusted_origins is set, have the CSRF handler allow those origins to serve up requests.
|
merge merge merge! |
| "cert_path": "gophish_admin.crt", | ||
| "key_path": "gophish_admin.key" | ||
| "key_path": "gophish_admin.key", | ||
| "trusted_origins": "" |
There was a problem hiding this comment.
This should be:
"trusted_origins": []
to match the structure in config/config.go.
There was a problem hiding this comment.
oof
Yep, I should have handled that.
|
Merged into master, thanks for the great new feature everyone. |
|
Nice! Are there any documentation tidbits to update in parallel? |
|
@mcab I added some info here: https://docs.getgophish.com/user-guide/installation#understanding-the-config.json Let me know if you think it needs anything more to better explain your contribution. |

With a load balancer acting as TLS-termination for the application, there have been issues where the underlying application runs into a CSRF issue:
In particular, it comes from the CSRF handler seeing the
Refererheader passed through as an invalid origin. This ultimately depends on how your load balancer does its negotiation. Since the application is unaware of the load balancer's existence, the application sees the forwarded request from a host it doesn't understand, and returns a 403.To fix this, explicitly state that there are origins that we will expect requests to come from. It's assumed to be a comma-delimited string, because I didn't know how to translate multiple environment variables into a string array using
jq.Happy to work a bit more (tests, docs, etc.) to get this in.