diff --git a/pkg/enpass/crypto.go b/pkg/enpass/crypto.go index 0787d2c..11e8da4 100644 --- a/pkg/enpass/crypto.go +++ b/pkg/enpass/crypto.go @@ -10,45 +10,75 @@ import ( "github.com/pkg/errors" ) +// NewItemKey returns a fresh 44-byte item key: a 32-byte AES-256 key followed +// by a 12-byte GCM nonce. Enpass stores this blob in item.key and uses it for +// every sensitive field of that item. +func NewItemKey() ([]byte, error) { + itemKey := make([]byte, 44) + if _, err := rand.Read(itemKey); err != nil { + return nil, errors.Wrap(err, "could not generate random item key") + } + + return itemKey, nil +} + // EncryptValue encrypts a plaintext value using AES-256-GCM. // Returns the hex-encoded ciphertext and the 44-byte key (32-byte AES key + 12-byte nonce). // The uuid (without dashes) is used as Additional Authenticated Data (AAD). func EncryptValue(plaintext string, uuid string) (encryptedValue string, itemKey []byte, err error) { - // Generate random 32-byte AES key - key := make([]byte, 32) - if _, err := rand.Read(key); err != nil { - return "", nil, errors.Wrap(err, "could not generate random key") + itemKey, err = NewItemKey() + if err != nil { + return "", nil, err + } + + encryptedValue, err = EncryptValueWithKey(plaintext, uuid, itemKey) + if err != nil { + return "", nil, err } - // Generate random 12-byte nonce for GCM - nonce := make([]byte, 12) - if _, err := rand.Read(nonce); err != nil { - return "", nil, errors.Wrap(err, "could not generate random nonce") + return encryptedValue, itemKey, nil +} + +// EncryptValueWithKey encrypts plaintext with an existing item key. +// +// Enpass keeps one key per item, not one per field: every sensitive row of an +// item is encrypted with the same 32-byte key and 12-byte nonce, which is why +// two fields holding the same value have byte-identical ciphertext. Reusing the +// existing key on update therefore keeps every other sensitive field of the +// item decryptable; generating a new key and storing only the updated field +// would leave the remaining fields encrypted under the old, discarded key. +// +// itemKey must be exactly 44 bytes. The uuid (without dashes) is used as AAD, +// so the ciphertext is bound to the item. +// +// Example: EncryptValueWithKey("hunter2", uuid, existingKey) +func EncryptValueWithKey(plaintext string, uuid string, itemKey []byte) (encryptedValue string, err error) { + if len(itemKey) != 44 { + return "", errors.Errorf("item key must be 44 bytes (32 key + 12 nonce), got %d", len(itemKey)) } + key := itemKey[:32] + nonce := itemKey[32:] + // Create cipher block, err := aes.NewCipher(key) if err != nil { - return "", nil, errors.Wrap(err, "could not create cipher") + return "", errors.Wrap(err, "could not create cipher") } aesgcm, err := cipher.NewGCM(block) if err != nil { - return "", nil, errors.Wrap(err, "could not create GCM") + return "", errors.Wrap(err, "could not create GCM") } // AAD is the UUID without dashes aad, err := hex.DecodeString(strings.ReplaceAll(uuid, "-", "")) if err != nil { - return "", nil, errors.Wrap(err, "could not decode UUID for AAD") + return "", errors.Wrap(err, "could not decode UUID for AAD") } // Encrypt (output includes authentication tag) ciphertextAndTag := aesgcm.Seal(nil, nonce, []byte(plaintext), aad) - // Return hex-encoded ciphertext and key+nonce - encryptedValue = hex.EncodeToString(ciphertextAndTag) - itemKey = append(key, nonce...) - - return encryptedValue, itemKey, nil + return hex.EncodeToString(ciphertextAndTag), nil } diff --git a/pkg/enpass/template.go b/pkg/enpass/template.go new file mode 100644 index 0000000..efd2abe --- /dev/null +++ b/pkg/enpass/template.go @@ -0,0 +1,148 @@ +package enpass + +import ( + "crypto/sha1" + "encoding/hex" + "encoding/json" + "net/url" + "strings" +) + +const ( + // loginCategory is the value Enpass stores in item.category for login items. + // The app writes it in lower case; the previous writer used "Login", which the + // app does not recognise as a built-in category. + loginCategory = "login" + + // loginTemplate is the value Enpass stores in item.template for login items. + // It must not be NULL: the macOS app passes this column to strlen() while + // rendering and crashes on a NULL template. + loginTemplate = "login.default" +) + +// templateField describes one field slot of the stock login.default template. +// +// The desktop app does not store template definitions in the vault: the +// template/category tables are empty for stock vaults and the definitions ship +// inside the app bundle (templates.json). The app resolves a field's label and +// section grouping by looking up (item.template, itemfield.item_field_uid), so +// a field written with an invented uid has no label to render. +// +// These uids and their order were taken from items written by Enpass 6.12 on +// macOS; they are stable across the 6.x releases we inspected. Fields with a +// high uid (5xxx/6xxx/9xxx) are user-created custom fields: those carry their +// label in itemfield.label and are not part of this list. +// +// A note on the hard-coded layout: the stock template is deliberately kept as a +// constant here rather than looked up, because stock vaults ship an empty +// template/category table — the definitions live in the app bundle, so there is +// nothing in the vault to read at write time. The more future-proof approach is +// to copy the skeleton from an existing item of the same template in the same +// vault (its template-provided slots are exactly the fields with an empty +// label), falling back to this constant only when the vault has no such item. +// That needs a fallback path and a policy for custom fields, so it is left as a +// follow-up rather than folded into this fix. +type templateField struct { + // uid is Enpass's item_field_uid for this slot. + uid int + // fieldType is the itemfield.type value ("username", "password", ...). + fieldType string + // orde is the display order inside the item, starting at 1. + orde int + // sensitive marks fields whose value is AES-GCM encrypted with item.key. + sensitive bool +} + +// loginFields is the stock login.default layout, in display order. +var loginFields = []templateField{ + {uid: 10, fieldType: "username", orde: 1}, + {uid: 12, fieldType: "email", orde: 2}, + {uid: 11, fieldType: "password", orde: 3, sensitive: true}, + {uid: 13, fieldType: "url", orde: 4}, + {uid: 101, fieldType: "section", orde: 5}, + {uid: 14, fieldType: "phone", orde: 6}, + {uid: 102, fieldType: "totp", orde: 7}, + {uid: 15, fieldType: "text", orde: 8}, + {uid: 16, fieldType: "text", orde: 9, sensitive: true}, +} + +// loginFieldByType returns the first stock slot of the given type. +// +// Username, password and url have exactly one slot each, so "first" is +// unambiguous. It returns false for types the login template does not have. +// +// Example: loginFieldByType("password") == templateField{uid: 11, orde: 3, ...}, true +// +// loginFieldByType("nope") == templateField{}, false +func loginFieldByType(fieldType string) (templateField, bool) { + for _, field := range loginFields { + if field.fieldType == fieldType { + return field, true + } + } + + return templateField{}, false +} + +// loginIcon builds the JSON blob Enpass expects in item.icon for a login item. +// +// The app stores a JSON object, not a plain icon name. A plain string such as +// "card_password" (what the previous writer stored) makes the item render with +// a blank/default icon. +// +// The favicon domain is derived from the item URL so the app can show the site's +// icon; when the URL is empty or unparseable the favicon domain is left empty +// and the generic login icon is used. +// +// Example: loginIcon("https://www.example.com/login") +// +// == `{"fav":"example.com","image":{"file":"misc/login"},"type":1,"uuid":""}` +func loginIcon(rawURL string) string { + favicon := "" + if parsed, err := url.Parse(rawURL); err == nil { + favicon = strings.TrimPrefix(parsed.Hostname(), "www.") + } + + icon, err := json.Marshal(map[string]interface{}{ + "fav": favicon, + "image": map[string]string{"file": "misc/login"}, + "type": 1, + "uuid": "", + }) + if err != nil { + // json.Marshal of a map of strings and ints cannot fail; return the + // generic icon rather than panicking if that ever changes. + return `{"fav":"","image":{"file":"misc/login"},"type":1,"uuid":""}` + } + + return string(icon) +} + +// valueHash returns the SHA-1 hex digest Enpass stores in itemfield.hash. +// +// The app keeps a digest of the plaintext value (passwords included) and uses +// it to detect duplicate/identical passwords. It is empty for empty values. +// +// Example: valueHash("noIdeaata11") == "a3f1...". +func valueHash(value string) string { + if value == "" { + return "" + } + + sum := sha1.Sum([]byte(value)) + + return hex.EncodeToString(sum[:]) +} + +// valueInitial returns the two leading characters Enpass displays for a +// password field in the item list. It is only used for passwords. +// +// Example: valueInitial("agentpass") == "ag"; valueInitial("") == "". +func valueInitial(value string) string { + runes := []rune(value) + if len(runes) > 2 { + return string(runes[:2]) + } + + return string(runes) +} diff --git a/pkg/enpass/vaultinfo.go b/pkg/enpass/vaultinfo.go index 9f6c3c6..ec31763 100644 --- a/pkg/enpass/vaultinfo.go +++ b/pkg/enpass/vaultinfo.go @@ -3,6 +3,9 @@ package enpass import ( "encoding/json" "os" + "path/filepath" + "strconv" + "time" "github.com/pkg/errors" ) @@ -36,3 +39,97 @@ func (v *Vault) loadVaultInfo() (VaultInfo, error) { return vaultInfo, nil } + +// syncVaultInfo keeps vault.json in step with the database after a write. +// +// The desktop app uses vault_items_count and last_modified_time for its own +// bookkeeping and for sync conflict detection. Leaving them stale made an entry +// created by this tool invisible to that accounting. Device and user fields +// describe whoever last wrote the vault and are left untouched. +// +// Failures are logged rather than returned: the database write is already +// committed by the time this runs, so returning an error would suggest the +// entry was not created while it actually exists. +func (v *Vault) syncVaultInfo() { + var count int + if err := v.db.QueryRow("SELECT count(*) FROM item WHERE deleted = 0").Scan(&count); err != nil { + v.logger.WithError(err).Warn("could not count vault items") + return + } + + now := time.Now().Unix() + err := v.patchVaultInfo(map[string]json.RawMessage{ + "vault_items_count": json.RawMessage(strconv.Itoa(count)), + "last_modified_time": json.RawMessage(strconv.FormatInt(now, 10)), + }) + if err != nil { + v.logger.WithError(err).Warn("could not update vault info") + return + } + + v.vaultInfo.VaultNumItems = count +} + +// patchVaultInfo replaces the given keys in vault.json, leaving every other +// key byte-identical. +// +// vault.json is shared with the desktop app and the sync engine, so it carries +// fields this tool does not interpret (device names, sync watermarks, the +// encryption parameters). It is therefore decoded as raw JSON, patched, and +// written back atomically: a temporary file in the same directory followed by +// a rename, so a crash can never leave the vault described by a truncated file. +// +// Example: patchVaultInfo(map[string]json.RawMessage{"vault_items_count": json.RawMessage("2")}) +func (v *Vault) patchVaultInfo(patch map[string]json.RawMessage) error { + infoBytes, err := os.ReadFile(v.vaultInfoFilename) + if err != nil { + return errors.Wrap(err, "could not read vault info") + } + + info := make(map[string]json.RawMessage) + if err := json.Unmarshal(infoBytes, &info); err != nil { + return errors.Wrap(err, "could not parse vault info") + } + + for key, value := range patch { + info[key] = value + } + + patched, err := json.MarshalIndent(info, "", " ") + if err != nil { + return errors.Wrap(err, "could not serialize vault info") + } + patched = append(patched, '\n') + + mode := os.FileMode(0o600) + if stat, err := os.Stat(v.vaultInfoFilename); err == nil { + mode = stat.Mode().Perm() + } + + tmp, err := os.CreateTemp(filepath.Dir(v.vaultInfoFilename), ".vault.json.*") + if err != nil { + return errors.Wrap(err, "could not create temporary vault info") + } + tmpName := tmp.Name() + + if _, err := tmp.Write(patched); err != nil { + tmp.Close() + os.Remove(tmpName) + return errors.Wrap(err, "could not write vault info") + } + if err := tmp.Chmod(mode); err != nil { + tmp.Close() + os.Remove(tmpName) + return errors.Wrap(err, "could not set vault info permissions") + } + if err := tmp.Close(); err != nil { + os.Remove(tmpName) + return errors.Wrap(err, "could not close vault info") + } + if err := os.Rename(tmpName, v.vaultInfoFilename); err != nil { + os.Remove(tmpName) + return errors.Wrap(err, "could not replace vault info") + } + + return nil +} diff --git a/pkg/enpass/write.go b/pkg/enpass/write.go index 7e34328..9242220 100644 --- a/pkg/enpass/write.go +++ b/pkg/enpass/write.go @@ -1,13 +1,20 @@ package enpass import ( + "database/sql" + "fmt" + "sort" + "strings" "time" "github.com/google/uuid" "github.com/pkg/errors" ) -// EntryData holds the data for creating or updating an entry +// EntryData holds the data for creating or updating an entry. +// +// Only login-shaped entries are supported: the writer populates the stock +// login.default template. Empty fields mean "leave unchanged" on update. type EntryData struct { Title string Username string @@ -17,7 +24,129 @@ type EntryData struct { Category string } -// CreateEntry creates a new password entry in the vault +// sqlExecutor is the subset of *sql.DB and *sql.Tx used by the schema helpers. +type sqlExecutor interface { + Query(query string, args ...interface{}) (*sql.Rows, error) + QueryRow(query string, args ...interface{}) *sql.Row + Exec(query string, args ...interface{}) (sql.Result, error) +} + +// tableColumns returns the set of columns that exist in table. +// +// Column sets differ between vaults: a vault created by Enpass 6.8 has fewer +// itemfield columns than one written by 6.12 (e.g. non_compliant and +// non_compliant_check_time). The writer therefore fills in only the columns the +// vault in front of it actually has, instead of failing on a version mismatch. +// +// table is always a compile-time constant from this package, so interpolating +// it into the query is safe; values are still passed as bound parameters. +// +// Example: tableColumns(tx, "item") == map[string]bool{"uuid": true, ...} +func tableColumns(q sqlExecutor, table string) (map[string]bool, error) { + rows, err := q.Query("SELECT name FROM pragma_table_info('" + table + "')") + if err != nil { + return nil, errors.Wrapf(err, "could not read schema of %s", table) + } + defer rows.Close() + + columns := make(map[string]bool) + for rows.Next() { + var name string + if err := rows.Scan(&name); err != nil { + return nil, errors.Wrapf(err, "could not read column of %s", table) + } + columns[name] = true + } + + return columns, rows.Err() +} + +// insertRow inserts values into table, silently skipping keys the table does +// not have. +// +// Values must be a map of column name to value; unknown columns are dropped so +// one code path works against every vault schema version. Column order is +// sorted to keep the generated statement deterministic. +// +// Example: insertRow(tx, "itemfield", fieldCols, map[string]interface{}{"type": "username"}) +func insertRow(q sqlExecutor, table string, available map[string]bool, values map[string]interface{}) error { + names := sortedColumns(values, available) + if len(names) == 0 { + return errors.Errorf("no known columns to insert into %s", table) + } + + placeholders := make([]string, len(names)) + args := make([]interface{}, len(names)) + for i, name := range names { + placeholders[i] = "?" + args[i] = values[name] + } + + query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", + table, strings.Join(names, ", "), strings.Join(placeholders, ", ")) + + _, err := q.Exec(query, args...) + + return err +} + +// updateRow updates values in table where where, skipping unknown columns. +// +// Example: updateRow(tx, "item", itemCols, map[string]interface{}{"title": "new"}, "uuid = ?", id) +func updateRow(q sqlExecutor, table string, available map[string]bool, values map[string]interface{}, where string, whereArgs ...interface{}) (int64, error) { + names := sortedColumns(values, available) + if len(names) == 0 { + return 0, nil + } + + assignments := make([]string, len(names)) + args := make([]interface{}, 0, len(names)+len(whereArgs)) + for i, name := range names { + assignments[i] = name + " = ?" + args = append(args, values[name]) + } + args = append(args, whereArgs...) + + query := fmt.Sprintf("UPDATE %s SET %s WHERE %s", table, strings.Join(assignments, ", "), where) + result, err := q.Exec(query, args...) + if err != nil { + return 0, err + } + + return result.RowsAffected() +} + +// sortedColumns returns the keys of values that exist in available, sorted. +func sortedColumns(values map[string]interface{}, available map[string]bool) []string { + names := make([]string, 0, len(values)) + for name := range values { + if available[name] { + names = append(names, name) + } + } + sort.Strings(names) + + return names +} + +// CreateEntry creates a new login entry in the vault. +// +// The row is written in the shape the desktop app expects: a non-NULL +// item.template, a lower-case category, a JSON icon, the full stock +// login.default field set with Enpass's own item_field_uid/orde values, and the +// per-field hashes the app uses for its password-strength and duplicate +// detection. Writing a partial row is what made Enpass 6.12 crash with a NULL +// dereference while rendering, and left earlier versions showing unlabelled +// values. +// +// Only the login category is supported, because EntryData only carries login +// fields; passing any other category returns an error instead of writing a row +// the app cannot map onto a template. +// +// v.syncVaultInfo is called after the row is committed so vault.json keeps its +// item count and modification time in step with the database. +// +// Example: id, err := vault.CreateEntry(&EntryData{Title: "GitHub", Username: "me", Password: "s3cret"}) func (v *Vault) CreateEntry(entry *EntryData) (string, error) { if v.db == nil { return "", errors.New("vault is not initialized") @@ -27,79 +156,90 @@ func (v *Vault) CreateEntry(entry *EntryData) (string, error) { return "", errors.New("title is required") } - // Generate UUID - entryUUID := uuid.New().String() - now := time.Now().Unix() - - // Set defaults - category := entry.Category + category := strings.ToLower(strings.TrimSpace(entry.Category)) if category == "" { - category = "Login" + category = loginCategory + } + if category != loginCategory { + return "", errors.Errorf("unsupported category %q: only %q entries can be created", entry.Category, loginCategory) } - // Start transaction - tx, err := v.db.Begin() + entryUUID := uuid.New().String() + now := time.Now().Unix() + + // One key per item, shared by every sensitive field. + itemKey, err := NewItemKey() if err != nil { - return "", errors.Wrap(err, "could not begin transaction") + return "", err } - defer tx.Rollback() - // Encrypt password first to get the key - var encryptedValue string - var itemKey []byte + encryptedPassword := "" if entry.Password != "" { - var err error - encryptedValue, itemKey, err = EncryptValue(entry.Password, entryUUID) + encryptedPassword, err = EncryptValueWithKey(entry.Password, entryUUID, itemKey) if err != nil { return "", errors.Wrap(err, "could not encrypt password") } } - // Insert into item table (key is stored here, not in itemfield) - _, err = tx.Exec(` - INSERT INTO item ( - uuid, created_at, field_updated_at, title, subtitle, - note, trashed, deleted, category, icon, last_used, key - ) VALUES (?, ?, ?, ?, ?, ?, 0, 0, ?, ?, ?, ?) - `, entryUUID, now, now, entry.Title, entry.Username, - entry.Notes, category, "card_password", now, itemKey) + tx, err := v.db.Begin() if err != nil { - return "", errors.Wrap(err, "could not insert item") + return "", errors.Wrap(err, "could not begin transaction") } + defer tx.Rollback() - // Insert password field (value only, key is in item table) - if entry.Password != "" { - _, err = tx.Exec(` - INSERT INTO itemfield ( - item_uuid, label, value, deleted, sensitive, type - ) VALUES (?, ?, ?, 0, 1, ?) - `, entryUUID, "", encryptedValue, "password") - if err != nil { - return "", errors.Wrap(err, "could not insert password field") - } + itemColumns, err := tableColumns(tx, "item") + if err != nil { + return "", err + } + fieldColumns, err := tableColumns(tx, "itemfield") + if err != nil { + return "", err } - // Insert username field (not encrypted) - if entry.Username != "" { - _, err = tx.Exec(` - INSERT INTO itemfield ( - item_uuid, label, value, deleted, sensitive, type - ) VALUES (?, ?, ?, 0, 0, ?) - `, entryUUID, "", entry.Username, "username") - if err != nil { - return "", errors.Wrap(err, "could not insert username field") - } + err = insertRow(tx, "item", itemColumns, map[string]interface{}{ + "uuid": entryUUID, + "created_at": now, + "meta_updated_at": now, + "field_updated_at": now, + "updated_at": now, + "title": entry.Title, + "subtitle": entry.Username, + "note": entry.Notes, + "icon": loginIcon(entry.URL), + "category": category, + "template": loginTemplate, + "auto_submit": 1, + "favorite": 0, + "trashed": 0, + "archived": 0, + "deleted": 0, + "wearable": 0, + "usage_count": 0, + "last_used": 0, + "form_data": "", + "extra": "", + "key": itemKey, + }) + if err != nil { + return "", errors.Wrap(err, "could not insert item") } - // Insert URL field (not encrypted) - if entry.URL != "" { - _, err = tx.Exec(` - INSERT INTO itemfield ( - item_uuid, label, value, deleted, sensitive, type - ) VALUES (?, ?, ?, 0, 0, ?) - `, entryUUID, "", entry.URL, "url") - if err != nil { - return "", errors.Wrap(err, "could not insert URL field") + // Write the complete template, including the empty slots: the app maps + // values onto template slots by uid, so a missing section or a missing + // empty field shows up as an unlabelled, ungrouped entry. + for _, field := range loginFields { + storedValue, plainValue := "", "" + switch field.fieldType { + case "username": + storedValue, plainValue = entry.Username, entry.Username + case "password": + storedValue, plainValue = encryptedPassword, entry.Password + case "url": + storedValue, plainValue = entry.URL, entry.URL + } + + if err := insertTemplateField(tx, fieldColumns, entryUUID, field, storedValue, plainValue, now); err != nil { + return "", errors.Wrapf(err, "could not insert %s field", field.fieldType) } } @@ -107,11 +247,27 @@ func (v *Vault) CreateEntry(entry *EntryData) (string, error) { return "", errors.Wrap(err, "could not commit transaction") } + v.syncVaultInfo() + v.logger.WithField("uuid", entryUUID).Debug("created entry") + return entryUUID, nil } -// UpdateEntry updates an existing entry in the vault +// UpdateEntry updates an existing entry. +// +// Empty fields in updates are left untouched, matching the CLI's flag +// semantics. Sensitive updates reuse the item's existing key: Enpass encrypts +// every sensitive field of an item with that one key, so replacing it would +// leave the other sensitive fields (extra password slots, TOTP seeds) encrypted +// under a key the app no longer has. +// +// The password field is located by its template uid first and by type second, +// so entries written by older versions of this tool are repaired in place +// instead of gaining a duplicate field. A missing field is added using the +// stock template slot. +// +// Example: err := vault.UpdateEntry(id, &EntryData{Password: "new-secret"}) func (v *Vault) UpdateEntry(entryUUID string, updates *EntryData) error { if v.db == nil { return errors.New("vault is not initialized") @@ -119,125 +275,231 @@ func (v *Vault) UpdateEntry(entryUUID string, updates *EntryData) error { now := time.Now().Unix() - // Start transaction tx, err := v.db.Begin() if err != nil { return errors.Wrap(err, "could not begin transaction") } defer tx.Rollback() - // Update item table if title, notes, or category changed - if updates.Title != "" || updates.Notes != "" || updates.Category != "" { - query := "UPDATE item SET field_updated_at = ?" - args := []interface{}{now} + var itemKey []byte + var currentCategory, currentTemplate sql.NullString + err = tx.QueryRow("SELECT key, category, template FROM item WHERE uuid = ?", entryUUID). + Scan(&itemKey, ¤tCategory, ¤tTemplate) + if errors.Is(err, sql.ErrNoRows) { + return errors.New("entry not found") + } else if err != nil { + return errors.Wrap(err, "could not read entry") + } - if updates.Title != "" { - query += ", title = ?" - args = append(args, updates.Title) - } - if updates.Notes != "" { - query += ", note = ?" - args = append(args, updates.Notes) - } - if updates.Category != "" { - query += ", category = ?" - args = append(args, updates.Category) - } + itemColumns, err := tableColumns(tx, "item") + if err != nil { + return err + } + fieldColumns, err := tableColumns(tx, "itemfield") + if err != nil { + return err + } - query += " WHERE uuid = ?" - args = append(args, entryUUID) + itemValues := map[string]interface{}{ + "field_updated_at": now, + "meta_updated_at": now, + "updated_at": now, + } - _, err = tx.Exec(query, args...) - if err != nil { - return errors.Wrap(err, "could not update item") + if updates.Title != "" { + itemValues["title"] = updates.Title + } + if updates.Notes != "" { + itemValues["note"] = updates.Notes + } + if updates.Category != "" { + category := strings.ToLower(strings.TrimSpace(updates.Category)) + itemValues["category"] = category + // Repair rows written before the template was set, so a later edit in + // the app can resolve their labels. + if category == loginCategory && currentTemplate.String == "" { + itemValues["template"] = loginTemplate } } - // Update username in item.subtitle and itemfield if updates.Username != "" { - _, err = tx.Exec("UPDATE item SET subtitle = ?, field_updated_at = ? WHERE uuid = ?", - updates.Username, now, entryUUID) - if err != nil { - return errors.Wrap(err, "could not update subtitle") - } + itemValues["subtitle"] = updates.Username + } - // Update or insert username field - result, err := tx.Exec("UPDATE itemfield SET value = ? WHERE item_uuid = ? AND type = ?", - updates.Username, entryUUID, "username") - if err != nil { - return errors.Wrap(err, "could not update username field") - } - rowsAffected, _ := result.RowsAffected() - if rowsAffected == 0 { - _, err = tx.Exec(` - INSERT INTO itemfield (item_uuid, label, value, deleted, sensitive, type) - VALUES (?, ?, ?, 0, 0, ?) - `, entryUUID, "", updates.Username, "username") + if updates.Password != "" { + // Keep the existing key when there is one; only allocate a key for + // items that never had a password (or were written without one). + if len(itemKey) != 44 { + itemKey, err = NewItemKey() if err != nil { - return errors.Wrap(err, "could not insert username field") + return err } + itemValues["key"] = itemKey } - } - // Update password (encrypted) - key is stored in item table - if updates.Password != "" { - encryptedValue, itemKey, err := EncryptValue(updates.Password, entryUUID) - if err != nil { - return errors.Wrap(err, "could not encrypt password") + passwordField, ok := loginFieldByType("password") + if !ok { + return errors.New("login template has no password field") } - // Update key in item table - _, err = tx.Exec("UPDATE item SET key = ?, field_updated_at = ? WHERE uuid = ?", - itemKey, now, entryUUID) + encryptedPassword, err := EncryptValueWithKey(updates.Password, entryUUID, itemKey) if err != nil { - return errors.Wrap(err, "could not update item key") + return errors.Wrap(err, "could not encrypt password") } - // Update password value in itemfield - result, err := tx.Exec("UPDATE itemfield SET value = ? WHERE item_uuid = ? AND type = ?", - encryptedValue, entryUUID, "password") - if err != nil { + if err := applyField(tx, fieldColumns, entryUUID, passwordField, encryptedPassword, updates.Password, now); err != nil { return errors.Wrap(err, "could not update password field") } - rowsAffected, _ := result.RowsAffected() - if rowsAffected == 0 { - _, err = tx.Exec(` - INSERT INTO itemfield (item_uuid, label, value, deleted, sensitive, type) - VALUES (?, ?, ?, 0, 1, ?) - `, entryUUID, "", encryptedValue, "password") - if err != nil { - return errors.Wrap(err, "could not insert password field") - } + } + + if updates.Username != "" { + usernameField, _ := loginFieldByType("username") + if err := applyField(tx, fieldColumns, entryUUID, usernameField, updates.Username, updates.Username, now); err != nil { + return errors.Wrap(err, "could not update username field") } } - // Update URL if updates.URL != "" { - result, err := tx.Exec("UPDATE itemfield SET value = ? WHERE item_uuid = ? AND type = ?", - updates.URL, entryUUID, "url") - if err != nil { + urlField, _ := loginFieldByType("url") + if err := applyField(tx, fieldColumns, entryUUID, urlField, updates.URL, updates.URL, now); err != nil { return errors.Wrap(err, "could not update URL field") } - rowsAffected, _ := result.RowsAffected() - if rowsAffected == 0 { - _, err = tx.Exec(` - INSERT INTO itemfield (item_uuid, label, value, deleted, sensitive, type) - VALUES (?, ?, ?, 0, 0, ?) - `, entryUUID, "", updates.URL, "url") - if err != nil { - return errors.Wrap(err, "could not insert URL field") - } - } + } + + if _, err := updateRow(tx, "item", itemColumns, itemValues, "uuid = ?", entryUUID); err != nil { + return errors.Wrap(err, "could not update item") } if err := tx.Commit(); err != nil { return errors.Wrap(err, "could not commit transaction") } + v.syncVaultInfo() + v.logger.WithField("uuid", entryUUID).Debug("updated entry") + return nil } +// applyField updates the field matching field's type, or inserts the template +// slot when the entry does not have it yet. +func applyField(q sqlExecutor, fieldColumns map[string]bool, itemUUID string, field templateField, storedValue, plainValue string, now int64) error { + existing, found, err := findField(q, itemUUID, field) + if err != nil { + return err + } + + if !found { + return insertTemplateField(q, fieldColumns, itemUUID, field, storedValue, plainValue, now) + } + + return updateField(q, fieldColumns, existing, field, storedValue, plainValue, now) +} + +// fieldRow is the identifying part of an existing itemfield row. +type fieldRow struct { + id int64 + uid int +} + +// findField locates the field row that should hold field, preferring the +// template uid and falling back to the first field of that type. +// +// The fallback is what lets an entry written by an older version of this tool +// (arbitrary uids, no orde) be updated instead of duplicated: the app treats a +// second password field as an extra slot, not as the same value. +// +// Example: findField(tx, id, passwordField) == {id: 42, uid: 11}, true, nil +func findField(q sqlExecutor, itemUUID string, field templateField) (fieldRow, bool, error) { + var row fieldRow + err := q.QueryRow(` + SELECT ID, item_field_uid FROM itemfield + WHERE item_uuid = ? AND type = ? AND deleted = 0 + ORDER BY (item_field_uid = ?) DESC, orde + LIMIT 1 + `, itemUUID, field.fieldType, field.uid).Scan(&row.id, &row.uid) + + if errors.Is(err, sql.ErrNoRows) { + return fieldRow{}, false, nil + } else if err != nil { + return fieldRow{}, false, errors.Wrap(err, "could not find field") + } + + return row, true, nil +} + +// insertTemplateField writes one field of the login template. +// +// storedValue is what goes into itemfield.value (ciphertext for sensitive +// fields), plainValue is the readable value used to compute hash and the +// password initials. The app expects a hash for every non-empty field, starts +// password strength at 0 and every other field at -1, and marks template slots +// as historical with algo_version 1. +func insertTemplateField(q sqlExecutor, fieldColumns map[string]bool, itemUUID string, field templateField, storedValue, plainValue string, now int64) error { + initial := "" + strength := -1 + if field.fieldType == "password" { + initial = valueInitial(plainValue) + strength = 0 + } + + return insertRow(q, "itemfield", fieldColumns, map[string]interface{}{ + "item_uuid": itemUUID, + "item_field_uid": field.uid, + "label": "", + "value": storedValue, + "deleted": 0, + "sensitive": boolToInt(field.sensitive), + "historical": 1, + "type": field.fieldType, + "form_id": "", + "updated_at": now, + "value_updated_at": now, + "orde": field.orde, + "wearable": 0, + "history": "", + "initial": initial, + "hash": valueHash(plainValue), + "strength": strength, + "algo_version": 1, + "expiry": 0, + "excluded": 0, + "pwned_check_time": now, + "extra": "", + "non_compliant": 0, + "non_compliant_check_time": 0, + }) +} + +// updateField rewrites one existing field in place, keeping its uid and order. +func updateField(q sqlExecutor, fieldColumns map[string]bool, existing fieldRow, field templateField, storedValue, plainValue string, now int64) error { + values := map[string]interface{}{ + "value": storedValue, + "hash": valueHash(plainValue), + "updated_at": now, + "value_updated_at": now, + } + + // The app only shows initials and a strength score for passwords. + if field.fieldType == "password" { + values["initial"] = valueInitial(plainValue) + values["strength"] = 0 + } + + _, err := updateRow(q, "itemfield", fieldColumns, values, "ID = ?", existing.id) + + return err +} + +// boolToInt converts a bool into the 0/1 SQLite uses for its boolean columns. +func boolToInt(value bool) int { + if value { + return 1 + } + + return 0 +} + // TrashEntry moves an entry to the trash func (v *Vault) TrashEntry(entryUUID string) error { if v.db == nil { @@ -245,7 +507,8 @@ func (v *Vault) TrashEntry(entryUUID string) error { } now := time.Now().Unix() - result, err := v.db.Exec("UPDATE item SET trashed = 1, field_updated_at = ? WHERE uuid = ?", now, entryUUID) + result, err := v.db.Exec("UPDATE item SET trashed = 1, field_updated_at = ?, meta_updated_at = ?, updated_at = ? WHERE uuid = ?", + now, now, now, entryUUID) if err != nil { return errors.Wrap(err, "could not trash entry") } @@ -266,7 +529,8 @@ func (v *Vault) RestoreEntry(entryUUID string) error { } now := time.Now().Unix() - result, err := v.db.Exec("UPDATE item SET trashed = 0, field_updated_at = ? WHERE uuid = ?", now, entryUUID) + result, err := v.db.Exec("UPDATE item SET trashed = 0, field_updated_at = ?, meta_updated_at = ?, updated_at = ? WHERE uuid = ?", + now, now, now, entryUUID) if err != nil { return errors.Wrap(err, "could not restore entry") } @@ -314,6 +578,8 @@ func (v *Vault) DeleteEntry(entryUUID string) error { return errors.Wrap(err, "could not commit transaction") } + v.syncVaultInfo() + v.logger.WithField("uuid", entryUUID).Debug("deleted entry") return nil } diff --git a/pkg/enpass/write_test.go b/pkg/enpass/write_test.go index bec836a..ef8c0d4 100644 --- a/pkg/enpass/write_test.go +++ b/pkg/enpass/write_test.go @@ -8,6 +8,13 @@ import ( "github.com/sirupsen/logrus" ) +// NOTE: these tests skip when the vault cannot be opened and only assert that +// an entry round-trips through this package's own reader. They do not check the +// row shape the desktop app needs (item.template, item_field_uid, orde, the +// vault.json counters), which is why a writer that made Enpass 6.12 crash could +// still pass. Tightening them into structural assertions is a deliberate +// follow-up so this change stays focused on the write path. + func copyTestVault(t *testing.T) string { t.Helper() tmpDir, err := os.MkdirTemp("", "enpass-test-*")