-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathshell.go
More file actions
83 lines (67 loc) · 2.32 KB
/
shell.go
File metadata and controls
83 lines (67 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package parse
import (
"encoding/json"
"strings"
"github.com/moby/buildkit/frontend/dockerfile/parser"
)
func parseSHELL(node *parser.Node) []Rule {
if node.Next == nil {
return []Rule{invalidInstructionRule(node, "SHELL requires at least one argument")}
}
// ERROR CHECKS - Return immediately on first error
// Extract shell configuration
shellConfig := extractSHELLConfig(node)
// Validate that config is not empty
if strings.TrimSpace(shellConfig) == "" {
return []Rule{NewErrorRule(node, "ShellMissingConfig",
"SHELL instruction must specify a shell configuration",
"https://docs.docker.com/reference/dockerfile/#shell")}
}
// SHELL instruction MUST be in JSON form
if !strings.HasPrefix(strings.TrimSpace(shellConfig), "[") {
return []Rule{NewErrorRule(node, "ShellRequiresJsonForm",
"SHELL instruction must be written in JSON form (e.g., SHELL [\"executable\", \"parameters\"])",
"https://docs.docker.com/reference/dockerfile/#shell")}
}
// Validate JSON form
if !checkSHELLJsonForm(shellConfig) {
return []Rule{NewErrorRule(node, "ShellInvalidJsonForm",
"SHELL instruction must be a valid JSON array with double quotes",
"https://docs.docker.com/reference/dockerfile/#shell")}
}
// No warnings in this file
return nil
}
// extractSHELLConfig extracts the shell configuration from the SHELL instruction
func extractSHELLConfig(node *parser.Node) string {
if node.Next == nil {
return ""
}
// Use the Original field and strip the instruction keyword
// This preserves the exact format including key=value pairs
config := strings.TrimPrefix(node.Original, node.Value)
return strings.TrimSpace(config)
}
// checkSHELLJsonForm validates that the SHELL instruction is valid JSON array
func checkSHELLJsonForm(config string) bool {
config = strings.TrimSpace(config)
// Must start with '[' and end with ']'
if !strings.HasPrefix(config, "[") || !strings.HasSuffix(config, "]") {
return false
}
// Try to parse as JSON array
var arr []string
if err := json.Unmarshal([]byte(config), &arr); err != nil {
return false
}
// Array should not be empty
if len(arr) == 0 {
return false
}
// Check that original string uses double quotes, not single quotes
// Single quotes are not valid JSON
if strings.Contains(config, "['") || strings.Contains(config, "']") {
return false
}
return true
}