server_key(){
IDFILE="${IDFILE:-${_DATA:-.}/serverkey}"
- if ! grep -m1 -xE '.{512}' "$IDFILE"; then
+ if [ "$(stat -c %s "$IDFILE")" -ne 512 ] || ! cat "$IDFILE"; then
dd count=1 bs=512 if=/dev/urandom \
| tee "$IDFILE"
fi 2>&-
}
randomid(){
- dd bs=12 count=1 if=/dev/urandom \
- | slopecode 2>&-
+ dd bs=12 count=1 if=/dev/urandom 2>&- \
+ | slopecode
}
timeid(){
$((d / 256 % 256)) \
$((d % 256))
)"
- dd bs=8 count=1 if=/dev/urandom
- } | slopecode 2>&-
+ dd bs=8 count=1 if=/dev/urandom 2>&-
+ } | slopecode
}
-checkid(){ grep -m 1 -xE '[0-9a-zA-Z:_]{16}'; }
+checkid(){ grep -m 1 -xE '[0-9a-zA-Z:=]{16}'; }
+
+transid(){
+ # transaction ID to modify a given file
+ local file="$1"
+ { stat -c %F%i%n%N%s%Y "$file" 2>&-
+ printf %s "$SESSION_ID"
+ server_key
+ } | sha256sum | cut -d\ -f1
+}
update_session(){
local session sid time sig serverkey checksig
checksig="${checksig%% *}"
d=$(date +%s)
- if [ "$checksig" != "$sig" -o "$time" -lt "$d" ] 2>&-; then
+ if ! [ "$checksig" = "$sig" \
+ -a "$time" -ge "$d" \
+ -a "$(printf %s "$sid" |checkid)" ] 2>&-
+ then
+ debug Setting up new session
sid="$(randomid)"
fi
}
SESSION_ID="$(update_session)"
-SET_COOKIE 0 "session=$SESSION_ID"
+SET_COOKIE 0 session="$SESSION_ID" Path=/ SameSite=Strict HttpOnly
SESSION_ID="${SESSION_ID%%-*}"