Fix literal obfuscation edge case.

String literals can be assigned to variables with a type that is an alias of string.

Function calls with return type string cannot be assigned to these variables.

This skips all var and const blocks which contain such assignments.
pull/37/head
lu4p 5 years ago
parent 4c64b13506
commit 75bf40cd8e

@ -14,17 +14,31 @@ import (
func obfuscateLiterals(files []*ast.File) []*ast.File {
pre := func(cursor *astutil.Cursor) bool {
decl, ok := cursor.Node().(*ast.GenDecl)
if !ok || decl.Tok != token.CONST {
if !ok {
return true
}
for _, spec := range decl.Specs {
for _, val := range spec.(*ast.ValueSpec).Values {
spec, ok := spec.(*ast.ValueSpec)
if !ok {
return false
}
if t, ok := spec.Type.(*ast.Ident); ok {
if t.Name != "string" {
return false // if the spec has a explicit type name of non string skip it
}
}
for _, val := range spec.Values {
if v, ok := val.(*ast.BasicLit); !ok || v.Kind != token.STRING {
return false // skip the block if it contains non basic literals
}
}
}
if decl.Tok != token.CONST {
return true
}
// constants are not possible if we want to obfuscate literals, therefore
// move all constant blocks which only contain strings to variables
@ -56,6 +70,8 @@ func obfuscateLiterals(files []*ast.File) []*ast.File {
return true // TODO: garble literals other than strings
}
cursor.Parent()
value, err := strconv.Unquote(x.Value)
if err != nil {
panic(fmt.Sprintf("cannot unquote string: %v", err))

@ -4,7 +4,7 @@ exec ./main$exe
cmp stderr main.stderr
! binsubstr main$exe 'Lorem' 'ipsum' 'dolor' 'first assign' 'second assign' 'First Line' 'Second Line' 'map value' 'to obfuscate' 'also obfuscate'
binsubstr main$exe 'Skip this block,' 'also skip this'
binsubstr main$exe 'Skip this block,' 'also skip this' 'skip typed const' 'skip typed var'
[short] stop # checking that the build is reproducible is slow
@ -30,6 +30,13 @@ const (
Second Line`
)
// stringType this broke previously
type stringType string
const skipTypedConst stringType = "skip typed const"
var skipTypedVar stringType = "skip typed var"
const (
skip1 = "Skip this block,"
i = 1
@ -78,6 +85,8 @@ func main() {
println(skip1, skip2)
println(skipTypedConst, skipTypedVar)
println(i, foo, bar)
}
@ -102,4 +111,5 @@ also obfuscate
map value
another literal
Skip this block, also skip this
skip typed const skip typed var
1 0 1

Loading…
Cancel
Save