Skip to content

Commit db3bf5f

Browse files
committed
FAB-16110 sanitize pkgID for externalbuilder
replace non-alphanumerics with dash Change-Id: I099c3dd1eb52091acaf69eb8930a1513aca22d1f Signed-off-by: Jay Guo <guojiannan1101@gmail.com>
1 parent 920c497 commit db3bf5f

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

core/container/externalbuilders/externalbuilders.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"os"
1616
"os/exec"
1717
"path/filepath"
18-
"strings"
18+
"regexp"
1919

2020
"github.com/hyperledger/fabric/common/flogging"
2121
"github.com/hyperledger/fabric/core/common/ccprovider"
@@ -100,9 +100,10 @@ type BuildContext struct {
100100
LaunchDir string
101101
}
102102

103+
var pkgIDreg = regexp.MustCompile("[^a-zA-Z0-9]+")
104+
103105
func NewBuildContext(ccci *ccprovider.ChaincodeContainerInfo, codePackage io.Reader) (*BuildContext, error) {
104-
// TODO, investigate if any other characters need sanitizing (we cannot have colons in the go path, for instance)
105-
scratchDir, err := ioutil.TempDir("", "fabric-"+strings.ReplaceAll(string(ccci.PackageID), string(os.PathListSeparator), "-"))
106+
scratchDir, err := ioutil.TempDir("", "fabric-"+pkgIDreg.ReplaceAllString(string(ccci.PackageID), "-"))
106107
if err != nil {
107108
return nil, errors.WithMessage(err, "could not create temp dir")
108109
}

core/container/externalbuilders/externalbuilders_test.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,6 @@ var _ = Describe("Externalbuilders", func() {
6060
Expect(err).NotTo(HaveOccurred())
6161
})
6262

63-
Context("when the tmp dir cannot be created", func() {
64-
BeforeEach(func() {
65-
ccci.PackageID = "/"
66-
})
67-
68-
It("returns an error", func() {
69-
_, err := externalbuilders.NewBuildContext(ccci, codePackage)
70-
Expect(err).To(MatchError(ContainSubstring("could not create temp dir")))
71-
})
72-
})
73-
7463
Context("when the archive cannot be extracted", func() {
7564
BeforeEach(func() {
7665
var err error
@@ -84,6 +73,16 @@ var _ = Describe("Externalbuilders", func() {
8473
Expect(err).To(MatchError(ContainSubstring("could not untar source package")))
8574
})
8675
})
76+
77+
Context("when package id contains inappropriate chars", func() {
78+
It("replaces them with dash", func() {
79+
ccci.PackageID = "i&am/pkg:id"
80+
81+
buildContext, err := externalbuilders.NewBuildContext(ccci, codePackage)
82+
Expect(err).NotTo(HaveOccurred())
83+
Expect(buildContext.ScratchDir).To(ContainSubstring("fabric-i-am-pkg-id"))
84+
})
85+
})
8786
})
8887

8988
Describe("Detector", func() {
@@ -103,17 +102,6 @@ var _ = Describe("Externalbuilders", func() {
103102
Expect(instance.(*externalbuilders.Instance).Builder).To(Equal(&externalbuilders.Builder{Location: "testdata"}))
104103
})
105104

106-
Context("when the build context cannot be created", func() {
107-
BeforeEach(func() {
108-
ccci.PackageID = "/"
109-
})
110-
111-
It("returns an error", func() {
112-
_, err := detector.Build(ccci, codePackageBytes)
113-
Expect(err).To(MatchError(ContainSubstring("could not create build context")))
114-
})
115-
})
116-
117105
Context("when no builder can be found", func() {
118106
BeforeEach(func() {
119107
detector.Builders = nil

0 commit comments

Comments
 (0)