server: config, migrations, redacted logging, /healthz entrypoint (#16)
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
## Logging with secret redaction (DESIGN 11.3, threat T6; mechanism FIXED owner-delegated, #16).
|
||||
## Secrets are never trusted to call-site discipline: registered values are scrubbed wherever they
|
||||
## appear, and fields whose name is sensitive are masked even when the value was never registered.
|
||||
import std/[unittest, strutils]
|
||||
import eventlog
|
||||
|
||||
type Capture = ref object
|
||||
lines: seq[string]
|
||||
|
||||
proc capture(): tuple[lg: Logger, cap: Capture] =
|
||||
let cap = Capture()
|
||||
let lg = newLogger(lvInfo, proc(line: string) = cap.lines.add line)
|
||||
(lg, cap)
|
||||
|
||||
suite "redaction by value":
|
||||
test "a secret passed as a field value never appears in the output":
|
||||
let (lg, cap) = capture()
|
||||
lg.addSecret("s3cr3t-value")
|
||||
lg.log(lvInfo, "join", [("player", "anna"), ("key", "s3cr3t-value")])
|
||||
check cap.lines.len == 1
|
||||
check "s3cr3t-value" notin cap.lines[0]
|
||||
check "[redacted]" in cap.lines[0]
|
||||
check "player=anna" in cap.lines[0]
|
||||
|
||||
test "a secret inside free text, the event name or a nested value is scrubbed too":
|
||||
let (lg, cap) = capture()
|
||||
lg.addSecret("s3cr3t-value")
|
||||
lg.log(lvInfo, "saw s3cr3t-value twice: s3cr3t-value", [("detail", "cookie=s3cr3t-value;")])
|
||||
check "s3cr3t-value" notin cap.lines[0]
|
||||
check cap.lines[0].count("[redacted]") == 3
|
||||
|
||||
test "redact() is usable on any string":
|
||||
let (lg, _) = capture()
|
||||
lg.addSecret("s3cr3t-value")
|
||||
check lg.redact("a s3cr3t-value b") == "a [redacted] b"
|
||||
check lg.redact("nothing here") == "nothing here"
|
||||
|
||||
test "two secrets, one a prefix of the other, are both removed":
|
||||
let (lg, cap) = capture()
|
||||
lg.addSecret("abcdefgh")
|
||||
lg.addSecret("abcdefgh-1234")
|
||||
lg.log(lvInfo, "x", [("a", "abcdefgh"), ("b", "abcdefgh-1234")])
|
||||
check "abcdefgh" notin cap.lines[0]
|
||||
check "[redacted]" in cap.lines[0]
|
||||
|
||||
test "registering a short value is refused: it would shred every line":
|
||||
let (lg, _) = capture()
|
||||
var raised = false
|
||||
try:
|
||||
lg.addSecret("abc")
|
||||
except ValueError:
|
||||
raised = true
|
||||
check raised
|
||||
lg.addSecret("abcd1234") # MinSecretLen
|
||||
check MinSecretLen == 8
|
||||
|
||||
suite "redaction by field name":
|
||||
test "sensitive field names are masked even for unregistered values":
|
||||
let (lg, cap) = capture()
|
||||
lg.log(lvInfo, "x", [("key", "plainvalue"), ("session", "plainvalue"),
|
||||
("player_key", "plainvalue"), ("gm_key", "plainvalue"),
|
||||
("secret", "plainvalue"), ("cookie", "plainvalue"),
|
||||
("token_hash", "plainvalue"), ("set-cookie", "plainvalue"),
|
||||
("authorization", "plainvalue")])
|
||||
check "plainvalue" notin cap.lines[0]
|
||||
check cap.lines[0].count("[redacted]") == 9
|
||||
|
||||
test "field names are matched case-insensitively and exactly":
|
||||
let (lg, cap) = capture()
|
||||
lg.log(lvInfo, "x", [("Cookie", "plainvalue"), ("monkey", "plainvalue")])
|
||||
check "Cookie=[redacted]" in cap.lines[0]
|
||||
check "monkey=plainvalue" in cap.lines[0] # a name that merely contains "key" is not sensitive
|
||||
check isSensitiveField("PlayER_KEY")
|
||||
check not isSensitiveField("monkey")
|
||||
check not isSensitiveField("key_count")
|
||||
|
||||
suite "line format and levels":
|
||||
test "every line carries an ISO-8601 UTC timestamp, the level and the event":
|
||||
let (lg, cap) = capture()
|
||||
lg.log(lvWarn, "slow", [("total_ms", "120")])
|
||||
let line = cap.lines[0]
|
||||
check line[0..3].allCharsInSet(Digits)
|
||||
check "T" in line
|
||||
check line.endsWith("Z") or "Z " in line
|
||||
check " WARN slow " in line
|
||||
check "total_ms=120" in line
|
||||
|
||||
test "values with spaces or quotes are quoted, so one line stays one record":
|
||||
let (lg, cap) = capture()
|
||||
lg.log(lvInfo, "x", [("note", "a b"), ("q", "he said \"hi\"")])
|
||||
check "note=\"a b\"" in cap.lines[0]
|
||||
check "q=\"he said \\\"hi\\\"\"" in cap.lines[0]
|
||||
|
||||
test "a value cannot forge a second log line":
|
||||
let (lg, cap) = capture()
|
||||
lg.log(lvInfo, "x", [("echo", "one\ntwo\r\u0000")])
|
||||
check cap.lines.len == 1
|
||||
check "one\\ntwo\\r\\x00" in cap.lines[0]
|
||||
|
||||
test "messages below the level are dropped":
|
||||
let cap = Capture()
|
||||
let lg = newLogger(lvWarn, proc(line: string) = cap.lines.add line)
|
||||
lg.log(lvInfo, "dropped")
|
||||
lg.log(lvDebug, "dropped")
|
||||
lg.log(lvWarn, "kept")
|
||||
check cap.lines.len == 1
|
||||
check "kept" in cap.lines[0]
|
||||
|
||||
test "no fields is still one valid line":
|
||||
let (lg, cap) = capture()
|
||||
lg.log(lvInfo, "server_start")
|
||||
check cap.lines.len == 1
|
||||
check "INFO server_start" in cap.lines[0]
|
||||
Reference in New Issue
Block a user