Skip to content

Commit ac8c13b

Browse files
authored
Fix: Reference CSV Column Name rather than Number, Add Check (#171)
2 parents 62b46eb + 3de5957 commit ac8c13b

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

internal/analyse/analyse.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import (
1616
type NoteLevel string
1717

1818
const (
19-
NoteLevelWarn NoteLevel = "warn"
20-
NoteLevelError NoteLevel = "error"
19+
NoteLevelWarn NoteLevel = "warn"
20+
NoteLevelError NoteLevel = "error"
21+
fingerprintHeader = "SHA-256 Fingerprint"
22+
commentsHeader = "Comments"
2123
)
2224

2325
type Note struct {
@@ -53,20 +55,46 @@ func downloadMozillaRemovedCACertsList() ([]removedCertificate, error) {
5355
return nil, err
5456
}
5557
csvReader := csv.NewReader(resp.Body)
56-
csvLines, err := csvReader.ReadAll()
58+
// Read the header first
59+
headers, err := csvReader.Read()
5760
if err != nil {
5861
return nil, err
5962
}
6063

61-
removedCerts := make([]removedCertificate, len(csvLines))
62-
for i, csvLine := range csvLines {
63-
removedCerts[i] = removedCertificate{
64-
// From the CSV format that Mozilla publishes, the 8th column (id 7) is the fingerprint and the 23rd column
65-
// (id 22) is the comment.
66-
Fingerprint: csvLine[7],
67-
Comments: csvLine[22],
64+
// Find column indices by their names from the header
65+
fingerprint := -1
66+
comments := -1
67+
for idx, header := range headers {
68+
switch header {
69+
case fingerprintHeader:
70+
fingerprint = idx
71+
case commentsHeader:
72+
comments = idx
6873
}
6974
}
75+
76+
if fingerprint == -1 {
77+
return nil, fmt.Errorf("required column 'SHA-256 Fingerprint' not found in CSV header")
78+
}
79+
if comments == -1 {
80+
return nil, fmt.Errorf("required column 'Comments' not found in CSV header")
81+
}
82+
83+
// Now process each line individually instead of loading everything into memory
84+
var removedCerts []removedCertificate
85+
for {
86+
record, err := csvReader.Read()
87+
if err != nil {
88+
break // End of file or other error
89+
}
90+
91+
removedCerts = append(removedCerts, removedCertificate{
92+
Fingerprint: record[fingerprint],
93+
Comments: record[comments],
94+
})
95+
}
96+
97+
// All CSV processing is done in the loop above, we already have the removedCerts slice
7098
return removedCerts, nil
7199
}
72100

0 commit comments

Comments
 (0)