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

Use SameSite=strict cookies consistently #1450

Merged
merged 2 commits into from Oct 20, 2017

Conversation

lol768
Copy link
Contributor

@lol768 lol768 commented Oct 14, 2017

Background

Cross-site request forgery is a common security issue that affects many web applications. ZNC mitigates the risk of CSRF in the same manner as many other applications - by making use of a token mechanism. A <input type="hidden" name="_CSRF_Check" value="token"> field is included in all forms and when the browser POSTs the data, the token is checked. This ensures that a cross-origin POST will be unsuccessful because the malicious site will not know the token's value.

SameSite

The SameSite attribute can be applied to cookies. Browser such as Chrome and Opera (which implement the spec) will refuse to send the cookie along with potentially dangerous requests which originate from another domain. SameSite can work in two modes of operation:

  • Strict: Cookies are not send for a request from a foreign origin, period. Note that this covers links that would trigger a GET.
  • Lax: Cookies are sent for HTTP methods considered safe. This relies on the web application following the best practice

This PR marks all cookies set by ZNC to be SameSite strict. This choice was made following the principle of least privilege - I cannot think of a compelling reason not to apply SameSite=strict to ZNC's cookies - it seems unlikely a user would want to deeplink a page within the ZNC administration panel from another origin, for example. If you have a reason to disagree or believe this will cause problems, please do post a reply and elaborate on your use-case.

Not a silver bullet

SameSite should be used as part of a defense in depth approach. It is not a replacement for the existing token-based approach at migitaging the risk of CSRF. Firefox does not yet have support for this attribute but a tracking bug exists.

Further reading

@codecov
Copy link

codecov bot commented Oct 14, 2017

Codecov Report

Merging #1450 into master will decrease coverage by 0.75%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1450      +/-   ##
==========================================
- Coverage   35.42%   34.66%   -0.76%     
==========================================
  Files         126      126              
  Lines       30635    30622      -13     
  Branches     4733     4733              
==========================================
- Hits        10851    10614     -237     
- Misses      19220    19361     +141     
- Partials      564      647      +83
Impacted Files Coverage Δ
src/HTTPSock.cpp 36.29% <100%> (-4.44%) ⬇️
modules/keepnick.cpp 40.16% <0%> (-4.1%) ⬇️
modules/autoattach.cpp 45.97% <0%> (-4.03%) ⬇️
src/Template.cpp 42.12% <0%> (-4.02%) ⬇️
modules/modperl/pstring.h 78.57% <0%> (-3.58%) ⬇️
src/WebModules.cpp 36.84% <0%> (-3.41%) ⬇️
modules/modpython.cpp 19.39% <0%> (-2.74%) ⬇️
src/znc.cpp 43.72% <0%> (-2.27%) ⬇️
src/Chan.cpp 45.49% <0%> (-2.21%) ⬇️
... and 23 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2ebd358...e80292d. Read the comment docs.

Copy link
Member

@DarthGandalf DarthGandalf left a comment

Choose a reason for hiding this comment

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

This is complicated.
In theory, SendCookie() function allows modules to send cookies to browser.
In practice, the only cookie sent is the session ID.

Session ID needs SameSite for sure. But the custom modules' cookies might suffer from SameSite, from HttpOnly, depending on their use case.
Perhaps ZNC cookies API should be changed, when the need for such cookies arises. Until this happens, +1 from me on adding SameSite the way you did.

src/HTTPSock.cpp Outdated
@@ -743,7 +743,7 @@ bool CHTTPSock::PrintHeader(off_t uContentLength, const CString& sContentType,
for (const auto& it : m_msResponseCookies) {
Write("Set-Cookie: " + it.first.Escape_n(CString::EURL) + "=" +
it.second.Escape_n(CString::EURL) + "; HttpOnly; path=/;" +
(GetSSL() ? "Secure;" : "") + "\r\n");
(GetSSL() ? "Secure;" : "") + " SameSite=strict;\r\n");
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it be Strict with capital S? To follow the example from RFC draft you linked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, thanks. Fixed

@lol768
Copy link
Contributor Author

lol768 commented Oct 19, 2017

Session ID needs SameSite for sure. But the custom modules' cookies might suffer from SameSite, from HttpOnly, depending on their use case.
Perhaps ZNC cookies API should be changed, when the need for such cookies arises. Until this happens, +1 from me on adding SameSite the way you did.

Yup, ideally there'd be a cookie API that would let you specify the path, whether or not to set the HttpOnly flag, the SameSite value etc.

@DarthGandalf DarthGandalf merged commit dca012f into znc:master Oct 20, 2017
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.

None yet

2 participants