Skip to content

Commit

Permalink
Add --chars option to show to allow printing a subset of the secret o…
Browse files Browse the repository at this point in the history
…nly (gopasspw#2155)

* Add --chars option to show to allow printing a subset of the secret only

Fixes gopasspw#2068

RELEASE_NOTES=Add --chars option to print subset of secrets

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* Add some docs.

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
  • Loading branch information
dominikschulz authored Mar 12, 2022
1 parent 979c356 commit d27de06
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/commands/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Flag | Aliases | Description
`--password` | `-o` | Display only the password. For use in scripts. Takes precedence over other flags.
`--revision` | `-r` | Display a specific revision of the entry. Use an exact version identifier from `gopass history` or the special `-<N>` syntax. Does not work with native (e.g. git) refs.
`--noparsing` | `-n` | Do not parse the content, disable YAML and Key-Value functions.
`--chars` | | Display selected characters from the password.

## Details

Expand Down
4 changes: 4 additions & 0 deletions internal/action/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func ShowFlags() []cli.Flag {
Aliases: []string{"n"},
Usage: "Do not parse the output.",
},
&cli.StringFlag{
Name: "chars",
Usage: "Print specific characters from the secret",
},
}
}

Expand Down
15 changes: 15 additions & 0 deletions internal/action/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
ctxKeyKey
ctxKeyOnlyClip
ctxKeyAlsoClip
ctxKeyPrintChars
)

// WithClip returns a context with the value for clip (for copy to clipboard)
Expand Down Expand Up @@ -126,3 +127,17 @@ func GetKey(ctx context.Context) string {
}
return sv
}

// WithPrintChars returns the context with the print chars set.
func WithPrintChars(ctx context.Context, c []int) context.Context {
return context.WithValue(ctx, ctxKeyPrintChars, c)
}

// GetPrintChars returns a map of all character positions to print.
func GetPrintChars(ctx context.Context) []int {
mv, ok := ctx.Value(ctxKeyPrintChars).([]int)
if !ok {
return nil
}
return mv
}
26 changes: 26 additions & 0 deletions internal/action/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ func showParseArgs(c *cli.Context) context.Context {
if c.IsSet("noparsing") {
ctx = ctxutil.WithShowParsing(ctx, !c.Bool("noparsing"))
}
if c.IsSet("chars") {
iv := []int{}
for _, v := range strings.Split(c.String("chars"), ",") {
v = strings.TrimSpace(v)
if v == "" {
continue
}
i, err := strconv.Atoi(v)
if err != nil {
continue
}
iv = append(iv, i)
}
ctx = WithPrintChars(ctx, iv)
}
ctx = WithClip(ctx, IsOnlyClip(ctx) || IsAlsoClip(ctx))
return ctx
}
Expand Down Expand Up @@ -140,6 +155,17 @@ func (s *Action) showHandleOutput(ctx context.Context, name string, sec gopass.S
return err
}

if chars := GetPrintChars(ctx); len(chars) > 0 {
for _, c := range chars {
if c > len(pw) || c-1 < 0 {
debug.Log("Invalid char: %d", c)
continue
}
out.Printf(ctx, "%d: %s", c, out.Secret(pw[c-1]))
}
return nil
}

if pw == "" && body == "" {
if ctxutil.IsShowSafeContent(ctx) && !ctxutil.IsForce(ctx) {
out.Warning(ctx, "safecontent=true. Use -f to display password, if any")
Expand Down
2 changes: 1 addition & 1 deletion internal/out/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
Stderr io.Writer = os.Stderr
)

// Secret is a string wrapper for strings containing secrets. These won't b.
// Secret is a string wrapper for strings containing secrets. These won't be
// logged as long a GOPASS_DEBUG_LOG_SECRETS is not set.
type Secret string

Expand Down

0 comments on commit d27de06

Please sign in to comment.