X-Git-Url: https://gitweb.ps.run/ps-cgit/blobdiff_plain/d6e9200cc35411f3f27426b608bcfdef9348e6d3..23c17d8ff01b96619bc1f71274cb44f1425e10f4:/filters/simple-authentication.lua diff --git a/filters/simple-authentication.lua b/filters/simple-authentication.lua index 4cd4983..de34d09 100644 --- a/filters/simple-authentication.lua +++ b/filters/simple-authentication.lua @@ -12,17 +12,27 @@ -- -- +-- A list of password protected repositories along with the users who can access them. local protected_repos = { glouglou = { laurent = true, jason = true }, qt = { jason = true, bob = true } } +-- Please note that, in production, you'll want to replace this simple lookup +-- table with either a table of salted and hashed passwords (using something +-- smart like scrypt), or replace this table lookup with an external support, +-- such as consulting your system's pam / shadow system, or an external +-- database, or an external validating web service. For testing, or for +-- extremely low-security usage, you may be able, however, to get away with +-- compromising on hardcoding the passwords in cleartext, as we have done here. local users = { jason = "secretpassword", laurent = "s3cr3t", bob = "ilikelua" } +-- All cookies will be authenticated based on this secret. Make it something +-- totally random and impossible to guess. It should be large. local secret = "BE SURE TO CUSTOMIZE THIS STRING TO SOMETHING BIG AND RANDOM" @@ -33,15 +43,28 @@ local secret = "BE SURE TO CUSTOMIZE THIS STRING TO SOMETHING BIG AND RANDOM" -- -- --- Sets HTTP cookie headers based on post +-- Sets HTTP cookie headers based on post and sets up redirection. function authenticate_post() local password = users[post["username"]] - -- TODO: Implement time invariant string comparison function to mitigate against timing attack. + local redirect = validate_value("redirect", post["redirect"]) + + if redirect == nil then + not_found() + return 0 + end + + redirect_to(redirect) + + -- Lua hashes strings, so these comparisons are time invariant. if password == nil or password ~= post["password"] then - construct_cookie("", "cgitauth") + set_cookie("cgitauth", "") else - construct_cookie(post["username"], "cgitauth") + -- One week expiration time + local username = secure_value("username", post["username"], os.time() + 604800) + set_cookie("cgitauth", username) end + + html("\n") return 0 end @@ -54,8 +77,8 @@ function authenticate_cookie() return 1 end - local username = validate_cookie(get_cookie(http["cookie"], "cgitauth")) - if username == nil or not accepted_users[username] then + local username = validate_value("username", get_cookie(http["cookie"], "cgitauth")) + if username == nil or not accepted_users[username:lower()] then return 0 else return 1 @@ -68,6 +91,9 @@ function body() html("
") + html("") html("") html("") html("") @@ -78,81 +104,10 @@ 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 follows below, exposing the http table, the cgit table, and the post table to the above functions. +-- Wrapper around filter API, exposing the http table, the cgit table, and the post table to the above functions. -- -- @@ -177,12 +132,7 @@ function filter_open(...) cgit["repo"] = select(9, ...) cgit["page"] = select(10, ...) cgit["url"] = select(11, ...) - - cgit["login"] = "" - for _ in cgit["url"]:gfind("/") do - cgit["login"] = cgit["login"] .. "../" - end - cgit["login"] = cgit["login"] .. "?p=login" + cgit["login"] = select(12, ...) end @@ -197,7 +147,7 @@ end -- -- --- Utility functions follow below, based on keplerproject/wsapi. +-- Utility functions based on keplerproject/wsapi. -- -- @@ -211,6 +161,16 @@ 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 @@ -223,3 +183,98 @@ 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(expected_field, cookie) + local i = 0 + local value = "" + local field = "" + 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 + field = component + elseif i == 1 then + value = component + elseif i == 2 then + expiration = tonumber(component) + if expiration == nil then + expiration = -1 + end + elseif i == 3 then + salt = component + elseif i == 4 then + hmac = component + else + break + end + i = i + 1 + end + + if hmac == nil or hmac:len() == 0 then + return nil + end + + -- Lua hashes strings, so these comparisons are time invariant. + if hmac ~= crypto.hmac.digest("sha1", field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt, secret) then + return nil + end + + if expiration == -1 or (expiration ~= 0 and expiration <= os.time()) then + return nil + end + + if url_decode(field) ~= expected_field then + return nil + end + + return url_decode(value) +end + +function secure_value(field, 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) + field = url_encode(field) + authstr = field .. "|" .. 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