You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2018/09/18 22:39:06 UTC

[couchdb] branch hard-fail-cookie-mode created (now 243ecb1)

This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a change to branch hard-fail-cookie-mode
in repository https://gitbox.apache.org/repos/asf/couchdb.git.


      at 243ecb1  introduce hard failure mode for session cookies

This branch includes the following new commits:

     new 742b7f5  Change cookie payload to an expandable erlang proplist
     new 243ecb1  introduce hard failure mode for session cookies

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[couchdb] 01/02: Change cookie payload to an expandable erlang proplist

Posted by rn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch hard-fail-cookie-mode
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 742b7f5722f46a67d17ab514f1ea9473b48bf3a4
Author: Robert Newson <rn...@apache.org>
AuthorDate: Tue Sep 18 23:15:17 2018 +0100

    Change cookie payload to an expandable erlang proplist
---
 src/couch/src/couch_httpd_auth.erl | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)

diff --git a/src/couch/src/couch_httpd_auth.erl b/src/couch/src/couch_httpd_auth.erl
index 6ac7b75..a7b5c9e 100644
--- a/src/couch/src/couch_httpd_auth.erl
+++ b/src/couch/src/couch_httpd_auth.erl
@@ -25,7 +25,7 @@
 
 -export([authenticate/2, verify_totp/2]).
 -export([ensure_cookie_auth_secret/0, make_cookie_time/0]).
--export([cookie_auth_cookie/4, cookie_scheme/1]).
+-export([cookie_auth_cookie/3, cookie_scheme/1]).
 -export([maybe_value/3]).
 
 -import(couch_httpd, [header_value/2, send_json/2,send_json/4, send_method_not_allowed/2]).
@@ -192,15 +192,18 @@ cookie_authentication_handler(#httpd{mochi_req=MochiReq}=Req, AuthModule) ->
     undefined -> Req;
     [] -> Req;
     Cookie ->
-        [User, TimeStr, HashStr] = try
+        [SignedData, Hash] = try
             AuthSession = couch_util:decodeBase64Url(Cookie),
-            [_A, _B, _Cs] = re:split(?b2l(AuthSession), ":",
-                                     [{return, list}, {parts, 3}])
+            [_A, _B] = re:split(AuthSession, ":",
+                [{return, binary}, {parts, 2}])
         catch
             _:_Error ->
                 Reason = <<"Malformed AuthSession cookie. Please clear your cookies.">>,
                 throw({bad_request, Reason})
         end,
+        SessionData = binary_to_term(SignedData, [safe]),
+        User = couch_util:get_value(u, SessionData),
+        TimeStamp = couch_util:get_value(t, SessionData),
         % Verify expiry and hash
         CurrentTime = make_cookie_time(),
         case config:get("couch_httpd_auth", "secret", undefined) of
@@ -214,26 +217,25 @@ cookie_authentication_handler(#httpd{mochi_req=MochiReq}=Req, AuthModule) ->
             {ok, UserProps, _AuthCtx} ->
                 UserSalt = couch_util:get_value(<<"salt">>, UserProps, <<"">>),
                 FullSecret = <<Secret/binary, UserSalt/binary>>,
-                ExpectedHash = crypto:hmac(sha, FullSecret, User ++ ":" ++ TimeStr),
-                Hash = ?l2b(HashStr),
+                ExpectedHash = crypto:hmac(sha256, FullSecret, term_to_binary(SessionData)),
                 Timeout = list_to_integer(
                     config:get("couch_httpd_auth", "timeout", "600")),
                 couch_log:debug("timeout ~p", [Timeout]),
-                case (catch erlang:list_to_integer(TimeStr, 16)) of
-                    TimeStamp when CurrentTime < TimeStamp + Timeout ->
+                if
+                    CurrentTime < TimeStamp + Timeout ->
                         case couch_passwords:verify(ExpectedHash, Hash) of
                             true ->
                                 TimeLeft = TimeStamp + Timeout - CurrentTime,
                                 couch_log:debug("Successful cookie auth as: ~p",
                                                 [User]),
                                 Req#httpd{user_ctx=#user_ctx{
-                                    name=?l2b(User),
+                                    name=User,
                                     roles=couch_util:get_value(<<"roles">>, UserProps, [])
                                 }, auth={FullSecret, TimeLeft < Timeout*0.9}};
                             _Else ->
                                 Req
                         end;
-                    _Else ->
+                    true ->
                         Req
                 end
             end
@@ -254,17 +256,19 @@ cookie_auth_header(#httpd{user_ctx=#user_ctx{name=User}, auth={Secret, true}}=Re
     AuthSession = couch_util:get_value("AuthSession", Cookies),
     if AuthSession == undefined ->
         TimeStamp = make_cookie_time(),
-        [cookie_auth_cookie(Req, ?b2l(User), Secret, TimeStamp)];
+        [cookie_auth_cookie(Req, Secret, [{u, User}, {t, TimeStamp}])];
     true ->
         []
     end;
 cookie_auth_header(_Req, _Headers) -> [].
 
-cookie_auth_cookie(Req, User, Secret, TimeStamp) ->
-    SessionData = User ++ ":" ++ erlang:integer_to_list(TimeStamp, 16),
-    Hash = crypto:hmac(sha, Secret, SessionData),
+cookie_auth_cookie(Req, Secret, SessionData) when not is_binary(SessionData) ->
+    cookie_auth_cookie(Req, Secret, term_to_binary(SessionData));
+
+cookie_auth_cookie(Req, Secret, SessionData) ->
+    Hash = crypto:hmac(sha256, Secret, SessionData),
     mochiweb_cookies:cookie("AuthSession",
-        couch_util:encodeBase64Url(SessionData ++ ":" ++ ?b2l(Hash)),
+        couch_util:encodeBase64Url(<<SessionData/binary, ":", Hash/binary>>),
         [{path, "/"}] ++ cookie_scheme(Req) ++ max_age() ++ cookie_domain()).
 
 ensure_cookie_auth_secret() ->
@@ -309,7 +313,8 @@ handle_session_req(#httpd{method='POST', mochi_req=MochiReq}=Req, AuthModule) ->
             Secret = ?l2b(ensure_cookie_auth_secret()),
             UserSalt = couch_util:get_value(<<"salt">>, UserProps),
             CurrentTime = make_cookie_time(),
-            Cookie = cookie_auth_cookie(Req, ?b2l(UserName), <<Secret/binary, UserSalt/binary>>, CurrentTime),
+            Cookie = cookie_auth_cookie(Req, <<Secret/binary, UserSalt/binary>>,
+                [{u, UserName}, {t, CurrentTime}]),
             % TODO document the "next" feature in Futon
             {Code, Headers} = case couch_httpd:qs_value(Req, "next", nil) of
                 nil ->


[couchdb] 02/02: introduce hard failure mode for session cookies

Posted by rn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch hard-fail-cookie-mode
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 243ecb11834ec2f125ec75b2035a534e12e6364c
Author: Robert Newson <rn...@apache.org>
AuthorDate: Tue Sep 18 23:38:41 2018 +0100

    introduce hard failure mode for session cookies
---
 src/couch/src/couch_httpd_auth.erl | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/couch/src/couch_httpd_auth.erl b/src/couch/src/couch_httpd_auth.erl
index a7b5c9e..9ca796c 100644
--- a/src/couch/src/couch_httpd_auth.erl
+++ b/src/couch/src/couch_httpd_auth.erl
@@ -204,6 +204,7 @@ cookie_authentication_handler(#httpd{mochi_req=MochiReq}=Req, AuthModule) ->
         SessionData = binary_to_term(SignedData, [safe]),
         User = couch_util:get_value(u, SessionData),
         TimeStamp = couch_util:get_value(t, SessionData),
+        HardFail = couch_util:get_value(f, SessionData),
         % Verify expiry and hash
         CurrentTime = make_cookie_time(),
         case config:get("couch_httpd_auth", "secret", undefined) of
@@ -231,10 +232,12 @@ cookie_authentication_handler(#httpd{mochi_req=MochiReq}=Req, AuthModule) ->
                                 Req#httpd{user_ctx=#user_ctx{
                                     name=User,
                                     roles=couch_util:get_value(<<"roles">>, UserProps, [])
-                                }, auth={FullSecret, TimeLeft < Timeout*0.9}};
+                                }, auth={FullSecret, TimeLeft < Timeout*0.9, HardFail}};
                             _Else ->
                                 Req
                         end;
+                    HardFail ->
+                        throw({unauthorized, <<"Cookie has expired.">>});
                     true ->
                         Req
                 end
@@ -243,7 +246,7 @@ cookie_authentication_handler(#httpd{mochi_req=MochiReq}=Req, AuthModule) ->
     end.
 
 cookie_auth_header(#httpd{user_ctx=#user_ctx{name=null}}, _Headers) -> [];
-cookie_auth_header(#httpd{user_ctx=#user_ctx{name=User}, auth={Secret, true}}=Req, Headers) ->
+cookie_auth_header(#httpd{user_ctx=#user_ctx{name=User}, auth={Secret, true, HardFail}}=Req, Headers) ->
     % Note: we only set the AuthSession cookie if:
     %  * a valid AuthSession cookie has been received
     %  * we are outside a 10% timeout window
@@ -256,7 +259,8 @@ cookie_auth_header(#httpd{user_ctx=#user_ctx{name=User}, auth={Secret, true}}=Re
     AuthSession = couch_util:get_value("AuthSession", Cookies),
     if AuthSession == undefined ->
         TimeStamp = make_cookie_time(),
-        [cookie_auth_cookie(Req, Secret, [{u, User}, {t, TimeStamp}])];
+        [cookie_auth_cookie(Req, Secret,
+            [{u, User}, {t, TimeStamp}, {f, HardFail}])];
     true ->
         []
     end;
@@ -301,6 +305,7 @@ handle_session_req(#httpd{method='POST', mochi_req=MochiReq}=Req, AuthModule) ->
     end,
     UserName = ?l2b(extract_username(Form)),
     Password = ?l2b(couch_util:get_value("password", Form, "")),
+    HardFail = couch_httpd:qs_value(Req, "hardfail", "false") == "true",
     couch_log:debug("Attempt Login: ~s",[UserName]),
     {ok, UserProps, _AuthCtx} = case AuthModule:get_user_creds(Req, UserName) of
         nil -> {ok, [], nil};
@@ -314,7 +319,7 @@ handle_session_req(#httpd{method='POST', mochi_req=MochiReq}=Req, AuthModule) ->
             UserSalt = couch_util:get_value(<<"salt">>, UserProps),
             CurrentTime = make_cookie_time(),
             Cookie = cookie_auth_cookie(Req, <<Secret/binary, UserSalt/binary>>,
-                [{u, UserName}, {t, CurrentTime}]),
+                [{u, UserName}, {t, CurrentTime}, {f, HardFail}]),
             % TODO document the "next" feature in Futon
             {Code, Headers} = case couch_httpd:qs_value(Req, "next", nil) of
                 nil ->