Skip to content

Commit 451ca40

Browse files
committed
Check gpg signature for library_index.json
1 parent 2cdc095 commit 451ca40

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

Diff for: arduino/libraries/librariesmanager/download.go

+3
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ var LibraryIndexURL, _ = url.Parse("https://downloads.arduino.cc/libraries/libra
2424

2525
// LibraryIndexGZURL is the URL where to get the gzipped library index.
2626
var LibraryIndexGZURL, _ = url.Parse("https://downloads.arduino.cc/libraries/library_index.json.gz")
27+
28+
// LibraryIndexSignature is the URL where to get the library index signature.
29+
var LibraryIndexSignature, _ = url.Parse("https://downloads.arduino.cc/libraries/library_index.json.sig")

Diff for: arduino/libraries/librariesmanager/librariesmanager.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ type LibrariesManager struct {
3535
LibrariesDir []*LibrariesDir
3636
Libraries map[string]*LibraryAlternatives `json:"libraries"`
3737

38-
Index *librariesindex.Index
39-
IndexFile *paths.Path
40-
DownloadsDir *paths.Path
38+
Index *librariesindex.Index
39+
IndexFile *paths.Path
40+
IndexFileSignature *paths.Path
41+
DownloadsDir *paths.Path
4142
}
4243

4344
// LibrariesDir is a directory containing libraries
@@ -95,15 +96,17 @@ func (lm LibrariesManager) Names() []string {
9596

9697
// NewLibraryManager creates a new library manager
9798
func NewLibraryManager(indexDir *paths.Path, downloadsDir *paths.Path) *LibrariesManager {
98-
var indexFile *paths.Path
99+
var indexFile, indexFileSignature *paths.Path
99100
if indexDir != nil {
100101
indexFile = indexDir.Join("library_index.json")
102+
indexFileSignature = indexDir.Join("library_index.json.sig")
101103
}
102104
return &LibrariesManager{
103-
Libraries: map[string]*LibraryAlternatives{},
104-
IndexFile: indexFile,
105-
DownloadsDir: downloadsDir,
106-
Index: librariesindex.EmptyIndex,
105+
Libraries: map[string]*LibraryAlternatives{},
106+
IndexFile: indexFile,
107+
IndexFileSignature: indexFileSignature,
108+
DownloadsDir: downloadsDir,
109+
Index: librariesindex.EmptyIndex,
107110
}
108111
}
109112

Diff for: commands/instances.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,40 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
201201
}
202202
}
203203

204+
// Download signature
205+
tmpSignature := tmp.Join("library_index.json.sig")
206+
if d, err := downloader.DownloadWithConfig(tmpSignature.String(), librariesmanager.LibraryIndexSignature.String(), *config, downloader.NoResume); err != nil {
207+
return err
208+
} else {
209+
if err := Download(d, "Updating index: library_index.json.sig", downloadCB); err != nil {
210+
return errors.Wrap(err, "downloading library_index.json.sig")
211+
}
212+
}
213+
204214
// Extract the real library_index
205215
tmpIndex := tmp.Join("library_index.json")
206216
if err := paths.GUnzip(tmpIndexGz, tmpIndex); err != nil {
207217
return errors.Wrap(err, "unzipping library_index.json.gz")
208218
}
209219

210-
// Copy extracted library_index to final destination
220+
// Check signature
221+
if ok, _, err := security.VerifyArduinoDetachedSignature(tmpIndex, tmpSignature); err != nil {
222+
return errors.Wrap(err, "verifying signature")
223+
} else if !ok {
224+
return errors.New("library_index.json has an invalid signature!")
225+
}
226+
227+
// Copy extracted library_index and signature to final destination
211228
lm.IndexFile.Remove()
229+
lm.IndexFileSignature.Remove()
212230
if err := tmpIndex.CopyTo(lm.IndexFile); err != nil {
213231
return errors.Wrap(err, "writing library_index.json")
214232
}
233+
if err := tmpSignature.CopyTo(lm.IndexFileSignature); err != nil {
234+
return errors.Wrap(err, "writing library_index.json.sig")
235+
}
215236

237+
// Rescan libraries
216238
if _, err := Rescan(req.GetInstance().GetId()); err != nil {
217239
return fmt.Errorf("rescanning filesystem: %s", err)
218240
}

0 commit comments

Comments
 (0)