Skip to content

Commit 5506d57

Browse files
committed
Release 0.4.2.0: key normalization and landing-page public key
1 parent 98babc3 commit 5506d57

6 files changed

Lines changed: 119 additions & 14 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# Changelog
22

3+
## 0.4.2.0 — 2026-06-10
4+
5+
- **Signing: public-key derivation and rendering** — new `toPublicKey`
6+
(derive the `PublicKey` for a `SecretKey`) and `renderPublicKey` (render
7+
the `name:base64` trust-anchor format clients put in
8+
`trusted-public-keys`).
9+
- **Signing: `normalizeKeyText`** — strips byte-order marks and surrounding
10+
whitespace from key text. The server applies it to `CACHE_API_KEY` and
11+
the signing key file, so keys that picked up a BOM or stray CRLF in
12+
transit load byte-clean instead of silently failing auth or signing.
13+
- **Server: the landing page shows the cache public key**, derived at
14+
startup from the live signing key, as a copyable `trusted-public-keys`
15+
line. The published trust anchor can no longer drift from the key in
16+
use.
17+
- README: the public cache key now matches the key the server signs with.
18+
- Removed the CI seed action and its README section; native pushing from
19+
nova-nix replaces it.
20+
321
## 0.4.1.1 — 2026-06-10
422

523
- Documentation-only release: the 0.4.1.0 entry below now records everything

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ cabal run --flag server nova-cache-server -- --port 5000 --store ./nix-cache
7171

7272
| Method | Path | Description |
7373
| --- | --- | --- |
74+
| `GET` | `/` | Landing page: live stats and the cache public key |
7475
| `GET` | `/nix-cache-info` | Cache metadata |
7576
| `GET` | `/narinfo-hashes` | All cached narinfo hashes, newline-delimited |
7677
| `GET` | `/<hash>.narinfo` | Fetch a narinfo |

‎exe/Main.hs‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Network.Wai
2727
import qualified Network.Wai.Handler.Warp as Warp
2828
import Network.Wai.Middleware.RequestLogger (logStdout)
2929
import NovaCache.NarInfo (NarInfo (..), parseNarInfo, renderNarInfo)
30-
import NovaCache.Signing (SecretKey, parseSecretKey, sign)
30+
import NovaCache.Signing (SecretKey, normalizeKeyText, parseSecretKey, renderPublicKey, sign, toPublicKey)
3131
import NovaCache.Store (CacheInfo (..), FileStore, getCacheInfo, listNarInfoHashes, newFileStore, readNar, readNarInfo, writeNar, writeNarInfo)
3232
import NovaCache.StorePath (defaultStoreDir, parseStorePath, storePathHashString)
3333
import NovaCache.Validate (validateNarInfo)
@@ -78,7 +78,10 @@ maxNarBodySize = 4 * 1024 * 1024 * 1024 -- 4 GB
7878
data Config = Config
7979
{ cfgStore :: !FileStore,
8080
cfgApiKey :: !(Maybe BS.ByteString),
81-
cfgSigningKey :: !(Maybe SecretKey)
81+
cfgSigningKey :: !(Maybe SecretKey),
82+
-- | The rendered @name:base64@ public key line, derived from the
83+
-- signing key at startup; shown on the landing page.
84+
cfgPublicKey :: !(Maybe Text)
8285
}
8386

8487
-- ---------------------------------------------------------------------------
@@ -107,12 +110,14 @@ main = do
107110

108111
store <- newFileStore storeRoot
109112
sigKey <- loadSigningKey sigKeyPath
113+
pubKey <- derivePublicKeyLine sigKey
110114

111115
let cfg =
112116
Config
113117
{ cfgStore = store,
114-
cfgApiKey = TE.encodeUtf8 . T.pack <$> apiKeyEnv,
115-
cfgSigningKey = sigKey
118+
cfgApiKey = TE.encodeUtf8 . normalizeKeyText . T.pack <$> apiKeyEnv,
119+
cfgSigningKey = sigKey,
120+
cfgPublicKey = pubKey
116121
}
117122

118123
let logRequests = logRequestsEnv /= Just "0"
@@ -121,6 +126,7 @@ main = do
121126
putStrLn ("nova-cache-server listening on port " ++ show port)
122127
putStrLn ("store root: " ++ storeRoot)
123128
putStrLn ("signing: " ++ maybe "disabled" (const "enabled") sigKey)
129+
mapM_ (\k -> putStrLn ("public key: " ++ T.unpack k)) pubKey
124130
putStrLn ("write auth: " ++ maybe "disabled (open writes!)" (const "enabled") (cfgApiKey cfg))
125131
putStrLn ("request logging: " ++ if logRequests then "enabled" else "disabled (LOG_REQUESTS=0)")
126132

@@ -147,12 +153,23 @@ loadSigningKey :: Maybe FilePath -> IO (Maybe SecretKey)
147153
loadSigningKey Nothing = pure Nothing
148154
loadSigningKey (Just path) = do
149155
raw <- BS.readFile path
150-
case first show (TE.decodeUtf8' raw) >>= parseSecretKey . T.strip of
156+
case first show (TE.decodeUtf8' raw) >>= parseSecretKey . normalizeKeyText of
151157
Left err -> do
152158
hPutStrLn stderr ("WARNING: failed to load signing key: " ++ err)
153159
pure Nothing
154160
Right sk -> pure (Just sk)
155161

162+
-- | Derive the rendered public key line from the signing key, if any.
163+
-- The landing page shows this line, so the published trust anchor always
164+
-- matches the key in use; on a derivation failure the page omits it.
165+
derivePublicKeyLine :: Maybe SecretKey -> IO (Maybe Text)
166+
derivePublicKeyLine Nothing = pure Nothing
167+
derivePublicKeyLine (Just sk) = case toPublicKey sk of
168+
Left err -> do
169+
hPutStrLn stderr ("WARNING: cannot derive the public key from the signing key: " ++ err)
170+
pure Nothing
171+
Right pk -> pure (Just (renderPublicKey pk))
172+
156173
-- ---------------------------------------------------------------------------
157174
-- WAI application
158175
-- ---------------------------------------------------------------------------
@@ -165,7 +182,7 @@ app cfg req respond = case (requestMethod req, pathInfo req) of
165182
pathCount <- length <$> listNarInfoHashes (cfgStore cfg)
166183
let info = getCacheInfo (cfgStore cfg)
167184
signingEnabled = isJust (cfgSigningKey cfg)
168-
body = TE.encodeUtf8 (landingHtml info signingEnabled pathCount)
185+
body = TE.encodeUtf8 (landingHtml info signingEnabled (cfgPublicKey cfg) pathCount)
169186
respond (responseLBS HTTP.status200 htmlHeaders (BL.fromStrict body))
170187
-- GET /nix-cache-info
171188
("GET", ["nix-cache-info"]) ->
@@ -370,11 +387,12 @@ boolText :: Bool -> Text
370387
boolText True = "1"
371388
boolText False = "0"
372389

373-
-- | The landing page served at @GET /@. Static apart from three live
374-
-- values (store-path count, store dir, signing status); styled to match
375-
-- novavero.ai. No user input is interpolated, so no escaping is needed.
376-
landingHtml :: CacheInfo -> Bool -> Int -> Text
377-
landingHtml info signingEnabled pathCount =
390+
-- | The landing page served at @GET /@. Static apart from four live
391+
-- values (store-path count, store dir, signing status, public key); styled
392+
-- to match novavero.ai. Nothing user-supplied is interpolated — the key
393+
-- line is operator configuration — so no escaping is needed.
394+
landingHtml :: CacheInfo -> Bool -> Maybe Text -> Int -> Text
395+
landingHtml info signingEnabled pubKey pathCount =
378396
T.unlines
379397
[ "<!DOCTYPE html>",
380398
"<html lang=\"en\">",
@@ -416,7 +434,7 @@ landingHtml info signingEnabled pathCount =
416434
"<div class=\"stat\"><div class=\"value\">" <> T.pack (show (ciPriority info)) <> "</div><div class=\"label\">priority</div></div>",
417435
"</div>",
418436
"<h2>Use it</h2>",
419-
"<pre><code>substituters = https://cache.novavero.ai</code></pre>",
437+
"<pre><code>substituters = https://cache.novavero.ai" <> trustAnchorLine <> "</code></pre>",
420438
"<p>Store dir: <code>" <> ciStoreDir info <> "</code> &middot; protocol endpoints: <code>/nix-cache-info</code>, <code>/&lt;hash&gt;.narinfo</code>, <code>/nar/&lt;file&gt;</code></p>",
421439
"<h2>What this is</h2>",
422440
"<p>The binary cache behind the Novavero Nix toolchain. Powered by <a href=\"https://github.com/Novavero-AI/nova-cache\">nova-cache</a>, a Haskell implementation of the Nix binary cache protocol. Read about the first package built by Nix natively on Windows on <a href=\"https://novavero.ai/blog/first-native-windows-nix-build.html\">the blog</a>.</p>",
@@ -425,6 +443,8 @@ landingHtml info signingEnabled pathCount =
425443
"</body>",
426444
"</html>"
427445
]
446+
where
447+
trustAnchorLine = maybe "" ("\ntrusted-public-keys = " <>) pubKey
428448

429449
-- | The Novavero mark (the novavero.ai favicon), inlined so the page stays
430450
-- a single self-contained response with no external assets.

‎nova-cache.cabal‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.0
22
name: nova-cache
3-
version: 0.4.1.1
3+
version: 0.4.2.0
44
synopsis: Pure-first Nix binary cache protocol library
55
description:
66
A pure-first library implementing the Nix binary cache protocol —

‎src/NovaCache/Signing.hs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module NovaCache.Signing
99
PublicKey (..),
1010
parseSecretKey,
1111
parsePublicKey,
12+
normalizeKeyText,
13+
toPublicKey,
14+
renderPublicKey,
1215
fingerprint,
1316
sign,
1417
verify,
@@ -76,10 +79,24 @@ fingerprintSep = ";"
7679
referenceSep :: Text
7780
referenceSep = ","
7881

82+
-- | The Unicode byte-order mark. Windows tooling (PowerShell pipes and
83+
-- redirects in particular) prepends it to text in transit; it is never
84+
-- legal key material.
85+
bomChar :: Char
86+
bomChar = '\xFEFF'
87+
7988
-- ---------------------------------------------------------------------------
8089
-- Key parsing
8190
-- ---------------------------------------------------------------------------
8291

92+
-- | Normalize key text picked up from an environment variable or a file:
93+
-- drop byte-order marks and surrounding whitespace. 'T.strip' alone is not
94+
-- enough — a BOM is not whitespace, so it survives stripping and silently
95+
-- corrupts the key. None of the dropped characters can be legitimate key
96+
-- material, so normalizing is always safe.
97+
normalizeKeyText :: Text -> Text
98+
normalizeKeyText = T.strip . T.filter (/= bomChar)
99+
83100
-- | Parse a @name:base64@ secret key string.
84101
parseSecretKey :: Text -> Either String SecretKey
85102
parseSecretKey txt = do
@@ -94,6 +111,22 @@ parsePublicKey txt = do
94111
expectSize ed25519PublicKeySize "public key" decoded
95112
pure PublicKey {pkName = keyName, pkBytes = decoded}
96113

114+
-- | Derive the public key corresponding to a secret key, keeping the name.
115+
--
116+
-- The 64-byte secret key carries the public half in its second half, but
117+
-- deriving from the seed through the crypto library validates the key
118+
-- material instead of trusting it.
119+
toPublicKey :: SecretKey -> Either String PublicKey
120+
toPublicKey (SecretKey keyName secretBytes) = do
121+
sk <- cryptoSecretKey (BS.take ed25519SeedSize secretBytes)
122+
pure PublicKey {pkName = keyName, pkBytes = convert (Ed25519.toPublic sk)}
123+
124+
-- | Render a public key in Nix's @name:base64@ trust-anchor format — the
125+
-- exact string a client puts in @trusted-public-keys@.
126+
renderPublicKey :: PublicKey -> Text
127+
renderPublicKey (PublicKey keyName bytes) =
128+
keyName <> keySeparator <> TE.decodeLatin1 (B64.encode bytes)
129+
97130
-- | Split a @name:base64@ string and decode the base64 payload.
98131
splitAndDecode :: Text -> String -> Either String (Text, ByteString)
99132
splitAndDecode txt label = case T.breakOn keySeparator txt of

‎test/Main.hs‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,40 @@ testSigning =
472472
pure False
473473
Right sig ->
474474
let tampered = ni {NarInfo.niNarSize = 999999}
475-
in assertFalse "verify rejects tampered" (Signing.verify pk tampered sig)
475+
in assertFalse "verify rejects tampered" (Signing.verify pk tampered sig),
476+
test "toPublicKey derives the verifying key" $ do
477+
sk <- generateTestSecretKey
478+
case Signing.toPublicKey sk of
479+
Left err -> do
480+
putStrLn (" toPublicKey failed: " ++ err)
481+
pure False
482+
Right pk -> do
483+
okName <- assertEqual "key name carried over" (Signing.skName sk) (Signing.pkName pk)
484+
okBytes <- assertEqual "matches the stored public half" (BS.drop 32 (Signing.skBytes sk)) (Signing.pkBytes pk)
485+
case Signing.sign sk mkTestNarInfo of
486+
Left err -> do
487+
putStrLn (" sign failed: " ++ err)
488+
pure False
489+
Right sig -> do
490+
okVerify <- assertTrue "derived key verifies a signature" (Signing.verify pk mkTestNarInfo sig)
491+
pure (okName && okBytes && okVerify),
492+
test "renderPublicKey round-trips through parsePublicKey" $ do
493+
sk <- generateTestSecretKey
494+
case Signing.toPublicKey sk of
495+
Left err -> do
496+
putStrLn (" toPublicKey failed: " ++ err)
497+
pure False
498+
Right pk -> case Signing.parsePublicKey (Signing.renderPublicKey pk) of
499+
Left err -> do
500+
putStrLn (" parse failed: " ++ err)
501+
pure False
502+
Right reparsed -> assertEqual "round-trip" pk reparsed,
503+
test "normalizeKeyText strips BOM and whitespace" $ do
504+
ok1 <- assertEqual "BOM stripped" "test-key:abc" (Signing.normalizeKeyText ("\xFEFF" <> "test-key:abc"))
505+
ok2 <- assertEqual "CRLF stripped" "test-key:abc" (Signing.normalizeKeyText "test-key:abc\r\n")
506+
ok3 <- assertEqual "spaces stripped" "test-key:abc" (Signing.normalizeKeyText " test-key:abc ")
507+
ok4 <- assertEqual "clean text unchanged" "test-key:abc" (Signing.normalizeKeyText "test-key:abc")
508+
pure (ok1 && ok2 && ok3 && ok4)
476509
]
477510

478511
-- | Create a test NarInfo for signing tests.

0 commit comments

Comments
 (0)