Artifact e3e116942120815d08a6c087f8301f0e60dea76b0f21fed963ad6218ef4659ef:
- File
internal/sync/auth.go
— part of check-in
[a745f4f2ac]
at
2026-07-28 00:44:04
on branch trunk
— fix: make authenticated sync interoperate with canonical fossil (#207)
* fix: make authenticated sync interoperate with canonical fossil (#203)
Every credentialed sync against a real fossil server was answered "login failed", whether or not the password was right. Push always needs a login, so push was unusable against any real server, as was pull from anything that did not grant anonymous read.
The login algorithm was never the problem. Two wire-level defects were, and each on its own produces that identical, password-independent rejection.
The push and pull cards carried their two codes in the wrong order. The canonical form is `push SERVERCODE PROJECTCODE` — canonical's own client writes `blob_appendf(&send, "push %s %s\n", zSCode, zPCode)` and both ends read the project code out of the second argument (src/xfer.c). We emitted and parsed project code first. A clone therefore adopted the server's server-code as its project code, and since a login signature is salted with the project code, every signature was derived from the wrong value. The same inversion put the server code where a server validates the project code, so an authenticated push was refused with "wrong project" even once the login worked.
The nonce covered a random comment that was never transmitted. The nonce is the hash of every byte following the login card, and the server re-hashes those bytes to check it (src/xfer.c check_tail_hash) — so the comment supplying the randomness has to be on the wire, not merely in the hash. We appended it to the hashed payload only, producing a nonce the server could not reproduce; it rejected the login before comparing the password at all, which is why a wrong password looked exactly like a right one. Comment lines now have a card type, so they can be sent as well as hashed.
Two smaller faults on the same path: a clone treated the server's round-0 "not authorized to clone" as fatal, ending the session before credentials were ever offered, where canonical tolerates that error on the first cycle precisely so the client can retry with the login card it can only build once it knows the project code; and the sync path signed with SyncOpts.ProjectCode rather than the code it had already resolved from the repository, so a caller that let the repo supply its project code — as the push and pull cards allow — panicked instead of authenticating.
The password derivation itself, sha1("<project-code>/<login>/<password>"), was correct and is unchanged. It is now pinned two ways: against the hash the fossil binary actually stores in user.pw, and against a login card captured from fossil 2.28 on the wire. Canonical's rule that an already hashed 40-hex password is used as-is is now honoured too.
Regression coverage stands up a canonical fossil server with anonymous capabilities removed, so a login card is the only way in, and asserts that a credentialed clone transfers content, a wrong password is refused, and a local commit pushed under authentication lands in the server's blob table and on its timeline. Nothing in the suite authenticated against real fossil before; clone was only ever exercised against servers granting anonymous read, where the login card is never sent, which is how this shipped.
Four xfer codec expectations that pinned the inverted argument order are corrected to the canonical bytes.
* fix(sync): correct the nonce doc to match what the server recomputes
The comment described the nonce as covering the encoded non-login cards and the signature as using the stored password hash. Both readings are wrong in ways that matter to anyone changing this code: the nonce is defined over the bytes that actually follow the login card on the wire, because the server recomputes it from what it received (src/xfer.c check_tail_hash), and the signature is salted with the project code rather than derived from user.pw directly. Hashing bytes the message does not carry produces a nonce the peer cannot reproduce, which is exactly the password-independent rejection this fix was chasing. (user: [email protected], size: 3520) [annotate] [blame] [check-ins using]
package sync import ( "bytes" "crypto/sha1" "encoding/hex" "strings" "github.com/danmestas/go-libfossil/internal/xfer" "github.com/danmestas/go-libfossil/simio" ) // computeLogin produces a LoginCard for the given credentials. // // payload must be exactly the bytes that will follow the login card on the // wire: the nonce is "SHA-1 over every decompressed body byte after the body // login line's LF through body end" (§6.2), and the server recomputes it from // the bytes it received (src/xfer.c check_tail_hash). Hashing anything the // message does not carry yields a nonce the server cannot reproduce, and it // rejects the login before the password is compared at all — which is why // #203 failed identically for right and wrong passwords. // // SIGNATURE = SHA1(NONCE-hex || shared-secret-hex) // // — §6.4: the two 40-character hex strings concatenated, not their raw digests. func computeLogin(user, password, projectCode string, payload []byte) *xfer.LoginCard { if user == "" { panic("sync.computeLogin: user must not be empty") } if payload == nil { panic("sync.computeLogin: payload must not be nil") } nonce := sha1Hex(payload) signature := sha1Hex([]byte(nonce + sharedSecret(password, user, projectCode))) return &xfer.LoginCard{User: user, Nonce: nonce, Signature: signature} } // sharedSecret derives the value a login signature is computed over, which is // also what the server stores in user.pw. // // shared-secret = SHA1(project-code "/" login-name "/" plaintext-password) // // — §6.3, 40 lowercase hex. The project code salts it, so the same password // yields a different secret per repository; deriving it from the wrong code is // indistinguishable from a wrong password (#203). // // Two branches canonical's C has are deliberately absent, because §6.3 states // neither is reachable on the wire: the cleartext-password fallback for an // unknown project code (src/sha1.c) cannot arise because "the initial clone // sends no login card", and http_build_login_card's rule that an already // 40-hex password is passed through unhashed serves fossil's own cached // last-sync-pw, which this library does not keep. Both are silent behavioural // forks on the authentication path, so the spec's narrower contract is the one // implemented here; a caller with no project code is a caller error, not a // wire case. func sharedSecret(password, login, projectCode string) string { if projectCode == "" { panic("sync.sharedSecret: projectCode must not be empty") } return sha1Hex([]byte(projectCode + "/" + login + "/" + password)) } // randomComment returns the nonce comment, sent as the last card before a // login card is prepended. "Random comments vary independently generated // nonces" (§6.4); without it two identical requests would share a nonce and // so a signature. // // Its grammar is nonce-comment = "#" SP 40HEXDIG-UC LF (§B.3) — 20 random // bytes as uppercase hex. func randomComment(rng simio.Rand) *xfer.CommentCard { rb := make([]byte, 20) rng.Read(rb) return &xfer.CommentCard{Text: strings.ToUpper(hex.EncodeToString(rb))} } // encodeCards returns the wire bytes of cards, which is what a login card's // nonce is computed over. func encodeCards(cards []xfer.Card) ([]byte, error) { var buf bytes.Buffer for _, c := range cards { if err := xfer.EncodeCard(&buf, c); err != nil { return nil, err } } return buf.Bytes(), nil } func sha1Hex(data []byte) string { h := sha1.Sum(data) return hex.EncodeToString(h[:]) }