Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correct nested groups for keepassxc builtin mode #4189

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/cmd/keepassxctemplatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ func keepassxcBuiltinBuildCache(
entry,
mapper,
)
if len(mapData) > 0 {
return mapData
}
}
}
return map[string]string{}
Expand Down
133 changes: 104 additions & 29 deletions internal/cmd/keepassxctemplatefuncs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ func TestKeepassxcTemplateFuncs(t *testing.T) {
attachmentName := "test / attachment name"
attachmentData := "test / attachment data"

nestedGroupName := "some/nested/group"
nestedEntryName := nestedGroupName + "/nested entry"
nestedEntryUsername := "nested / username"
nestedEntryPassword := "nested / password"
nestedAttachmentName := "nested / attachment name"
nestedAttachmentData := "nested / attachment data"

// Create a KeePassXC database.
dbCreateCmd := exec.Command(command, "db-create", "--set-password", database)
dbCreateCmd.Stdin = strings.NewReader(chezmoitest.JoinLines(
Expand All @@ -84,35 +91,36 @@ func TestKeepassxcTemplateFuncs(t *testing.T) {
dbCreateCmd.Stderr = os.Stderr
assert.NoError(t, dbCreateCmd.Run())

// Create a group in the database.
mkdirCmd := exec.Command(command, "mkdir", database, groupName)
mkdirCmd.Stdin = strings.NewReader(chezmoitest.JoinLines(
databasePassword,
))
mkdirCmd.Stdout = os.Stdout
mkdirCmd.Stderr = os.Stderr
assert.NoError(t, mkdirCmd.Run())

// Create an entry in the database.
addCmd := exec.Command(command, "add", database, entryName, "--username", entryUsername, "--password-prompt")
addCmd.Stdin = strings.NewReader(chezmoitest.JoinLines(
databasePassword,
entryPassword,
))
addCmd.Stdout = os.Stdout
addCmd.Stderr = os.Stderr
assert.NoError(t, addCmd.Run())

// Import an attachment to the entry in the database.
importFile := filepath.Join(tempDir, "import file")
assert.NoError(t, os.WriteFile(importFile, []byte(attachmentData), 0o666))
attachmentImportCmd := exec.Command(command, "attachment-import", database, entryName, attachmentName, importFile)
attachmentImportCmd.Stdin = strings.NewReader(chezmoitest.JoinLines(
databasePassword,
))
attachmentImportCmd.Stdout = os.Stdout
attachmentImportCmd.Stderr = os.Stderr
assert.NoError(t, attachmentImportCmd.Run())
setupKeepassEntry(
t,
command,
tempDir,
keepassEntry{
database,
databasePassword,
groupName,
entryName,
entryUsername,
entryPassword,
attachmentName,
attachmentData,
},
)
setupKeepassEntry(
t,
command,
tempDir,
keepassEntry{
database,
databasePassword,
nestedGroupName,
nestedEntryName,
nestedEntryUsername,
nestedEntryPassword,
nestedAttachmentName,
nestedAttachmentData,
},
)

for _, mode := range []keepassxcMode{
keepassxcModeCachePassword,
Expand All @@ -130,6 +138,14 @@ func TestKeepassxcTemplateFuncs(t *testing.T) {
assert.Equal(t, entryPassword, config.keepassxcTemplateFunc(entryName)["Password"])
assert.Equal(t, entryUsername, config.keepassxcAttributeTemplateFunc(entryName, "UserName"))
assert.Equal(t, attachmentData, config.keepassxcAttachmentTemplateFunc(entryName, attachmentName))

assert.Equal(t, nestedEntryPassword, config.keepassxcTemplateFunc(nestedEntryName)["Password"])
assert.Equal(t, nestedEntryUsername, config.keepassxcAttributeTemplateFunc(nestedEntryName, "UserName"))
assert.Equal(
t,
nestedAttachmentData,
config.keepassxcAttachmentTemplateFunc(nestedEntryName, nestedAttachmentName),
)
})

t.Run("incorrect_password", func(t *testing.T) {
Expand Down Expand Up @@ -170,3 +186,62 @@ func TestKeepassxcTemplateFuncs(t *testing.T) {
})
}
}

func setupKeepassEntry(t *testing.T, command, tempDir string, kpe keepassEntry) {
t.Helper()
// Create nested groups in the database.
groupPath := strings.Split(kpe.groupName, "/")
for i := range groupPath {
var name string
if i == 0 {
name = groupPath[i]
} else {
name = strings.Join(groupPath[0:i+1], "/")
}
mkdirCmd := exec.Command(command, "mkdir", kpe.database, name)
mkdirCmd.Stdin = strings.NewReader(chezmoitest.JoinLines(
kpe.databasePassword,
))
mkdirCmd.Stdout = os.Stdout
mkdirCmd.Stderr = os.Stderr
assert.NoError(t, mkdirCmd.Run())
}
// Create an entry in the database.
addCmd := exec.Command(command, "add", kpe.database, kpe.entryName, "--username", kpe.entryUsername, "--password-prompt")
addCmd.Stdin = strings.NewReader(chezmoitest.JoinLines(
kpe.databasePassword,
kpe.entryPassword,
))
addCmd.Stdout = os.Stdout
addCmd.Stderr = os.Stderr
assert.NoError(t, addCmd.Run())

// Import an attachment to the entry in the database.
importFile := filepath.Join(tempDir, "import file")
assert.NoError(t, os.WriteFile(importFile, []byte(kpe.attachmentData), 0o666))
attachmentImportCmd := exec.Command(
command,
"attachment-import",
kpe.database,
kpe.entryName,
kpe.attachmentName,
importFile,
)
attachmentImportCmd.Stdin = strings.NewReader(chezmoitest.JoinLines(
kpe.databasePassword,
))
attachmentImportCmd.Stdout = os.Stdout
attachmentImportCmd.Stderr = os.Stderr
assert.NoError(t, attachmentImportCmd.Run())
}

type keepassEntry struct {
database string
databasePassword string
groupName string
entryName string
entryUsername string
entryPassword string
attachmentName string
attachmentData string
}
Loading