Skip to content

Commit

Permalink
Transform saltpack errors to remove filenames (#22983)
Browse files Browse the repository at this point in the history
* Transform saltpack errors to remove filenames

* Transform os.PathError inside zip creation

* Fix gocritic garbage
  • Loading branch information
patrickxb authored and cjb committed Mar 10, 2020
1 parent 263d2ce commit f2fc2d1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
3 changes: 3 additions & 0 deletions go/client/stellar_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ func transformStellarCLIError(err *error) {
if err == nil {
return
}
if *err == nil {
return
}
switch e := (*err).(type) {
case libkb.AppStatusError:
if e.Code == libkb.SCStellarNeedDisclaimer {
Expand Down
58 changes: 48 additions & 10 deletions go/service/saltpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ func (h *SaltpackHandler) SaltpackEncryptString(ctx context.Context, arg keybase
return h.encryptString(ctx, arg.SessionID, arg.Plaintext, eopts)
}

func (h *SaltpackHandler) SaltpackEncryptStringToTextFile(ctx context.Context, arg keybase1.SaltpackEncryptStringToTextFileArg) (keybase1.SaltpackEncryptFileResult, error) {
func (h *SaltpackHandler) SaltpackEncryptStringToTextFile(ctx context.Context, arg keybase1.SaltpackEncryptStringToTextFileArg) (r keybase1.SaltpackEncryptFileResult, err error) {
defer transformSaltpackError(&err)
ctx = libkb.WithLogTag(ctx, "SP")
eopts := h.encryptOptions(arg.Opts)
res, err := h.encryptString(ctx, arg.SessionID, arg.Plaintext, eopts)
Expand Down Expand Up @@ -250,7 +251,8 @@ func (h *SaltpackHandler) SaltpackSignString(ctx context.Context, arg keybase1.S
return h.signString(ctx, arg.SessionID, arg.Plaintext)
}

func (h *SaltpackHandler) SaltpackSignStringToTextFile(ctx context.Context, arg keybase1.SaltpackSignStringToTextFileArg) (string, error) {
func (h *SaltpackHandler) SaltpackSignStringToTextFile(ctx context.Context, arg keybase1.SaltpackSignStringToTextFileArg) (s string, err error) {
defer transformSaltpackError(&err)
ctx = libkb.WithLogTag(ctx, "SP")
signed, err := h.signString(ctx, arg.SessionID, arg.Plaintext)
if err != nil {
Expand Down Expand Up @@ -299,7 +301,8 @@ func (h *SaltpackHandler) SaltpackVerifyString(ctx context.Context, arg keybase1
return res, nil
}

func (h *SaltpackHandler) SaltpackEncryptFile(ctx context.Context, arg keybase1.SaltpackEncryptFileArg) (keybase1.SaltpackEncryptFileResult, error) {
func (h *SaltpackHandler) SaltpackEncryptFile(ctx context.Context, arg keybase1.SaltpackEncryptFileArg) (r keybase1.SaltpackEncryptFileResult, err error) {
defer transformSaltpackError(&err)
ctx = libkb.WithLogTag(ctx, "SP")
dir, err := isDir(arg.Filename)
if err != nil {
Expand Down Expand Up @@ -339,9 +342,11 @@ func (h *SaltpackHandler) saltpackEncryptFile(ctx context.Context, arg keybase1.
if err != nil {
h.G().Log.Debug("encrypt error, so removing output file")
if clErr := bw.Close(); clErr != nil {
transformSaltpackError(&clErr)
h.G().Log.Debug("error closing bw for output file: %s", clErr)
}
if rmErr := os.Remove(outFilename); rmErr != nil {
transformSaltpackError(&rmErr)
h.G().Log.Debug("error removing output file: %s", rmErr)
}
return keybase1.SaltpackEncryptFileResult{}, err
Expand Down Expand Up @@ -393,9 +398,11 @@ func (h *SaltpackHandler) saltpackEncryptDirectory(ctx context.Context, arg keyb
if err != nil {
h.G().Log.Debug("encrypt error, so removing output file")
if clErr := bw.Close(); clErr != nil {
transformSaltpackError(&clErr)
h.G().Log.Debug("error closing bw for output file: %s", clErr)
}
if rmErr := os.Remove(outFilename); rmErr != nil {
transformSaltpackError(&rmErr)
h.G().Log.Debug("error removing output file: %s", rmErr)
}
return keybase1.SaltpackEncryptFileResult{}, err
Expand All @@ -410,7 +417,8 @@ func (h *SaltpackHandler) saltpackEncryptDirectory(ctx context.Context, arg keyb
}, nil
}

func (h *SaltpackHandler) SaltpackDecryptFile(ctx context.Context, arg keybase1.SaltpackDecryptFileArg) (keybase1.SaltpackFileResult, error) {
func (h *SaltpackHandler) SaltpackDecryptFile(ctx context.Context, arg keybase1.SaltpackDecryptFileArg) (r keybase1.SaltpackFileResult, err error) {
defer transformSaltpackError(&err)
ctx = libkb.WithLogTag(ctx, "SP")
sf, err := newSourceFile(h.G(), keybase1.SaltpackOperationType_DECRYPT, arg.EncryptedFilename)
if err != nil {
Expand All @@ -433,23 +441,26 @@ func (h *SaltpackHandler) SaltpackDecryptFile(ctx context.Context, arg keybase1.
if err != nil {
h.G().Log.Debug("decrypt error, so removing output file")
if clErr := bw.Close(); clErr != nil {
transformSaltpackError(&clErr)
h.G().Log.Debug("error closing bw for output file: %s", clErr)
}
if rmErr := os.Remove(outFilename); rmErr != nil {
transformSaltpackError(&rmErr)
h.G().Log.Debug("error removing output file: %s", rmErr)
}
return keybase1.SaltpackFileResult{}, err
}

r := keybase1.SaltpackFileResult{
r = keybase1.SaltpackFileResult{
Info: info,
DecryptedFilename: outFilename,
Signed: signed,
}
return r, nil
}

func (h *SaltpackHandler) SaltpackSignFile(ctx context.Context, arg keybase1.SaltpackSignFileArg) (string, error) {
func (h *SaltpackHandler) SaltpackSignFile(ctx context.Context, arg keybase1.SaltpackSignFileArg) (s string, err error) {
defer transformSaltpackError(&err)
ctx = libkb.WithLogTag(ctx, "SP")
dir, err := isDir(arg.Filename)
if err != nil {
Expand Down Expand Up @@ -486,9 +497,11 @@ func (h *SaltpackHandler) saltpackSignFile(ctx context.Context, arg keybase1.Sal
if err := h.frontendSign(ctx, arg.SessionID, earg); err != nil {
h.G().Log.Debug("sign error, so removing output file")
if clErr := bw.Close(); clErr != nil {
transformSaltpackError(&clErr)
h.G().Log.Debug("error closing bw for output file: %s", clErr)
}
if rmErr := os.Remove(outFilename); rmErr != nil {
transformSaltpackError(&rmErr)
h.G().Log.Debug("error removing output file: %s", rmErr)
}
return "", err
Expand Down Expand Up @@ -534,9 +547,11 @@ func (h *SaltpackHandler) saltpackSignDirectory(ctx context.Context, arg keybase
if err := h.frontendSign(ctx, arg.SessionID, earg); err != nil {
h.G().Log.Debug("sign error, so removing output file")
if clErr := bw.Close(); clErr != nil {
transformSaltpackError(&clErr)
h.G().Log.Debug("error closing bw for output file: %s", clErr)
}
if rmErr := os.Remove(outFilename); rmErr != nil {
transformSaltpackError(&rmErr)
h.G().Log.Debug("error removing output file: %s", rmErr)
}
return "", err
Expand All @@ -547,7 +562,8 @@ func (h *SaltpackHandler) saltpackSignDirectory(ctx context.Context, arg keybase
return outFilename, nil
}

func (h *SaltpackHandler) SaltpackVerifyFile(ctx context.Context, arg keybase1.SaltpackVerifyFileArg) (keybase1.SaltpackVerifyFileResult, error) {
func (h *SaltpackHandler) SaltpackVerifyFile(ctx context.Context, arg keybase1.SaltpackVerifyFileArg) (r keybase1.SaltpackVerifyFileResult, err error) {
defer transformSaltpackError(&err)
ctx = libkb.WithLogTag(ctx, "SP")
sf, err := newSourceFile(h.G(), keybase1.SaltpackOperationType_VERIFY, arg.SignedFilename)
if err != nil {
Expand All @@ -570,9 +586,11 @@ func (h *SaltpackHandler) SaltpackVerifyFile(ctx context.Context, arg keybase1.S
if err != nil {
h.G().Log.Debug("verify error, so removing ouput file")
if clErr := bw.Close(); clErr != nil {
transformSaltpackError(&clErr)
h.G().Log.Debug("error closing bw for output file: %s", clErr)
}
if rmErr := os.Remove(outFilename); rmErr != nil {
transformSaltpackError(&rmErr)
h.G().Log.Debug("error removing output file: %s", rmErr)
}
return keybase1.SaltpackVerifyFileResult{}, err
Expand All @@ -590,12 +608,14 @@ func (h *SaltpackHandler) SaltpackVerifyFile(ctx context.Context, arg keybase1.S
return res, nil
}

func (h *SaltpackHandler) SaltpackSaveCiphertextToFile(ctx context.Context, arg keybase1.SaltpackSaveCiphertextToFileArg) (string, error) {
func (h *SaltpackHandler) SaltpackSaveCiphertextToFile(ctx context.Context, arg keybase1.SaltpackSaveCiphertextToFileArg) (s string, err error) {
defer transformSaltpackError(&err)
ctx = libkb.WithLogTag(ctx, "SP")
return h.writeStringToFile(ctx, arg.Ciphertext, txtExtension+encryptedExtension)
}

func (h *SaltpackHandler) SaltpackSaveSignedMsgToFile(ctx context.Context, arg keybase1.SaltpackSaveSignedMsgToFileArg) (string, error) {
func (h *SaltpackHandler) SaltpackSaveSignedMsgToFile(ctx context.Context, arg keybase1.SaltpackSaveSignedMsgToFileArg) (s string, err error) {
defer transformSaltpackError(&err)
ctx = libkb.WithLogTag(ctx, "SP")
return h.writeStringToFile(ctx, arg.SignedMsg, txtExtension+signedExtension)
}
Expand Down Expand Up @@ -894,7 +914,8 @@ func zipDir(directory string, prog *progress.ProgressWriter) io.ReadCloser {
}

go func() {
err := filepath.Walk(directory, func(path string, info os.FileInfo, inErr error) error {
err := filepath.Walk(directory, func(path string, info os.FileInfo, inErr error) (outErr error) {
defer transformSaltpackError(&outErr)
if inErr != nil {
return inErr
}
Expand Down Expand Up @@ -976,3 +997,20 @@ func newDirProgressWriter(p types.ProgressReporter, dir string) (*progress.Progr

return progress.NewProgressWriterWithUpdateDuration(p, size, 80*time.Millisecond), nil
}

func transformSaltpackError(err *error) {
if err == nil {
return
}
if *err == nil {
return
}

switch e := (*err).(type) {
case *os.PathError:
// this should remove the path from the error
*err = e.Unwrap()
default:
// golangci-lint/gocritic, you really drive me crazy sometimes...
}
}

0 comments on commit f2fc2d1

Please sign in to comment.