Skip to content

Commit

Permalink
feat: don't store if MIME==x-kde-passwordManagerHint
Browse files Browse the repository at this point in the history
  • Loading branch information
mjholub committed Apr 1, 2024
1 parent 08ee1fb commit 1a0244b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cliphist.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"image"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -82,11 +83,30 @@ func main() {
}
}

func filterPasswordType() error {
out, err := exec.Command("wl-paste", "--list-types").Output()
if err != nil {
return fmt.Errorf("error while checking MIME types of clipboard data: %w", err)
}
if strings.Contains(string(out), "x-kde-passwordManagerHint") {
return fmt.Errorf("password data detected, not storing")
}

return nil
}

func store(in io.Reader, maxDedupeSearch, maxItems uint64) error {
input, err := io.ReadAll(in)
if err != nil {
return fmt.Errorf("read stdin: %w", err)
}
if err := filterPasswordType(); err != nil {
if err.Error() == "password data detected, not storing" {
return nil
}
return err
}

if len(input) > 5*1e6 { // don't store >5MB
return nil
}
Expand Down
31 changes: 31 additions & 0 deletions cliphist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
mathrand "math/rand"
"os"
"os/exec"
"strconv"
"testing"

Expand Down Expand Up @@ -46,6 +47,36 @@ func TestMain(m *testing.M) {
}))
}

func TestFilterPasswordType(t *testing.T) {
tests := []struct {
name string
inCommandArgs []string
want string
}{{
name: "explicitly specified x-kde-passwordManagerHint",
inCommandArgs: []string{"--type", "x-kde-passwordManagerHint", "testvalue"},
want: "password data detected, not storing",
}, {
name: "random string",
inCommandArgs: []string{"testvalue-blah"},
want: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := exec.Command("wl-copy", tt.inCommandArgs...).Run(); err != nil {
t.Errorf("failed to run wl-copy: %v", err)
}
got := filterPasswordType()
if got != nil && got.Error() != tt.want {
t.Errorf("filterPasswordType() = %v, want %v", got, tt.want)
}
})
}

}

func TestScripts(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata",
Expand Down

0 comments on commit 1a0244b

Please sign in to comment.