update sanitization to handle ctrl_R widget

plus handle situations when ';' and '|||' separators are used together
pull/89/head
Simon Let 6 years ago
parent 5ccc7f4fd9
commit 944c8b52ee
  1. 2
      cmd/control/cmd/sanitize.go
  2. 110
      cmd/sanitize/main.go
  3. 1
      go.sum

@ -22,7 +22,7 @@ var sanitizeCmd = &cobra.Command{
fmt.Println(" HOW IT WORKS") fmt.Println(" HOW IT WORKS")
fmt.Println(" In sanitized history, all sensitive information is replaced with its SHA1 hashes.") fmt.Println(" In sanitized history, all sensitive information is replaced with its SHA1 hashes.")
fmt.Println() fmt.Println()
fmt.Println("Sanitizing ...") fmt.Println("Resulting sanitized files ...")
fmt.Println(" * ~/resh_history_sanitized.json (full lengh hashes)") fmt.Println(" * ~/resh_history_sanitized.json (full lengh hashes)")
execCmd := exec.Command("resh-sanitize", "-trim-hashes", "0", "--output", dir+"/resh_history_sanitized.json") execCmd := exec.Command("resh-sanitize", "-trim-hashes", "0", "--output", dir+"/resh_history_sanitized.json")
execCmd.Stdout = os.Stdout execCmd.Stdout = os.Stdout

@ -10,6 +10,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"math"
"net/url" "net/url"
"os" "os"
"os/user" "os/user"
@ -19,6 +20,7 @@ import (
"strings" "strings"
"unicode" "unicode"
"github.com/coreos/go-semver/semver"
"github.com/curusarn/resh/pkg/records" "github.com/curusarn/resh/pkg/records"
giturls "github.com/whilp/git-urls" giturls "github.com/whilp/git-urls"
) )
@ -178,7 +180,7 @@ func (s *sanitizer) sanitizeRecord(record *records.Record) error {
} }
if len(record.RecallActionsRaw) > 0 { if len(record.RecallActionsRaw) > 0 {
record.RecallActionsRaw, err = s.sanitizeRecallActions(record.RecallActionsRaw) record.RecallActionsRaw, err = s.sanitizeRecallActions(record.RecallActionsRaw, record.ReshVersion)
if err != nil { if err != nil {
log.Fatal("RecallActionsRaw:", record.RecallActionsRaw, "; sanitization error:", err) log.Fatal("RecallActionsRaw:", record.RecallActionsRaw, "; sanitization error:", err)
} }
@ -188,44 +190,80 @@ func (s *sanitizer) sanitizeRecord(record *records.Record) error {
return nil return nil
} }
// sanitizes the recall actions by replacing the recall prefix with it's length func fixSeparator(str string) string {
func (s *sanitizer) sanitizeRecallActions(str string) (string, error) { if len(str) > 0 && str[0] == ';' {
sanStr := "" return "|||" + str[1:]
divider := ";" }
if strings.Contains(str, "|||") { return str
divider = "|||" }
// normal mode
} func minIndex(str string, substrs []string) (idx, substrIdx int) {
for x, actionStr := range strings.Split(str, divider+"arrow_") { minMatch := math.MaxInt32
if x == 0 { for i, sep := range substrs {
continue match := strings.Index(str, sep)
if match != -1 && match < minMatch {
minMatch = match
substrIdx = i
} }
if len(actionStr) == 0 { }
return str, errors.New("Action can't be empty; idx=" + strconv.Itoa(x)) idx = minMatch
return
}
// sanitizes the recall actions by replacing the recall prefix with it's length
func (s *sanitizer) sanitizeRecallActions(str string, reshVersion string) (string, error) {
if len(str) == 0 {
return "", nil
}
var separators []string
seps := []string{"|||"}
refVersion, err := semver.NewVersion("2.5.14")
if err != nil {
return str, err
}
if len(reshVersion) == 0 {
return str, errors.New("sanitizeRecallActions: record.ReshVersion is an empty string")
}
recordVersion, err := semver.NewVersion(reshVersion[1:])
if err != nil {
return str, err
}
if recordVersion.LessThan(*refVersion) {
seps = append(seps, ";")
}
actions := []string{"arrow_up", "arrow_down", "control_R"}
for _, sep := range seps {
for _, action := range actions {
separators = append(separators, sep+action+":")
} }
var action string }
var prefix string /*
if strings.HasPrefix(actionStr, "up:") { - find any of {|||,;}{arrow_up,arrow_down,control_R}: in the recallActions (on the lowest index)
action = "arrow_up" - use found substring to parse out the next prefix
if len(actionStr) < 3 { - sanitize prefix
return str, errors.New("Action is too short:" + actionStr) - add fixed substring and sanitized prefix to output
} */
if len(actionStr) != 3 { doBreak := false
prefix = actionStr[4:] sanStr := ""
} idx := 0
} else if strings.HasPrefix(actionStr, "down:") { var currSeparator string
action = "arrow_down" tokenLen, sepIdx := minIndex(str, separators)
if len(actionStr) < 5 { if tokenLen != 0 {
return str, errors.New("Action is too short:" + actionStr) return str, errors.New("sanitizeReacallActions: unexpected string before first action/separator")
} }
if len(actionStr) != 5 { currSeparator = separators[sepIdx]
prefix = actionStr[6:] idx += len(currSeparator)
} for !doBreak {
} else { tokenLen, sepIdx := minIndex(str[idx:], separators)
return str, errors.New("Action should start with one of (arrow_up, arrow_down); got: arrow_" + actionStr) if tokenLen > len(str[idx:]) {
tokenLen = len(str[idx:])
doBreak = true
} }
sanPrefix := strconv.Itoa(len(prefix)) // token := str[idx : idx+tokenLen]
sanStr += "|||" + action + ":" + sanPrefix sanStr += fixSeparator(currSeparator) + strconv.Itoa(tokenLen)
idx += tokenLen + len(currSeparator)
currSeparator = separators[sepIdx]
} }
return sanStr, nil return sanStr, nil
} }

@ -7,6 +7,7 @@ github.com/awesome-gocui/termbox-go v0.0.0-20190427202837-c0aef3d18bcc h1:wGNpKc
github.com/awesome-gocui/termbox-go v0.0.0-20190427202837-c0aef3d18bcc/go.mod h1:tOy3o5Nf1bA17mnK4W41gD7PS3u4Cv0P0pqFcoWMy8s= github.com/awesome-gocui/termbox-go v0.0.0-20190427202837-c0aef3d18bcc/go.mod h1:tOy3o5Nf1bA17mnK4W41gD7PS3u4Cv0P0pqFcoWMy8s=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0 h1:3Jm3tLmsgAYcjC+4Up7hJrFBPr+n7rAqYeSw/SZazuY=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

Loading…
Cancel
Save