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

Remove DEL characters from password input #5837

Merged
merged 3 commits into from
Dec 12, 2018
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
7 changes: 6 additions & 1 deletion helper/password/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"
"os/signal"
"strings"
)

var ErrInterrupted = errors.New("interrupted")
Expand All @@ -34,7 +35,7 @@ func Read(f *os.File) (string, error) {
case <-ch:
return "", ErrInterrupted
case <-doneCh:
return result, resultErr
return removeiTermDelete(result), resultErr
}
}

Expand Down Expand Up @@ -62,3 +63,7 @@ func readline(f *os.File) (string, error) {

return string(resultBuf), nil
}

func removeiTermDelete(input string) string {
return strings.TrimPrefix(input, "\x20\x7f")
}
27 changes: 27 additions & 0 deletions helper/password/password_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package password

import "testing"

type testCase struct {
name string
input string
expected string
}

func TestRemoveiTermDelete(t *testing.T) {
var tests = []testCase{
{"NoDelete", "TestingStuff", "TestingStuff"},
{"SingleDelete", "Testing\x7fStuff", "Testing\x7fStuff"},
{"DeleteFirst", "\x7fTestingStuff", "\x7fTestingStuff"},
{"DoubleDelete", "\x7f\x7fTestingStuff", "\x7f\x7fTestingStuff"},
{"SpaceFirst", "\x20TestingStuff", "\x20TestingStuff"},
{"iTermDelete", "\x20\x7fTestingStuff", "TestingStuff"},
}

for _, test := range tests {
result := removeiTermDelete(test.input)
if result != test.expected {
t.Errorf("Test %s failed, input: '%s', expected: '%s', output: '%s'", test.name, test.input, test.expected, result)
}
}
}