Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disabled CSRF check for github codespaces #4336

Merged
merged 5 commits into from
Dec 13, 2023

Conversation

niladrix719
Copy link
Member

Fixes #4334

Describe the changes you have made in this PR -

disabled CSRF check for github codespaces

Screenshots of the changes (If any) -

Screen.Recording.2023-12-02.at.11.59.22.PM.mov

@tachyons
Copy link
Member

tachyons commented Dec 5, 2023

We need to add codespaces URL here

config.hosts << /.*\.gitpod\.io\Z/
. Removing CSRF check all together makes it harder to test on code spaces.

@niladrix719
Copy link
Member Author

but in dev environment we are already whitelisting everything -

config.hosts << /.*\Z/ # Whitelist everything in Dev

i have tried using config.hosts << /.*\.app\.github\.dev\Z/ which seems not to work

upon research i found out this issue
github/codespaces-rails#37

will investigate more if there is a solution without disabling it

@tanmoysrt
Copy link
Member

tanmoysrt commented Dec 5, 2023

Hi @niladrix719
I checked the issue. It seems that Github Codespaces has a reverse proxy setup which pass the requests to the containers.

Now, while doing the reverse proxy they don't rewrite the Host Header's.
As a result - Request URL becomes localhost:3000/*
And Http Host Header also changes to localhost, But the X-Forwarded-Host header contains the actual URL (*.github.dev).

So, as a result it fails CSRF verification (request base !== X-Forwarded-Host).

In older Rails, It gives preference to X-Forwarded-Host header instead of Host Header for decide actual Host
In latest patches, they have given preference to Host header to resolve cache poisoning attack. Ref - rails/rails#29893 rails/rails#22965

Usually, while using nginx/haproxy/traefik we can rewrite this header but in this Codespaces we can't have that flexibility.

So, we may disable CSRF for only Github Codespaces

We can add this configuration to disable the CSRF check in development

config.action_controller.forgery_protection_origin_check = false

@tanmoysrt
Copy link
Member

If you want to just check whether it's a codespace, you can use the DEV_CONTAINER environment variable
Ref - https://github.com/CircuitVerse/CircuitVerse/blob/master/.devcontainer/docker-compose.yml#L21

@tanmoysrt
Copy link
Member

Hi @niladrix719 I checked the issue. It seems that Github Codespaces has a reverse proxy setup which pass the requests to the containers.

Now, while doing the reverse proxy they don't rewrite the Host Header's. As a result - Request URL becomes localhost:3000/* But Http Host Header also changes to localhost, but the X-Forwarded-Host header contains the actual URL (*.github.dev).

So, as a result it fails CSRF verification (request base !== X-Forwarded-Host).

In older Rails, It gives preference to X-Forwarded-Host header instead of Host Header for decide actual Host In latest patches, they have given preference to Host header to resolve cache poisoning attack. Ref - rails/rails#29893 rails/rails#22965

Usually, while using nginx/haproxy/traefik we can rewrite this header but in this Codespaces we can't have that flexibility.

So, we may disable CSRF for only Github Codespaces

We can add this configuration to disable the CSRF check in development

config.action_controller.forgery_protection_origin_check = false

cc @tachyons

@niladrix719
Copy link
Member Author

@tanmoysrt thanks that was great help, just one question can we use CODESPACES environment variable instead of DEV_CONTAINER to identify codespaces

@tanmoysrt
Copy link
Member

@tanmoysrt thanks that was great help, just one question can we use CODESPACES environment variable instead of DEV_CONTAINER to identify codespaces

Yes you can
If you got time, just verify whether Dev container actually working In local environment.
In VS Code, you can run the same dev container. In codespaces that's actually running on the cloud.

@tachyons
Copy link
Member

tachyons commented Dec 5, 2023

Fantastic 🥳

@niladrix719
Copy link
Member Author

Screen.Recording.2023-12-05.at.8.20.13.PM.mov

i think its working fine

Copy link

codeclimate bot commented Dec 13, 2023

Code Climate has analyzed commit e1d327b and detected 0 issues on this pull request.

View more on Code Climate.

@tachyons tachyons merged commit 8d3df19 into CircuitVerse:master Dec 13, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid AuthenticityToken Error in Github Codespaces
3 participants