Artifact 9d2143fd0fc8a4007b88e18cd8c987ed86a3afd9d7d5b648bcbcc6fdac392316:
- File
internal/sync/config_groups.go
— part of check-in
[3a6126c3e9]
at
2026-10-11 01:34:31
on branch trunk
— fix: harden sync and repository defaults (security) (#282)
* fix(xfer): reject non-decimal card sizes and bound payload memory
A payload card's size was parsed with strconv.Atoi, so a peer sending `file HASH -1` reached readPayload's negative-size assertion and panicked the client or server decoding the message, and a signed size such as `+5` was accepted. Sizes now go through parseSize, which follows the sync draft's `decimal = 1*DIGIT` grammar and fossil's 32-bit limit and returns a decoding error for anything else. uvfile FLAGS use the same parser, so negative FLAGS are rejected as the draft requires. The assertion stays, now unreachable from peer input.
readPayload no longer allocates the declared size up front: its buffer grows with the bytes read, so a large size over a short message fails after allocating about what was sent.
A cfile payload may inflate only to its USIZE (a margin above it for a delta) and not past its 4-byte size prefix, and all cfile payloads in one message share a MaxCFileInflateBytes (1 GiB) budget, so a few megabytes of zlib cannot inflate to tens of gigabytes.
FuzzDecode exercises the decoder over both framings.
* fix(sync): refuse malformed xrow payloads and cap HTTP reply bodies
An xrow payload or xdelete PKData from a peer was decoded into a map and used as is. JSON null gave a nil map that reached coerceJSONNumbers' assertion, and a primary-key value of the wrong type or a nested value reached normalizeValue's, panicking the client or server. Both directions now decode through decodeRowJSON: one JSON object, nothing after it, and repo.ValidateRow's rules, which require scalar values and primary-key values that PKHash can normalize. A client refuses an xrow for a table it has not registered. Column types match case-insensitively, as table creation accepts them.
Both HTTP transports read replies through xfer.ReadBody, which stops at MaxBodyBytes (MaxDecompressedBytes plus framing headroom) and returns ErrBodyTooLarge, untagged for retry, instead of reading an endless body into memory.
FuzzTableSyncPayload drives the server handler and client over arbitrary payloads.
* fix(sync): check synced-table rows and identifiers before building SQL
repo.UpsertXRow wrote a row's map keys into its INSERT as column names, and the rows of an xrow payload or xdelete PKData come from the peer, so a key such as "val, mtime) VALUES(...) --" chose the statement's columns and values, and a key could select from other tables into a synced one. This held on the server for pushed rows and tombstones, and on the client for a server's rows.
A peer's row must now use exactly the table's declared columns: repo.ValidateRow refuses any other key, which also keeps a peer from setting _owner. UpsertXRow no longer trusts its caller: it reads the table's registered schema and refuses any key it does not declare, apart from _owner on an ownership-mode table. ListXRows, LookupXRowOwner and DeleteXRowByPKHash validate the table name and the column names they interpolate, so no synced-table identifier reaches SQL text unchecked.
* fix(sync): answer reqconfig only with fossil's transferable config
The sync server answered reqconfig NAME with any row of the config table, for any requester, including one with no capabilities, so project-code, server-code and every local setting could be read by anyone who could reach the sync endpoint.
handleReqConfig now follows fossil's page_xfer and configure.c. It answers only a /GROUP name, only for a requester with Read, matches the group by prefix in fossil's table order, and sends only the names fossil transfers in that group, in fossil's encoding. The user and subscriber tables also need Admin, which counts only for a verified login, and the concealed table needs RdAddr. A refused request gets no cards and no error.
The groups, their names and their capability rules live in one place, config_groups.go. A differential test answers the same requests with fossil's own server and with HandleSync on the same repository and requires identical config cards.
* test(sync): keep fossil http from leaving a backoffice process
* fix(sync): check the login nonce against the request body
The server verified a login card's signature over its nonce but never checked that the nonce was the SHA-1 of the request body after the login line, as fossil's check_tail_hash does (draft-fossil-sync-protocol-00 §6.2). A login card captured from one request therefore authenticated any other request body.
The decoder now records, for each login card, the SHA-1 of the decompressed bytes after the card's line, and VerifyLogin refuses a card whose nonce does not match it. A login card that was never decoded has no tail and is refused.
Login handling otherwise follows fossil's page_xfer and check_login: - a refused login ends the exchange with "error login failed" instead of continuing as nobody; - a second login card is refused with "multiple login cards"; - a successful login adds the user's capabilities to nobody's instead of replacing them; - nobody and anonymous log in without a password, and the reserved developer and reader logins and accounts with an empty password cannot be logged into.
Fixes GHSA-2g5p-q5f3-477p.
* fix(repo): seed nobody with fossil's default capabilities
A new repository seeded the nobody user with "cghijknorswy", so every request without a login card could push check-ins and unversioned files, write wiki pages and tickets, and held Setup. Seed the four built-in users `fossil new` creates, with fossil's capabilities: nobody "gjorz", anonymous "hz", developer "ei" and reader "kptw".
Setup and Admin now include check-in, read and clone, as they do in fossil, so the creating user can sync once given a password. A login to an account with no stored password is refused, as fossil's check_login refuses it; the creator starts with none, and the signature would otherwise be the SHA-1 of the nonce alone.
Tests that push without credentials grant nobody push explicitly. Existing repositories keep their stored capabilities.
* test(auth): reserved-login test updates the seeded developer and reader rows
New repositories now seed developer and reader as fossil does, so the test gives the existing rows a password instead of inserting them.
* docs(changelog): group the sync hardening fixes under Security
* test(sync): reqconfig tests sign their logins over the request body
The login nonce now has to cover the request body, so the reqconfig tests build their admin requests with signedRequest instead of a bare login card. (user: [email protected], size: 10377) [annotate] [blame] [check-ins using]
package sync import ( "database/sql" "errors" "fmt" "strings" "github.com/danmestas/go-libfossil/db" "github.com/danmestas/go-libfossil/internal/auth" "github.com/danmestas/go-libfossil/internal/xfer" ) // This file owns what a reqconfig card may read: the configuration groups // fossil transfers, the names in each, and the capability each needs. It // mirrors fossil 2.28's configure.c (aGroupName, aConfig, // configure_send_group) and the reqconfig branch of xfer.c page_xfer. A // config row outside these tables — project-code, server-code, any local // setting — is never sent, whatever the requester's capabilities. // Configuration group bits, fossil's CONFIGSET_* values. const ( cfgCSS = 1 << iota // style sheet only cfgSkin // web interface appearance cfgTicket // ticket setup and the reportfmt table cfgProject // project name and repository policy cfgShun // the shun table cfgUser // the user table, password hashes included cfgAddr // the concealed table of email addresses cfgXfer // transfer hook scripts cfgAlias // walias:/ URL aliases cfgScriber // the subscriber table cfgIWiki // interwiki: prefixes cfgAll = 1<<iota - 1 ) // configGroups is fossil's aGroupName, in its order: a request matches the // first group whose name it prefixes, so the order is part of the protocol. var configGroups = []struct { name string mask int }{ {"email", cfgAddr}, {"project", cfgProject}, {"skin", cfgSkin | cfgCSS}, {"css", cfgCSS}, {"shun", cfgShun}, {"ticket", cfgTicket}, {"user", cfgUser}, {"xfer", cfgXfer}, {"alias", cfgAlias}, {"subscriber", cfgScriber}, {"interwiki", cfgIWiki}, {"all", cfgAll}, } // configScalars is the scalar part of fossil 2.28's aConfig, in its order, // which is the order the cards go out in. The "@" entries there are the // tables in configTables. th1-hooks is left out as fossil builds leave it // out by default; no listed name is a "sensitive" setting. var configScalars = []struct { name string mask int }{ {"css", cfgCSS}, {"header", cfgSkin}, {"mainmenu", cfgSkin}, {"footer", cfgSkin}, {"details", cfgSkin}, {"js", cfgSkin}, {"default-skin", cfgSkin}, {"logo-mimetype", cfgSkin}, {"logo-image", cfgSkin}, {"background-mimetype", cfgSkin}, {"background-image", cfgSkin}, {"icon-mimetype", cfgSkin}, {"icon-image", cfgSkin}, {"timeline-date-format", cfgSkin}, {"timeline-default-style", cfgSkin}, {"timeline-dwelltime", cfgSkin}, {"timeline-closetime", cfgSkin}, {"timeline-hard-newlines", cfgSkin}, {"timeline-max-comment", cfgSkin}, {"timeline-plaintext", cfgSkin}, {"timeline-truncate-at-blank", cfgSkin}, {"timeline-tslink-info", cfgSkin}, {"timeline-utc", cfgSkin}, {"adunit", cfgSkin}, {"adunit-omit-if-admin", cfgSkin}, {"adunit-omit-if-user", cfgSkin}, {"default-csp", cfgSkin}, {"sitemap-extra", cfgSkin}, {"safe-html", cfgSkin}, {"th1-uri-regexp", cfgSkin | cfgTicket | cfgXfer}, {"project-name", cfgProject}, {"short-project-name", cfgProject}, {"project-description", cfgProject}, {"index-page", cfgProject}, {"manifest", cfgProject}, {"binary-glob", cfgProject}, {"clean-glob", cfgProject}, {"ignore-glob", cfgProject}, {"keep-glob", cfgProject}, {"crlf-glob", cfgProject}, {"crnl-glob", cfgProject}, {"encoding-glob", cfgProject}, {"empty-dirs", cfgProject}, {"dotfiles", cfgProject}, {"parent-project-code", cfgProject}, {"parent-project-name", cfgProject}, {"hash-policy", cfgProject}, {"comment-format", cfgProject}, {"mimetypes", cfgProject}, {"forbid-delta-manifests", cfgProject}, {"mv-rm-files", cfgProject}, {"ticket-table", cfgTicket}, {"ticket-common", cfgTicket}, {"ticket-change", cfgTicket}, {"ticket-newpage", cfgTicket}, {"ticket-viewpage", cfgTicket}, {"ticket-editpage", cfgTicket}, {"ticket-reportlist", cfgTicket}, {"ticket-report-template", cfgTicket}, {"ticket-key-template", cfgTicket}, {"ticket-title-expr", cfgTicket}, {"ticket-closed-expr", cfgTicket}, {"user-color-map", cfgUser}, {"xfer-common-script", cfgXfer}, {"xfer-push-script", cfgXfer}, {"xfer-commit-script", cfgXfer}, {"xfer-ticket-script", cfgXfer}, } // configTable describes one table-backed group as configure_send_group // writes it: each row becomes "MTIME KEY" followed by " FIELD VALUE" pairs, // values SQL-quoted. type configTable struct { mask int card string // config card name table string // source table key string // column after mtime fields []string // columns written as FIELD VALUE pairs filter string // extra WHERE term, or "" omitNull bool // user and reportfmt skip NULL fields; the rest print NULL intMtime bool // subscriber writes mtime as an integer } // configTables is in configure_send_group's order. var configTables = []configTable{ {mask: cfgShun, card: "/shun", table: "shun", key: "uuid", fields: []string{"scom"}}, {mask: cfgUser, card: "/user", table: "user", key: "login", fields: []string{"pw", "cap", "info", "photo", "jx"}, omitNull: true}, {mask: cfgTicket, card: "/reportfmt", table: "reportfmt", key: "title", fields: []string{"owner", "cols", "sqlcode", "jx"}, omitNull: true}, {mask: cfgAddr, card: "/concealed", table: "concealed", key: "hash", fields: []string{"content"}}, {mask: cfgAlias, card: "/config", table: "config", key: "name", fields: []string{"value"}, filter: "name GLOB 'walias:/*'"}, {mask: cfgIWiki, card: "/config", table: "config", key: "name", fields: []string{"value"}, filter: "name GLOB 'interwiki:*'"}, {mask: cfgScriber, card: "/subscriber", table: "subscriber", key: "semail", fields: []string{"suname", "sdigest", "sdonotcall", "ssub", "sctime", "smip"}, filter: "sverified", intMtime: true}, } // configRequestMask returns the groups a reqconfig NAME may read for a // requester holding caps, or 0 when it reads nothing. Like fossil, it needs // Read and a "/"-prefixed group name; it matches the first group the name // prefixes; and it withholds the user and subscriber tables without Admin // and concealed addresses without RdAddr. // // fossil grants Admin to any principal holding a or s. Here it counts only // for a verified login, so password hashes and subscriber addresses never go // to an anonymous requester, whatever capabilities nobody has been given. func configRequestMask(name, caps string, authed bool) int { if !auth.CanRead(caps) || !strings.HasPrefix(name, "/") { return 0 } mask := 0 for _, g := range configGroups { if strings.HasPrefix(g.name, name[1:]) { mask = g.mask break } } admin := authed && auth.IsAdmin(caps) if !admin { mask &^= cfgUser | cfgScriber } if !admin && !auth.HasCapability(caps, 'e') { mask &^= cfgAddr } return mask } // configGroupCards returns the config cards for every group in mask, tables // first and then scalar settings, as configure_send_group orders them. Rows // without an mtime are skipped, as fossil's "mtime>=0" skips them. func configGroupCards(d *db.DB, mask int) ([]xfer.Card, error) { if d == nil { panic("sync.configGroupCards: nil db") } var cards []xfer.Card for _, t := range configTables { if t.mask&mask == 0 { continue } tc, err := configTableCards(d, t) if err != nil { return nil, err } cards = append(cards, tc...) } for _, s := range configScalars { if s.mask&mask == 0 { continue } c, err := configScalarCard(d, s.name) if err != nil { return nil, err } if c != nil { cards = append(cards, c) } } return cards, nil } // configScalarCard renders one config row as a /config card, or returns nil // when the row is absent or has no mtime. func configScalarCard(d *db.DB, name string) (xfer.Card, error) { var mtime, qname, value string err := d.QueryRow("SELECT CAST(mtime AS TEXT), quote(name), quote(value)"+ " FROM config WHERE name=? AND mtime>=0", name).Scan(&mtime, &qname, &value) if errors.Is(err, sql.ErrNoRows) { return nil, nil } if err != nil { return nil, fmt.Errorf("config %s: %w", name, err) } content := mtime + " " + qname + " value " + value return &xfer.ConfigCard{Name: "/config", Content: []byte(content)}, nil } // configTableCards renders one table-backed group. A table the repository // lacks sends nothing, and a jx column it lacks reads as NULL, as fossil // tolerates repositories created before either existed. func configTableCards(d *db.DB, t configTable) ([]xfer.Card, error) { var n int if err := d.QueryRow("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?", t.table).Scan(&n); err != nil { return nil, fmt.Errorf("config %s: %w", t.table, err) } if n == 0 { return nil, nil } var hasJX bool if err := d.QueryRow("SELECT count(*)>0 FROM pragma_table_info(?) WHERE name='jx'", t.table).Scan(&hasJX); err != nil { return nil, fmt.Errorf("config %s columns: %w", t.table, err) } mtime := "CAST(mtime AS TEXT)" if t.intMtime { mtime = "CAST(CAST(mtime AS INTEGER) AS TEXT)" } cols := []string{mtime, "quote(" + t.key + ")"} for _, f := range t.fields { if f == "jx" && !hasJX { cols = append(cols, "'NULL'") } else { cols = append(cols, "quote("+f+")") } } where := "mtime>=0" if t.filter != "" { where = t.filter + " AND " + where } rows, err := d.Query("SELECT " + strings.Join(cols, ", ") + " FROM " + t.table + " WHERE " + where) if err != nil { return nil, fmt.Errorf("config %s: %w", t.table, err) } defer rows.Close() var cards []xfer.Card vals := make([]string, len(cols)) ptrs := make([]any, len(cols)) for i := range vals { ptrs[i] = &vals[i] } for rows.Next() { if err := rows.Scan(ptrs...); err != nil { return nil, fmt.Errorf("config %s row: %w", t.table, err) } content := []byte(configRecord(t, vals)) cards = append(cards, &xfer.ConfigCard{Name: t.card, Content: content}) } return cards, rows.Err() } // configRecord joins one row as "MTIME KEY FIELD VALUE ...". func configRecord(t configTable, vals []string) string { if len(vals) != len(t.fields)+2 { panic("sync.configRecord: column count does not match fields") } var b strings.Builder b.WriteString(vals[0] + " " + vals[1]) for i, f := range t.fields { v := vals[i+2] if t.omitNull && v == "NULL" { continue } b.WriteString(" " + f + " " + v) } return b.String() }