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

Loading…
Cancel
Save