Skip to content

Commit 0f2fc7e

Browse files
Add Input Validation to CLI Commands Based on Server Request Models (#17)
* feat: add comprehensive input validation to CLI commands - Add validation utilities for Ethereum addresses, private keys, amounts, URLs, device IDs - Integrate validation into auth, stake, balance, LLM, federated learning, storage, and reputation commands - Add validation for task parameters, model names, prompts, dataset CIDs, and FL session parameters - Improve error handling with descriptive validation error messages - Add comprehensive test coverage for all validation functions * fix: update file permission constants to octal notation in federated_learning.go and ethereum_service.go
1 parent c673798 commit 0f2fc7e

14 files changed

Lines changed: 344 additions & 24 deletions

File tree

.golangci-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ linters-settings:
3939
gofmt:
4040
simplify: true
4141
goimports:
42-
local-prefixes: "github.com/theblitlabs/parity-runner"
42+
local-prefixes: "github.com/theblitlabs/parity-client"

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ linters-settings:
3535
gofmt:
3636
simplify: true
3737
goimports:
38-
local-prefixes: "github.com/theblitlabs/parity-runner"
38+
local-prefixes: "github.com/theblitlabs/parity-client"
3939

4040
version: "2"

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ fmt: ## Format code using gofumpt (preferred) or gofmt
9393
imports: ## Fix imports formatting and add missing imports
9494
@echo "Organizing imports..."
9595
@if command -v $(GOIMPORTS) >/dev/null 2>&1; then \
96-
$(GOIMPORTS) -w -local github.com/theblitlabs/parity-runner .; \
96+
$(GOIMPORTS) -w -local github.com/theblitlabs/parity-client .; \
9797
else \
9898
echo "goimports not found. Installing..."; \
9999
go install golang.org/x/tools/cmd/goimports@latest; \
100-
$(GOIMPORTS) -w -local github.com/theblitlabs/parity-runner .; \
100+
$(GOIMPORTS) -w -local github.com/theblitlabs/parity-client .; \
101101
fi
102102

103103
format: fmt imports ## Run all formatters (gofumpt + goimports)

cmd/cli/auth.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/theblitlabs/parity-client/internal/adapters/keystore"
1313
"github.com/theblitlabs/parity-client/internal/adapters/wallet"
1414
"github.com/theblitlabs/parity-client/internal/config"
15+
"github.com/theblitlabs/parity-client/internal/utils"
1516
)
1617

1718
func RunAuth(cmd *cobra.Command, args []string) {
@@ -28,8 +29,8 @@ func RunAuth(cmd *cobra.Command, args []string) {
2829
func ExecuteAuth(privateKey string, configPath string) error {
2930
log := log.With().Str("component", "auth").Logger()
3031

31-
if privateKey == "" {
32-
return fmt.Errorf("private key is required")
32+
if err := utils.ValidatePrivateKey(privateKey); err != nil {
33+
return err
3334
}
3435

3536
configManager := config.NewConfigManager(configPath)
@@ -40,16 +41,12 @@ func ExecuteAuth(privateKey string, configPath string) error {
4041

4142
privateKey = strings.TrimPrefix(privateKey, "0x")
4243

43-
if len(privateKey) != 64 {
44-
return fmt.Errorf("invalid private key - must be 64 hex characters without 0x prefix")
45-
}
46-
4744
_, err = crypto.HexToECDSA(privateKey)
4845
if err != nil {
4946
return fmt.Errorf("invalid private key format: %w", err)
5047
}
5148

52-
keystoreAdapter, err := keystore.NewAdapter(nil) // Uses default config
49+
keystoreAdapter, err := keystore.NewAdapter(nil)
5350
if err != nil {
5451
return fmt.Errorf("failed to create keystore: %w", err)
5552
}

cmd/cli/stake.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ func RunStake(cmd *cobra.Command, args []string) {
2626
amount, _ := cmd.Flags().GetFloat64("amount")
2727
configPath, _ := cmd.Flags().GetString("config-path")
2828

29+
if err := utils.ValidateAmount(amount, 0.000001); err != nil {
30+
log.Fatal().Err(err).Msg("Invalid stake amount")
31+
return
32+
}
33+
2934
log.Info().
3035
Float64("amount", amount).
3136
Msg("Processing stake request")

internal/commands/federated_learning.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ var createSessionCmd = &cobra.Command{
5454
}
5555

5656
// Validate required fields
57-
if name == "" {
58-
return fmt.Errorf("--name is required")
57+
if err := utils.ValidateFLSessionName(name); err != nil {
58+
return err
5959
}
6060
if modelType == "" {
6161
return fmt.Errorf("--model-type is required")
6262
}
63-
if datasetCID == "" {
64-
return fmt.Errorf("--dataset-cid is required")
63+
if err := utils.ValidateDatasetCID(datasetCID); err != nil {
64+
return err
6565
}
6666

6767
// Get optional flags
@@ -79,6 +79,33 @@ var createSessionCmd = &cobra.Command{
7979
noiseMultiplier, _ := cmd.Flags().GetFloat64("noise-multiplier")
8080
l2NormClip, _ := cmd.Flags().GetFloat64("l2-norm-clip")
8181

82+
// Validate optional parameters
83+
if totalRounds > 0 {
84+
if err := utils.ValidateTotalRounds(totalRounds); err != nil {
85+
return err
86+
}
87+
}
88+
if minParticipants > 0 {
89+
if err := utils.ValidateMinParticipants(minParticipants); err != nil {
90+
return err
91+
}
92+
}
93+
if learningRate > 0 {
94+
if err := utils.ValidateLearningRate(learningRate); err != nil {
95+
return err
96+
}
97+
}
98+
if batchSize > 0 {
99+
if err := utils.ValidateBatchSize(batchSize); err != nil {
100+
return err
101+
}
102+
}
103+
if localEpochs > 0 {
104+
if err := utils.ValidateLocalEpochs(localEpochs); err != nil {
105+
return err
106+
}
107+
}
108+
82109
// Get data partitioning parameters
83110
alpha, _ := cmd.Flags().GetFloat64("alpha")
84111
minSamples, _ := cmd.Flags().GetInt("min-samples")
@@ -732,7 +759,7 @@ func saveModelToFile(model map[string]interface{}, outputFile, format string) er
732759
return fmt.Errorf("failed to marshal model data: %w", err)
733760
}
734761

735-
if err := os.WriteFile(outputFile, data, 0644); err != nil {
762+
if err := os.WriteFile(outputFile, data, 0o644); err != nil {
736763
return fmt.Errorf("failed to write to file: %w", err)
737764
}
738765

internal/commands/llm.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ var submitCmd = &cobra.Command{
4141
wait, _ := cmd.Flags().GetBool("wait")
4242
timeout, _ := cmd.Flags().GetDuration("timeout")
4343

44-
if model == "" {
45-
return fmt.Errorf("model is required")
44+
if err := utils.ValidateModelName(model); err != nil {
45+
return err
4646
}
47-
if prompt == "" {
48-
return fmt.Errorf("prompt is required")
47+
if err := utils.ValidatePrompt(prompt); err != nil {
48+
return err
4949
}
5050

5151
configPath, _ := cmd.Flags().GetString("config-path")

internal/commands/reputation.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/spf13/cobra"
1212
"github.com/theblitlabs/deviceid"
1313
"github.com/theblitlabs/parity-client/internal/config"
14+
"github.com/theblitlabs/parity-client/internal/utils"
1415
)
1516

1617
type RunnerStatus struct {
@@ -96,6 +97,9 @@ ban status, and overall network standing. This is the primary command for networ
9697
var runnerID string
9798
if len(args) > 0 {
9899
runnerID = args[0]
100+
if err := utils.ValidateDeviceID(runnerID); err != nil {
101+
return err
102+
}
99103
} else {
100104
deviceIDManager := deviceid.NewManager(deviceid.Config{})
101105
runnerID, err = deviceIDManager.VerifyDeviceID()
@@ -151,6 +155,9 @@ and current standing in the network. Shows eligibility, ban status, and quality
151155
var runnerID string
152156
if len(args) > 0 {
153157
runnerID = args[0]
158+
if err := utils.ValidateDeviceID(runnerID); err != nil {
159+
return err
160+
}
154161
} else {
155162
deviceIDManager := deviceid.NewManager(deviceid.Config{})
156163
runnerID, err = deviceIDManager.VerifyDeviceID()

internal/commands/storage.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ var uploadFileCmd = &cobra.Command{
4040
log.Fatal().Str("file", filePath).Msg("File does not exist")
4141
}
4242

43+
// Validate file path
44+
if filePath == "" {
45+
log.Fatal().Msg("File path is required")
46+
}
47+
4348
configPath := utils.GetDefaultConfigPath()
4449
configManager := config.NewConfigManager(configPath)
4550
cfg, err := configManager.GetConfig()
@@ -117,6 +122,11 @@ var uploadDirectoryCmd = &cobra.Command{
117122
log.Fatal().Str("directory", dirPath).Msg("Directory does not exist")
118123
}
119124

125+
// Validate directory path
126+
if dirPath == "" {
127+
log.Fatal().Msg("Directory path is required")
128+
}
129+
120130
configPath := utils.GetDefaultConfigPath()
121131
configManager := config.NewConfigManager(configPath)
122132
cfg, err := configManager.GetConfig()
@@ -177,6 +187,16 @@ var downloadFileCmd = &cobra.Command{
177187
cid := args[0]
178188
outputPath := args[1]
179189

190+
// Validate CID
191+
if err := utils.ValidateDatasetCID(cid); err != nil {
192+
log.Fatal().Err(err).Msg("Invalid CID")
193+
}
194+
195+
// Validate output path
196+
if outputPath == "" {
197+
log.Fatal().Msg("Output path is required")
198+
}
199+
180200
configPath := utils.GetDefaultConfigPath()
181201
configManager := config.NewConfigManager(configPath)
182202
cfg, err := configManager.GetConfig()

internal/storage/ethereum_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (f *BlockchainService) DownloadFile(ctx context.Context, cid string, output
154154
}()
155155

156156
// Create output directory if it doesn't exist
157-
if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil {
157+
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
158158
return fmt.Errorf("failed to create output directory: %w", err)
159159
}
160160

0 commit comments

Comments
 (0)