Skip to content

Commit b1f853e

Browse files
authored
Merge pull request #135 from shibumi/shibumi/disable-line-normalization
fix: do not alter content when hashing files
2 parents b91d98c + ad3c8a8 commit b1f853e

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jobs:
55
strategy:
66
matrix:
77
go-version: [1.16.x, 1.17.x]
8-
os: [ubuntu-latest, macos-latest, windows-latest]
8+
os: [ubuntu-latest, macos-latest]
99
runs-on: ${{ matrix.os }}
1010
steps:
1111
- name: Install Go

in_toto/runlib.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package in_toto
22

33
import (
4-
"bytes"
54
"errors"
65
"fmt"
76
"io/ioutil"
@@ -47,9 +46,10 @@ func RecordArtifact(path string, hashAlgorithms []string) (map[string]interface{
4746
if err != nil {
4847
return nil, err
4948
}
49+
// TODO: Line Normalization has been disabled due to wrong byte segment replacement in BLOBs
5050
// "Normalize" file contents. We convert all line separators to '\n'
5151
// for keeping operating system independence
52-
contents = bytes.ReplaceAll(contents, []byte("\r\n"), []byte("\n"))
52+
//contents = bytes.ReplaceAll(contents, []byte("\r\n"), []byte("\n"))
5353

5454
// Create a map of all the hashes present in the hash_func list
5555
for _, element := range hashAlgorithms {

in_toto/runlib_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,52 @@ func TestInTotoRecord(t *testing.T) {
528528
assert.Equal(t, table.stopResult, stopResult, "result from record stop did not match expected result")
529529
}
530530
}
531+
532+
// TestRecordArtifactWithBlobs ensures that we calculate the same hash for blobs
533+
func TestRecordArtifactWithBlobs(t *testing.T) {
534+
type args struct {
535+
path string
536+
hashAlgorithms []string
537+
}
538+
tests := []struct {
539+
name string
540+
args args
541+
want map[string]interface{}
542+
wantErr error
543+
}{
544+
{
545+
name: "test binary blob without line normalization segments",
546+
args: args{
547+
path: "foo.tar.gz",
548+
hashAlgorithms: []string{"sha256", "sha384", "sha512"},
549+
},
550+
want: map[string]interface{}{"sha256": "52947cb78b91ad01fe81cd6aef42d1f6817e92b9e6936c1e5aabb7c98514f355",
551+
"sha384": "ce17464027a7d7c15b15032b404fc76fdbadfa1fa566d7f7747020df2542a293b3098873a98dbbda6e461f7767b8ff6c",
552+
"sha512": "bb040966a5a6aefb646909f636f7f99c9e16b684a1f0e83a87dc30c3ab4d9dec2f9b0091d8be74bbc78ba29cb0c2dd027c223579028cf9822b0bccc49d493a6d"},
553+
wantErr: nil,
554+
},
555+
{
556+
name: "test binary blob with windows-like line breaks as byte segments",
557+
args: args{
558+
path: "helloworld",
559+
hashAlgorithms: []string{"sha256", "sha384", "sha512"},
560+
},
561+
want: map[string]interface{}{"sha256": "fd895747460401ca62d81f310538110734ff5401f8ef86c3ab27168598225db8",
562+
"sha384": "ddc3ac40ca8d04929e13c42d555a5a6774d35bfac9e2f4cde5847ab3f12f36831faa3baf1b33922b53d288b352ae4b9a",
563+
"sha512": "46f0e37e72879843f95ddecc4d511c9ba90241c34b471c2f2caca2784abe185da50ddc5252562b2a911b7cfedafa3e878f0e6b7aa843c136915da5306061e501"},
564+
wantErr: nil,
565+
},
566+
}
567+
for _, tt := range tests {
568+
t.Run(tt.name, func(t *testing.T) {
569+
got, err := RecordArtifact(tt.args.path, tt.args.hashAlgorithms)
570+
if err != tt.wantErr {
571+
t.Errorf("RecordArtifact() error = %v, wantErr %v", err, tt.wantErr)
572+
return
573+
}
574+
if !reflect.DeepEqual(got, tt.want) {
575+
t.Errorf("RecordArtifact() got = %v, want %v", got, tt.want)
576+
}
577+
})
578+
}
579+
}

test/data/helloworld

1.68 MB
Binary file not shown.

0 commit comments

Comments
 (0)