-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-2122] Scan codepackage for illegal content
We allow each chaincode platform to define its own deployment validation logic, and then implement a FAB-2122 oriented filter for GOLANG. What this means in practice is we perform some basic checks against the tarball that was passed in via the DeploymentSpec. For instance, there is no reason for a client to submit binaries (+x set), files in /bin/*, or source packages outside of the declared package. As noted, this doesn't provide 100% protection but it does at least reject some basic things that are clearly garbage. Change-Id: I6c725d4cb0522a8be5717ab80d4f6205e83bf858 Signed-off-by: Greg Haskins <gregory.haskins@gmail.com>
- Loading branch information
Showing
6 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package golang | ||
|
||
import ( | ||
"testing" | ||
|
||
"archive/tar" | ||
"bytes" | ||
"compress/gzip" | ||
"time" | ||
|
||
pb "github.com/hyperledger/fabric/protos/peer" | ||
) | ||
|
||
func writeBytesToPackage(name string, payload []byte, mode int64, tw *tar.Writer) error { | ||
//Make headers identical by using zero time | ||
var zeroTime time.Time | ||
tw.WriteHeader(&tar.Header{Name: name, Size: int64(len(payload)), ModTime: zeroTime, AccessTime: zeroTime, ChangeTime: zeroTime, Mode: mode}) | ||
tw.Write(payload) | ||
|
||
return nil | ||
} | ||
|
||
func generateFakeCDS(path, file string, mode int64) (*pb.ChaincodeDeploymentSpec, error) { | ||
codePackage := bytes.NewBuffer(nil) | ||
gw := gzip.NewWriter(codePackage) | ||
tw := tar.NewWriter(gw) | ||
|
||
payload := make([]byte, 25, 25) | ||
err := writeBytesToPackage(file, payload, mode, tw) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tw.Close() | ||
gw.Close() | ||
|
||
cds := &pb.ChaincodeDeploymentSpec{ | ||
ChaincodeSpec: &pb.ChaincodeSpec{ | ||
ChaincodeId: &pb.ChaincodeID{ | ||
Name: "Bad Code", | ||
Path: path, | ||
}, | ||
}, | ||
CodePackage: codePackage.Bytes(), | ||
} | ||
|
||
return cds, nil | ||
} | ||
|
||
type spec struct { | ||
Path, File string | ||
Mode int64 | ||
SuccessExpected bool | ||
} | ||
|
||
func TestValidateCDS(t *testing.T) { | ||
platform := &Platform{} | ||
|
||
specs := make([]spec, 0) | ||
specs = append(specs, spec{Path: "path/to/nowhere", File: "/bin/warez", Mode: 0100400, SuccessExpected: false}) | ||
specs = append(specs, spec{Path: "path/to/somewhere", File: "/src/path/to/somewhere/main.go", Mode: 0100400, SuccessExpected: true}) | ||
specs = append(specs, spec{Path: "path/to/somewhere", File: "/src/path/to/somewhere/warez", Mode: 0100555, SuccessExpected: false}) | ||
|
||
for _, s := range specs { | ||
cds, err := generateFakeCDS(s.Path, s.File, s.Mode) | ||
|
||
err = platform.ValidateDeploymentSpec(cds) | ||
if s.SuccessExpected == true && err != nil { | ||
t.Errorf("Unexpected failure: %s", err) | ||
} | ||
if s.SuccessExpected == false && err == nil { | ||
t.Log("Expected validation failure") | ||
t.Fail() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters