aboutsummaryrefslogtreecommitdiff
diff refs
from: back
to: back
| flip
diff options
context:
space:
mode:
-rw-r--r--cgit.c22
-rw-r--r--cgitrc.5.txt3
-rw-r--r--filters/simple-authentication.lua200
3 files changed, 94 insertions, 131 deletions
diff --git a/cgit.c b/cgit.c
index be1265d..c52ef33 100644
--- a/cgit.c
+++ b/cgit.c
@@ -614,19 +614,22 @@ static inline void open_auth_filter(struct cgit_context *ctx, const char *functi
ctx->qry.url ? ctx->qry.url : "");
}
-/* We intentionally keep this rather small, instead of looping and
- * feeding it to the filter a couple bytes at a time. This way, the
- * filter itself does not need to handle any denial of service or
- * buffer bloat issues. If this winds up being too small, people
- * will complain on the mailing list, and we'll increase it as needed. */
#define MAX_AUTHENTICATION_POST_BYTES 4096
-/* The filter is expected to spit out "Status: " and all headers. */
static inline void authenticate_post(struct cgit_context *ctx)
{
- char buffer[MAX_AUTHENTICATION_POST_BYTES];
- int len;
+ if (ctx->env.http_referer && strlen(ctx->env.http_referer) > 0) {
+ html("Status: 302 Redirect\n");
+ html("Cache-Control: no-cache, no-store\n");
+ htmlf("Location: %s\n", ctx->env.http_referer);
+ } else {
+ html("Status: 501 Missing Referer\n");
+ html("Cache-Control: no-cache, no-store\n\n");
+ exit(0);
+ }
open_auth_filter(ctx, "authenticate-post");
+ char buffer[MAX_AUTHENTICATION_POST_BYTES];
+ int len;
len = ctx->env.content_length;
if (len > MAX_AUTHENTICATION_POST_BYTES)
len = MAX_AUTHENTICATION_POST_BYTES;
@@ -634,7 +637,10 @@ static inline void authenticate_post(struct cgit_context *ctx)
die_errno("Could not read POST from stdin");
if (write(STDOUT_FILENO, buffer, len) < 0)
die_errno("Could not write POST to stdout");
+ /* The filter may now spit out a Set-Cookie: ... */
cgit_close_filter(ctx->cfg.auth_filter);
+
+ html("\n");
exit(0);
}
diff --git a/cgitrc.5.txt b/cgitrc.5.txt
index 682d8bb..c45dbd3 100644
--- a/cgitrc.5.txt
+++ b/cgitrc.5.txt
@@ -662,8 +662,7 @@ auth filter::
the http cookie and return a 0 if it is invalid or 1 if it is invalid,
in the exit code / close function. If the filter action is
"authenticate-post", this filter receives POST'd parameters on
- standard input, and should write a complete CGI request, preferably
- with a 302 redirect, and write to output one or more "Set-Cookie"
+ standard input, and should write to output one or more "Set-Cookie"
HTTP headers, each followed by a newline.
Please see `filters/simple-authentication.lua` for a clear example
diff --git a/filters/simple-authentication.lua b/filters/simple-authentication.lua
index 5935d08..4cd4983 100644
--- a/filters/simple-authentication.lua
+++ b/filters/simple-authentication.lua
@@ -33,28 +33,15 @@ local secret = "BE SURE TO CUSTOMIZE THIS STRING TO SOMETHING BIG AND RANDOM"
--
--
--- Sets HTTP cookie headers based on post and sets up redirection.
+-- Sets HTTP cookie headers based on post
function authenticate_post()
local password = users[post["username"]]
- local redirect = validate_value(post["redirect"])
-
- if redirect == nil then
- not_found()
- return 0
- end
-
- redirect_to(redirect)
-
- -- TODO: Implement time invariant string comparison function to mitigate timing attack.
+ -- TODO: Implement time invariant string comparison function to mitigate against timing attack.
if password == nil or password ~= post["password"] then
- set_cookie("cgitauth", "")
+ construct_cookie("", "cgitauth")
else
- -- One week expiration time
- local username = secure_value(post["username"], os.time() + 604800)
- set_cookie("cgitauth", username)
+ construct_cookie(post["username"], "cgitauth")
end
-
- html("\n")
return 0
end
@@ -67,8 +54,8 @@ function authenticate_cookie()
return 1
end
- local username = validate_value(get_cookie(http["cookie"], "cgitauth"))
- if username == nil or not accepted_users[username:lower()] then
+ local username = validate_cookie(get_cookie(http["cookie"], "cgitauth"))
+ if username == nil or not accepted_users[username] then
return 0
else
return 1
@@ -81,9 +68,6 @@ function body()
html("<form method='post' action='")
html_attr(cgit["login"])
html("'>")
- html("<input type='hidden' name='redirect' value='")
- html_attr(secure_value(cgit["url"], 0))
- html("' />")
html("<table>")
html("<tr><td><label for='username'>Username:</label></td><td><input id='username' name='username' autofocus /></td></tr>")
html("<tr><td><label for='password'>Password:</label></td><td><input id='password' name='password' type='password' /></td></tr>")
@@ -94,10 +78,81 @@ function body()
end
+--
+--
+-- Cookie construction and validation helpers.
+--
+--
+
+local crypto = require("crypto")
+
+-- Returns username of cookie if cookie is valid. Otherwise returns nil.
+function validate_cookie(cookie)
+ local i = 0
+ local username = ""
+ local expiration = 0
+ local salt = ""
+ local hmac = ""
+
+ if cookie:len() < 3 or cookie:sub(1, 1) == "|" then
+ return nil
+ end
+
+ for component in string.gmatch(cookie, "[^|]+") do
+ if i == 0 then
+ username = component
+ elseif i == 1 then
+ expiration = tonumber(component)
+ if expiration == nil then
+ expiration = 0
+ end
+ elseif i == 2 then
+ salt = component
+ elseif i == 3 then
+ hmac = component
+ else
+ break
+ end
+ i = i + 1
+ end
+
+ if hmac == nil or hmac:len() == 0 then
+ return nil
+ end
+
+ -- TODO: implement time invariant comparison to prevent against timing attack.
+ if hmac ~= crypto.hmac.digest("sha1", username .. "|" .. tostring(expiration) .. "|" .. salt, secret) then
+ return nil
+ end
+
+ if expiration <= os.time() then
+ return nil
+ end
+
+ return username:lower()
+end
+
+function construct_cookie(username, cookie)
+ local authstr = ""
+ if username:len() > 0 then
+ -- One week expiration time
+ local expiration = os.time() + 604800
+ local salt = crypto.hex(crypto.rand.bytes(16))
+
+ authstr = username .. "|" .. tostring(expiration) .. "|" .. salt
+ authstr = authstr .. "|" .. crypto.hmac.digest("sha1", authstr, secret)
+ end
+
+ html("Set-Cookie: " .. cookie .. "=" .. authstr .. "; HttpOnly")
+ if http["https"] == "yes" or http["https"] == "on" or http["https"] == "1" then
+ html("; secure")
+ end
+ html("\n")
+end
--
--
--- Wrapper around filter API, exposing the http table, the cgit table, and the post table to the above functions.
+-- Wrapper around filter API follows below, exposing the http table, the cgit table, and the post table to the above functions.
--
--
@@ -142,7 +197,7 @@ end
--
--
--- Utility functions based on keplerproject/wsapi.
+-- Utility functions follow below, based on keplerproject/wsapi.
--
--
@@ -156,16 +211,6 @@ function url_decode(str)
return str
end
-function url_encode(str)
- if not str then
- return ""
- end
- str = string.gsub(str, "\n", "\r\n")
- str = string.gsub(str, "([^%w ])", function (c) return string.format("%%%02X", string.byte(c)) end)
- str = string.gsub(str, " ", "+")
- return str
-end
-
function parse_qs(qs)
local tab = {}
for key, val in string.gmatch(qs, "([^&=]+)=([^&=]*)&?") do
@@ -178,90 +223,3 @@ function get_cookie(cookies, name)
cookies = string.gsub(";" .. cookies .. ";", "%s*;%s*", ";")
return url_decode(string.match(cookies, ";" .. name .. "=(.-);"))
end
-
-
---
---
--- Cookie construction and validation helpers.
---
---
-
-local crypto = require("crypto")
-
--- Returns value of cookie if cookie is valid. Otherwise returns nil.
-function validate_value(cookie)
- local i = 0
- local value = ""
- local expiration = 0
- local salt = ""
- local hmac = ""
-
- if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then
- return nil
- end
-
- for component in string.gmatch(cookie, "[^|]+") do
- if i == 0 then
- value = component
- elseif i == 1 then
- expiration = tonumber(component)
- if expiration == nil then
- expiration = 0
- end
- elseif i == 2 then
- salt = component
- elseif i == 3 then
- hmac = component
- else
- break
- end
- i = i + 1
- end
-
- if hmac == nil or hmac:len() == 0 then
- return nil
- end
-
- -- TODO: implement time invariant comparison to prevent against timing attack.
- if hmac ~= crypto.hmac.digest("sha1", value .. "|" .. tostring(expiration) .. "|" .. salt, secret) then
- return nil
- end
-
- if expiration ~= 0 and expiration <= os.time() then
- return nil
- end
-
- return url_decode(value)
-end
-
-function secure_value(value, expiration)
- if value == nil or value:len() <= 0 then
- return ""
- end
-
- local authstr = ""
- local salt = crypto.hex(crypto.rand.bytes(16))
- value = url_encode(value)
- authstr = value .. "|" .. tostring(expiration) .. "|" .. salt
- authstr = authstr .. "|" .. crypto.hmac.digest("sha1", authstr, secret)
- return authstr
-end
-
-function set_cookie(cookie, value)
- html("Set-Cookie: " .. cookie .. "=" .. value .. "; HttpOnly")
- if http["https"] == "yes" or http["https"] == "on" or http["https"] == "1" then
- html("; secure")
- end
- html("\n")
-end
-
-function redirect_to(url)
- html("Status: 302 Redirect\n")
- html("Cache-Control: no-cache, no-store\n")
- html("Location: " .. url .. "\n")
-end
-
-function not_found()
- html("Status: 404 Not Found\n")
- html("Cache-Control: no-cache, no-store\n\n")
-end