Skip to content

Commit e18d218

Browse files
authored
[build]: restrict ssh option to default and return error (#91)
1 parent 267b5ab commit e18d218

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

pkg/build/buildopts.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,21 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
241241
return args, nil
242242
}
243243

244-
sshExtract := func(key string) []sshprovider.AgentConfig {
244+
sshExtract := func(key string) ([]sshprovider.AgentConfig, error) {
245245
values, ok := contextMap[key]
246246
if !ok {
247-
return nil
247+
return nil, nil
248248
}
249+
// Only --ssh default is supported for now, so all other cases are rejected.
250+
if len(values) != 1 {
251+
return nil, ErrUnsupportedSSH
252+
}
253+
254+
value := strings.TrimSpace(values[0])
255+
if value != "default" {
256+
return nil, ErrUnsupportedSSH
257+
}
258+
249259
agentConfigs := make([]sshprovider.AgentConfig, 0, len(values))
250260
for _, value := range values {
251261
id, path, hasPath := strings.Cut(value, "=")
@@ -267,7 +277,7 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
267277
}
268278
agentConfigs = append(agentConfigs, config)
269279
}
270-
return agentConfigs
280+
return agentConfigs, nil
271281
}
272282

273283
labels := mapExtract(KeyLabels)
@@ -276,7 +286,10 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
276286
if err != nil {
277287
return nil, err
278288
}
279-
ssh := sshExtract(KeySSH)
289+
ssh, err := sshExtract(KeySSH)
290+
if err != nil {
291+
return nil, err
292+
}
280293
cacheIn := contextMap[KeyCacheIn]
281294
cacheOut := contextMap[KeyCacheOut]
282295
outputs := contextMap[KeyOutput]

pkg/build/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ var (
2828
ErrNoBuildDirectives = fmt.Errorf("no build directives")
2929
ErrInvalidImageContextFormat = fmt.Errorf("image resolver: image name format is invalid")
3030
ErrInvalidProgress = fmt.Errorf("build arg progress value is invalid")
31+
ErrUnsupportedSSH = fmt.Errorf("build arg ssh value is not supported")
3132
)

0 commit comments

Comments
 (0)