tobischo / gokeepasslib

A library to read and write keepass 2 files written in go
MIT License
251 stars 30 forks source link

Autotype keystroke is always empty #68

Closed xxxserxxx closed 3 years ago

xxxserxxx commented 3 years ago

Entry.AutoType.Association.KeystrokeSequence appears to always be empty, regardless of what it's set to in the database.

Here's a test DB with a custom sequence set:

Testdata.kdbx.zip

Unzip the attachment first; here's code to dump out the contents (a single entry), including the KeystrokeSequence:

package main

import (
    "fmt"
    "os"
    "github.com/tobischo/gokeepasslib/v3"
)

func main() {
    kp := gokeepasslib.NewDatabase()
    kp.Credentials = gokeepasslib.NewPasswordCredentials("none")
    file, err := os.Open("Testdata.kdbx")
    if err != nil {
        panic(err)
    }
    err = gokeepasslib.NewDecoder(file).Decode(kp)
    e := kp.Content.Root.Groups[0].Entries[0]
    for _, v := range e.Values {
        if v.Key == "Title" {
            fmt.Printf("%s = %s\n", v.Key, v.Value.Content)
        }
    }
    fmt.Printf("Window: %s\n", e.AutoType.Association.Window)
    fmt.Printf("Keystroke: %s\n", e.AutoType.Association.KeystrokeSequence)
    fmt.Printf("Keystroke is empty? %t\n", e.AutoType.Association.KeystrokeSequence == "")
}

The Window association is correct, but the keystroke sequence is empty.

tobischo commented 3 years ago

Hey @xxxserxxx,

when I am looking at the raw data of the example you provided, then the keystroke sequence field you are trying to access is actually empty

Here an excerpt from the XML of the KDBX example file you provided:

<AutoType>
  <Enabled>True</Enabled>
  <DataTransferObfuscation>0</DataTransferObfuscation>
  <DefaultSequence>{USERNAME}{TAB}{PASSWORD}{TAB}{TOTP}</DefaultSequence>
  <Association>
    <Window>twmn</Window>
    <KeystrokeSequence/>
  </Association>
</AutoType>

If you did not add any specific keystroke sequence, then I assume you are trying to access the data held in DefaultSequence? That was actually missing and I added that just now in v3.2.1

xxxserxxx commented 3 years ago

Yes, I think that was it. I was expecting to see the custom key sequence for the entry; I didn't consider the custom sequence for the window association.

Does DefaultSequence contain the "use custom key sequence" set on the entry if it's set? If so, that'd do it.

2021-08-14-101107_888x475_scrot

tobischo commented 3 years ago

If you only edit the default, it will just replace the value in DefaultSequence

<AutoType>
  <Enabled>True</Enabled>
  <DataTransferObfuscation>0</DataTransferObfuscation>
  <DefaultSequence>{USERNAME}{TAB}{PASSWORD}{TAB}{TOTP}{ENTER}</DefaultSequence>
  <Association>
    <Window>twmn</Window>
    <KeystrokeSequence></KeystrokeSequence>
  </Association>
</AutoType>

You have to specifically go in and set a specific sequence for the window:

<AutoType>
  <Enabled>True</Enabled>
  <DataTransferObfuscation>0</DataTransferObfuscation>
  <DefaultSequence>{USERNAME}{TAB}{PASSWORD}{TAB}{TOTP}{ENTER}</DefaultSequence>
  <Association>
    <Window>twmn</Window>
    <KeystrokeSequence>{USERNAME}{TAB}{PASSWORD}{TAB}{TOTP}{ENTER}</KeystrokeSequence>
  </Association>
</AutoType>

Kind of makes sense actually, that in the context of the association, it has to be set to for the specific window association.

See Testdata.kdbx.zip for an example where it is set.

Generally, to me it would appear that you should check the window association first and if it is empty, check for the default.

xxxserxxx commented 3 years ago

This makes sense. Because of DefaultSequence I can close this ticket.

One other question though: the keepass format allows for multiple associations, correct? Like in the screenshot, it's "Associations". Shouldn't Entry.AutoType.Association be an array?

tobischo commented 3 years ago

You are right, it should be a slice and not a struct.

The XML does not really reveal much of that when there is only 1 Association by default.

Fixed in v3.2.2

xxxserxxx commented 3 years ago

Very cool, thank you.

xxxserxxx commented 3 years ago

FYI, this is going toward adding autotype to kpmenu; thanks again for the help.