fix 2 small bugs in import obfuscation (#195)

The first bug fixed is not garbling package names that don't contain any code. For example, given the import path "github.com/foo/bar", "github.com" was treated as a package name that should be garbled, which doesn't make sense.

The other bug was incorrectly matching private package names inside import paths. Before, if "internal" was an imported package matched by GOPRIVATE, but "internal/foo" was not, any instance of "internal/foo" would still be garbled as "<garbled>/foo", which is incorrect.
pull/196/head
Andrew LeFevre 4 years ago committed by GitHub
parent cf290b8e6d
commit 6857aa1426
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -367,7 +367,9 @@ func explodeImportPath(path string) ([]string, []privateName) {
newPath += "/" + paths[i]
if isPrivate(newPath) {
pkgPaths = append(pkgPaths, newPath)
pkgNames = append(pkgNames, newPrivateName(paths[i], newPath))
if newName, ok := newPrivateName(paths[i], newPath); ok {
pkgNames = append(pkgNames, newName)
}
privateIdx = i + 1
restPrivate = true
@ -384,29 +386,35 @@ func explodeImportPath(path string) ([]string, []privateName) {
for i := privateIdx; i < len(paths); i++ {
newPath := pkgPaths[lastComboIdx-1] + "/" + paths[i]
pkgPaths = append(pkgPaths, newPath)
pkgNames = append(pkgNames, newPrivateName(paths[i], newPath))
if newName, ok := newPrivateName(paths[i], newPath); ok {
pkgNames = append(pkgNames, newName)
}
lastComboIdx++
}
lastPath := paths[len(paths)-1]
pkgNames = append(pkgNames, newPrivateName(lastPath, path))
if newName, ok := newPrivateName(lastPath, path); ok {
pkgNames = append(pkgNames, newName)
}
return pkgPaths, pkgNames
}
// newPrivateName creates a privateName, from a package name
// and package path, setting the seed to path's actionID if
// we know it; if not, the seed is set to the path itself.
func newPrivateName(name, path string) privateName {
pName := privateName{name: name}
if actionID := buildInfo.imports[path].actionID; actionID == nil {
pName.seed = []byte(path)
} else {
pName.seed = actionID
// newPrivateName creates a privateName from a package name
// and import path. The seed is set to path's actionID if
// we know the path. If not, the package doesn't contain any
// code, and false is returned so that the caller knows not
// to use this name as a private name.
func newPrivateName(name, path string) (privateName, bool) {
actionID := buildInfo.imports[path].actionID
if actionID == nil {
// log.Printf("*** Skipped %s of %s ***", name, path)
return privateName{}, false
}
return pName
pName := privateName{name: name, seed: actionID}
return pName, true
}
func dedupStrings(paths []string) []string {
@ -698,8 +706,15 @@ func privateImportIndex(symName string, privImports privateImports, nameDataSym
if off == -1 {
continue
} else if off < firstOff || firstOff == -1 {
firstOff = off
// preform the same check that matchPkg does above, but
// on the byte after the end of the match so we are
// completely sure we didn't match inside an import path
l = len(privatePkg)
if bAfter := off + l; bAfter < len(symName)-1 && symName[bAfter] != '.' && (!isSymbol(symName[bAfter]) || symName[bAfter] == '/') {
continue
}
firstOff = off
}
}

Loading…
Cancel
Save