Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package config
import (
"errors"
"fmt"
"net/url"
"os"
"runtime"

Expand Down Expand Up @@ -138,6 +139,10 @@ func Load(configFile string) (*Config, error) {
if err := globalConfig.validateProfile(); err != nil {
return nil, err
}
// Validate submit config
if err := globalConfig.validateSubmit(); err != nil {
return nil, err
}
// Populate our Indexer startup
if err := globalConfig.populateIndexer(); err != nil {
return nil, err
Expand Down Expand Up @@ -173,6 +178,26 @@ func (c *Config) validateProfile() error {
return nil
}

func (c *Config) validateSubmit() error {
if c.Submit.Url == "" {
return nil
}
u, err := url.Parse(c.Submit.Url)
if err != nil {
return fmt.Errorf("invalid submit URL %q: %w", c.Submit.Url, err)
}
if u.Scheme != "http" && u.Scheme != "https" {
return fmt.Errorf(
"invalid submit URL scheme %q: must be http or https",
u.Scheme,
)
}
if u.Host == "" {
return fmt.Errorf("invalid submit URL %q: missing host", c.Submit.Url)
}
return nil
}

func (c *Config) populateIndexer() error {
profile, ok := Profiles[c.Network][c.Profile]
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func submitTxApi(txRawBytes []byte) (string, error) {
req.Header.Add("project_id", cfg.Submit.BlockFrostProjectID)
}
client := &http.Client{Timeout: 5 * time.Minute}
resp, err := client.Do(req) //nolint:gosec
resp, err := client.Do(req) //nolint:gosec // URL is validated at config load time
if err != nil {
return "", fmt.Errorf(
"failed to send request: %s: %w",
Expand All @@ -553,12 +553,12 @@ func submitTxApi(txRawBytes []byte) (string, error) {
cfg.Submit.Url,
)
}
defer resp.Body.Close()
// We have to read the entire response body and close it to prevent a memory leak
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusAccepted {
return string(respBody), nil
Expand Down
10 changes: 5 additions & 5 deletions internal/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ func Setup() {
}
// Write seed.txt
// WARNING: this will clobber existing files
f, err := os.Create(seedPath)
f, err := os.OpenFile(seedPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
panic(err)
}
l, err := f.WriteString(mnemonic)
slog.Debug( //nolint:gosec
"wrote seed file",
"bytes", l,
)
if err != nil {
f.Close()
panic(err)
}
slog.Debug( //nolint:gosec
"wrote seed file",
"bytes", l,
)
err = f.Close()
if err != nil {
panic(err)
Expand Down