SCUA 0.45.0 lets secrets read and write base32, the
alphabet authenticator apps use for their seeds, so a program can turn a
TOTP seed into one-time codes without the seed ever becoming text. The
release also adds form encoding for secrets, and the HTTP client now
returns a server's answer when the server replies before an upload has
finished.
#secrets can now read and write base32
secrets.decode(s, "base32") reads a seed written in
base32, such as the one in an authenticator app's setup link, into a
secret that holds its bytes. Upper and lower case both work, and the
= padding is optional. hash.hmac takes a
secret as its key, so a one-time code takes a short function:
import secrets
import hash
import bytes
fn totp(seed, unix_time)
let step = unix_time // 30
let counter = []
for i in 0:8 do
push(counter, (step >> (56 - 8 * i)) & 0xff)
end
let mac = hash.hmac("sha1", seed, bytes.from_array(counter))?
let offset = bytes.u8(mac, 19) & 0x0f
let code = (bytes.u32be(mac, offset) & 0x7fffffff) % 1000000
return Ok(format("{:06}", code))
end
let seed = secrets.decode(secrets.of("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"), "base32")?
print(totp(seed, 59)?)
print(totp(seed, 1111111109)?)
print(totp(seed, 1234567890)?)
$ scua totp.scua
287082
081804
005924
Those are the last six digits of the codes RFC 6238 lists for its
SHA-1 test seed at those three times. In a real program the seed comes
from env.get("TOTP_SEED", { as_secret = true }) or a file
read with { as_secret = true }, and it stays a secret the
whole way: printing seed shows
<secret>.
secrets.encode(seed, "base32") goes the other way. It
writes upper case without padding, which is what an
otpauth:// link wants, and secrets.format
builds the link without the seed becoming text:
import secrets
let seed = secrets.decode(secrets.of("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"), "base32")?
let link = secrets.format("otpauth://totp/Example:ann?secret={}&issuer=Example", secrets.encode(seed, "base32"))
print(link)
print(link.reveal()) -- shown here only to see what it holds
$ scua link.scua
<secret>
otpauth://totp/Example:ann?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=Example
secrets.redact now finds a secret written in base32 too,
so a log line that echoes the link comes out as
secret=[secret]. Decoding refuses spaces inside the text,
so take the spaces out of a seed shown in groups, such as
JBSW Y3DP, before you decode it. See Handle secrets.
#secrets can now write form-encoded text
A login form posted as
application/x-www-form-urlencoded, and a query value read
the same way, write a space as +.
secrets.encode(pw, "form") is percent encoding with that
one difference, and it writes a + in the value as
%2B so the server can't mistake it for a space:
import secrets
let pw = secrets.of("correct horse+battery")
let body = secrets.format("user=ann&password={}", secrets.encode(pw, "form"))
print(body.reveal()) -- shown here only to see what it holds
let echoed = "server said: bad login for user=ann&password=correct+horse%2Bbattery"
print(secrets.redact(echoed, pw))
$ scua form.scua
user=ann&password=correct+horse%2Bbattery
server said: bad login for user=ann&password=[secret]
secrets.redact recognises a secret in its form encoding
as well, as the second line shows.
#http.post now returns a server's early answer
A server can answer before it has read the whole body.
http.serve does this with a 413 when a body is
over its max_body. http.post,
http.request and http.open now return that
answer, with its status and body, so code can act on the
413 it was sent. This server takes bodies up to 1 MiB:
import http
import bytes
fn handle(req)
return { status = 200, body = bytes.from_string(`got {len(req.body)} bytes\n`) }
end
http.serve(handle, { bind = "127.0.0.1:8091", max_body = 1024 * 1024 })?
Start it with
scua --allow-serve=127.0.0.1:8091 server.scua. This client
sends it 64 MiB:
import http
import bytes
fn upload(data)
let res = http.post("http://127.0.0.1:8091/upload", data)?
return Ok(`{res.status} {bytes.to_string(res.body)?}`)
end
let photo = b"x"
for i in 0:26 do
photo = photo .. photo
end
print(upload(photo)?)
$ scua --allow-net=127.0.0.1 client.scua
413 request body of 67,108,864 bytes is over this server's limit of 1,048,576 bytes
A server that sends 100 Continue still gets the whole
body. On Windows, for an https:// call, and in a program
that neither serves nor runs with --io=async, the call can
still return Error({ kind = "connection_closed" }) in this
case.
#Upgrading
- On a server that sets
max_upload, a body overmax_bodyreaches the handler as a handle thathttp.bodyaccepts.len(req.body)already refused that handle and answered the client413(or411for a chunked body). A template,..,tostring,printand==against a string or bytes went ahead instead, so the route answered200having read none of the body. They now refuse the same way. Accept the body withhttp.body(req, { max_bytes })before using it. Inside another value, as inprint(req), the body still shows as<request body>. - A module file named like a built-in module, such as a
secrets.scuaof your own, is now reported by name, with a message that tells you to rename it. For a file on--mod-pathor beside a module you import, the error is at theimport. For a file beside the script you run, it comes when the program calls a function that only the file has.
#What's next
The next step on the roadmap is still the public package registry: a static index served from a CDN, with a signed transparency log from the first public publish. For the agent runtime, snapshot, fork and replay of agent sessions are designed and queued.
The changelog lists everything in 0.45.0.