Skip to content

Commit d8f530c

Browse files
authored
fix: config validation (#527)
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent ff32212 commit d8f530c

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

internal/config/config.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package config
1717
import (
1818
"errors"
1919
"fmt"
20+
"net/url"
2021
"os"
2122
"runtime"
2223

@@ -138,6 +139,10 @@ func Load(configFile string) (*Config, error) {
138139
if err := globalConfig.validateProfile(); err != nil {
139140
return nil, err
140141
}
142+
// Validate submit config
143+
if err := globalConfig.validateSubmit(); err != nil {
144+
return nil, err
145+
}
141146
// Populate our Indexer startup
142147
if err := globalConfig.populateIndexer(); err != nil {
143148
return nil, err
@@ -173,6 +178,26 @@ func (c *Config) validateProfile() error {
173178
return nil
174179
}
175180

181+
func (c *Config) validateSubmit() error {
182+
if c.Submit.Url == "" {
183+
return nil
184+
}
185+
u, err := url.Parse(c.Submit.Url)
186+
if err != nil {
187+
return fmt.Errorf("invalid submit URL %q: %w", c.Submit.Url, err)
188+
}
189+
if u.Scheme != "http" && u.Scheme != "https" {
190+
return fmt.Errorf(
191+
"invalid submit URL scheme %q: must be http or https",
192+
u.Scheme,
193+
)
194+
}
195+
if u.Host == "" {
196+
return fmt.Errorf("invalid submit URL %q: missing host", c.Submit.Url)
197+
}
198+
return nil
199+
}
200+
176201
func (c *Config) populateIndexer() error {
177202
profile, ok := Profiles[c.Network][c.Profile]
178203
if !ok {

internal/tx/tx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ func submitTxApi(txRawBytes []byte) (string, error) {
539539
req.Header.Add("project_id", cfg.Submit.BlockFrostProjectID)
540540
}
541541
client := &http.Client{Timeout: 5 * time.Minute}
542-
resp, err := client.Do(req) //nolint:gosec
542+
resp, err := client.Do(req) //nolint:gosec // URL is validated at config load time
543543
if err != nil {
544544
return "", fmt.Errorf(
545545
"failed to send request: %s: %w",
@@ -553,12 +553,12 @@ func submitTxApi(txRawBytes []byte) (string, error) {
553553
cfg.Submit.Url,
554554
)
555555
}
556+
defer resp.Body.Close()
556557
// We have to read the entire response body and close it to prevent a memory leak
557558
respBody, err := io.ReadAll(resp.Body)
558559
if err != nil {
559560
return "", fmt.Errorf("failed to read response body: %w", err)
560561
}
561-
defer resp.Body.Close()
562562

563563
if resp.StatusCode == http.StatusAccepted {
564564
return string(respBody), nil

internal/wallet/wallet.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ func Setup() {
5454
}
5555
// Write seed.txt
5656
// WARNING: this will clobber existing files
57-
f, err := os.Create(seedPath)
57+
f, err := os.OpenFile(seedPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
5858
if err != nil {
5959
panic(err)
6060
}
6161
l, err := f.WriteString(mnemonic)
62-
slog.Debug( //nolint:gosec
63-
"wrote seed file",
64-
"bytes", l,
65-
)
6662
if err != nil {
6763
f.Close()
6864
panic(err)
6965
}
66+
slog.Debug( //nolint:gosec
67+
"wrote seed file",
68+
"bytes", l,
69+
)
7070
err = f.Close()
7171
if err != nil {
7272
panic(err)

0 commit comments

Comments
 (0)