Skip to content

Commit

Permalink
Validate label when packaging _lifecycle chaincode
Browse files Browse the repository at this point in the history
FAB-17126

Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
  • Loading branch information
wlahti authored and sykesm committed Jan 28, 2020
1 parent 419690a commit 3777652
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
8 changes: 5 additions & 3 deletions core/chaincode/persistence/chaincode_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,13 @@ type ChaincodePackageParser struct {
MetadataProvider MetadataProvider
}

// LabelRegexp is the regular expression controlling the allowed characters
// LabelRegexp is the regular expression controlling the allowed characters
// for the package label.
var LabelRegexp = regexp.MustCompile(`^[[:alnum:]][[:alnum:]_.+-]*$`)

func validateLabel(label string) error {
// ValidateLabel return an error if the provided label contains any invalid
// characters, as determined by LabelRegexp.
func ValidateLabel(label string) error {
if !LabelRegexp.MatchString(label) {
return errors.Errorf("invalid label '%s'. Label must be non-empty, can only consist of alphanumerics, symbols from '.+-_', and can only begin with alphanumerics", label)
}
Expand Down Expand Up @@ -307,7 +309,7 @@ func (ccpp ChaincodePackageParser) Parse(source []byte) (*ChaincodePackage, erro
return nil, errors.Errorf("did not find any package metadata (missing %s)", MetadataFile)
}

if err := validateLabel(ccPackageMetadata.Label); err != nil {
if err := ValidateLabel(ccPackageMetadata.Label); err != nil {
return nil, err
}

Expand Down
3 changes: 2 additions & 1 deletion core/chaincode/persistence/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ func TestLabels(t *testing.T) {
{label: "a:b", success: false},
{label: "a__b", success: true},
{label: "a_b", success: true},
{label: "a a", success: false},
{label: "a_bb", success: true},
{label: "aa", success: true},
{label: "v1.0.0", success: true},
}

for _, tt := range tests {
t.Run(tt.label, func(t *testing.T) {
err := validateLabel(tt.label)
err := ValidateLabel(tt.label)
if tt.success {
assert.NoError(t, err)
} else {
Expand Down
3 changes: 3 additions & 0 deletions internal/peer/lifecycle/chaincode/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func (p *PackageInput) Validate() error {
if p.Label == "" {
return errors.New("package label must be specified")
}
if err := persistence.ValidateLabel(p.Label); err != nil {
return err
}

return nil
}
Expand Down
11 changes: 11 additions & 0 deletions internal/peer/lifecycle/chaincode/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ var _ = Describe("Package", func() {
})
})

Context("when the label is invalid", func() {
BeforeEach(func() {
input.Label = "label with spaces"
})

It("returns an error", func() {
err := packager.Package()
Expect(err).To(MatchError("invalid label 'label with spaces'. Label must be non-empty, can only consist of alphanumerics, symbols from '.+-_', and can only begin with alphanumerics"))
})
})

Context("when the platform registry fails to normalize the path", func() {
BeforeEach(func() {
mockPlatformRegistry.NormalizePathReturns("", errors.New("cortado"))
Expand Down

0 comments on commit 3777652

Please sign in to comment.