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

Add support for Angular XSRF protection #2153

Merged
merged 14 commits into from
Jun 12, 2018
Merged

Conversation

nwmac
Copy link
Contributor

@nwmac nwmac commented May 11, 2018

No description provided.

@nwmac nwmac self-assigned this May 11, 2018
@cfdreddbot
Copy link

Hey nwmac!

Thanks for submitting this pull request! I'm here to inform the recipients of the pull request that you and the commit authors have already signed the CLA.

@nwmac
Copy link
Contributor Author

nwmac commented May 15, 2018

Note that the e2e tests will need updating, as we won't be able to call the API without the XSRF header.

if c.Request().Method() == "GET" || c.Request().Method() == "HEAD" {
return h(c)
}
errMsg := "Failed to get stored XSRF token from user session"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's recommended to check for cross-origin requests at this point using request headers: https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet#Verifying_Same_Origin_with_Standard_Headers.

@jmcarp
Copy link

jmcarp commented May 16, 2018

Just curious--why not use an existing csrf library like https://github.com/gorilla/csrf here? The approach is similar, but the library is already battle-tested and mitigates a few more issues than this implementation (afaict).

@nwmac
Copy link
Contributor Author

nwmac commented May 16, 2018

@jmcarp Thank you for the comments... I will take a look at that library and will update this PR to use it in place of the custom middleware. Thanks for the pointer.

@nwmac
Copy link
Contributor Author

nwmac commented May 18, 2018

@jmcarp I tried to switch over to the gorilla library, but we use echo server and its written for the golang http handler mechanism. There is a wrapper that lets you use this with echo - which I tried. Could get the middleware working, but could not get the token in order to send to the front-end.

Spent a long time debugging this and the gorilla csrf lib stores the token on the a context keyed by the request object - it appears that the request object that the middleware sees is different to that later on in the chain, so when I request the token it always comes back empty.

Couldn't find a way around this. For now, I've updated the PR slightly.

We are already using the echo cors middleware - https://echo.labstack.com/middleware/cors - for CORS, so that should be covered. They do have a csrf middleware, but there isn't much to it over and above what was already implemented.

@KlapTrap KlapTrap added the conflicts Merge conflicts on PR label May 31, 2018
…port

# Conflicts:
#	package.json
#	src/backend/app-core/main.go
#	src/frontend/app/app.module.ts
#	src/test-e2e/endpoints/endpoints-connect.e2e-spec.ts
#	src/test-e2e/endpoints/endpoints-register.e2e-spec.ts
#	src/test-e2e/helpers/e2e-helpers.ts
@codecov
Copy link

codecov bot commented May 31, 2018

Codecov Report

Merging #2153 into v2-master will decrease coverage by 0.03%.
The diff coverage is 36.84%.

@@              Coverage Diff              @@
##           v2-master    #2153      +/-   ##
=============================================
- Coverage      71.05%   71.02%   -0.04%     
=============================================
  Files            581      581              
  Lines          24255    24257       +2     
  Branches        5441     5442       +1     
=============================================
- Hits           17235    17229       -6     
- Misses          7020     7028       +8

@nwmac nwmac removed the conflicts Merge conflicts on PR label Jun 1, 2018
@richard-cox richard-cox added the conflicts Merge conflicts on PR label Jun 5, 2018
…port

# Conflicts:
#	src/frontend/app/store/effects/auth.effects.ts
@nwmac nwmac removed the conflicts Merge conflicts on PR label Jun 11, 2018
@nwmac nwmac requested a review from irfanhabib June 11, 2018 13:20
@irfanhabib irfanhabib added in review needs attention This PR needs attention labels Jun 11, 2018
@nwmac nwmac removed the needs attention This PR needs attention label Jun 11, 2018
@irfanhabib
Copy link
Contributor

LGTM

@irfanhabib irfanhabib merged commit e96555f into v2-master Jun 12, 2018
@irfanhabib irfanhabib deleted the angular-xsrf-support branch June 12, 2018 07:45
@irfanhabib irfanhabib restored the angular-xsrf-support branch June 12, 2018 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants