| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"regexp" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/go-git/go-git/v5/internal/pathutil" |
| 11 |
format "github.com/go-git/go-git/v5/plumbing/format/config" |
| 12 |
) |
| 13 |
|
| 14 |
var ( |
| 15 |
ErrModuleEmptyURL = errors.New("module config: empty URL") |
| 16 |
ErrModuleEmptyPath = errors.New("module config: empty path") |
| 17 |
ErrModuleBadPath = errors.New("submodule has an invalid path") |
| 18 |
ErrModuleBadName = errors.New("ignoring suspicious submodule name") |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
// Matches module paths with dotdot ".." components. |
| 23 |
dotdotPath = regexp.MustCompile(`(^|[/\\])\.\.([/\\]|$)`) |
| 24 |
) |
| 25 |
|
| 26 |
// Modules defines the submodules properties, represents a .gitmodules file |
| 27 |
// https://www.kernel.org/pub/software/scm/git/docs/gitmodules.html |
| 28 |
type Modules struct { |
| 29 |
// Submodules is a map of submodules being the key the name of the submodule. |
| 30 |
Submodules map[string]*Submodule |
| 31 |
|
| 32 |
raw *format.Config |
| 33 |
} |
| 34 |
|
| 35 |
// NewModules returns a new empty Modules |
| 36 |
func NewModules() *Modules { |
| 37 |
return &Modules{ |
| 38 |
Submodules: make(map[string]*Submodule), |
| 39 |
raw: format.New(), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
const ( |
| 44 |
pathKey = "path" |
| 45 |
branchKey = "branch" |
| 46 |
) |
| 47 |
|
| 48 |
// Unmarshal parses a git-config file and stores it. |
| 49 |
func (m *Modules) Unmarshal(b []byte) error { |
| 50 |
r := bytes.NewBuffer(b) |
| 51 |
d := format.NewDecoder(r) |
| 52 |
|
| 53 |
m.raw = format.New() |
| 54 |
if err := d.Decode(m.raw); err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
|
| 58 |
unmarshalSubmodules(m.raw, m.Submodules) |
| 59 |
return nil |
| 60 |
} |
| 61 |
|
| 62 |
// Marshal returns Modules encoded as a git-config file. |
| 63 |
func (m *Modules) Marshal() ([]byte, error) { |
| 64 |
s := m.raw.Section(submoduleSection) |
| 65 |
s.Subsections = make(format.Subsections, len(m.Submodules)) |
| 66 |
|
| 67 |
var i int |
| 68 |
for _, r := range m.Submodules { |
| 69 |
s.Subsections[i] = r.marshal() |
| 70 |
i++ |
| 71 |
} |
| 72 |
|
| 73 |
buf := bytes.NewBuffer(nil) |
| 74 |
if err := format.NewEncoder(buf).Encode(m.raw); err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
|
| 78 |
return buf.Bytes(), nil |
| 79 |
} |
| 80 |
|
| 81 |
// Submodule defines a submodule. |
| 82 |
type Submodule struct { |
| 83 |
// Name module name |
| 84 |
Name string |
| 85 |
// Path defines the path, relative to the top-level directory of the Git |
| 86 |
// working tree. |
| 87 |
Path string |
| 88 |
// URL defines a URL from which the submodule repository can be cloned. |
| 89 |
URL string |
| 90 |
// Branch is a remote branch name for tracking updates in the upstream |
| 91 |
// submodule. Optional value. |
| 92 |
Branch string |
| 93 |
|
| 94 |
// raw representation of the subsection, filled by marshal or unmarshal are |
| 95 |
// called. |
| 96 |
raw *format.Subsection |
| 97 |
} |
| 98 |
|
| 99 |
// Validate validates the fields and sets the default values. |
| 100 |
func (m *Submodule) Validate() error { |
| 101 |
if err := validSubmoduleName(m.Name); err != nil { |
| 102 |
return fmt.Errorf("%w: %q", ErrModuleBadName, m.Name) |
| 103 |
} |
| 104 |
|
| 105 |
if m.Path == "" { |
| 106 |
return ErrModuleEmptyPath |
| 107 |
} |
| 108 |
|
| 109 |
if m.URL == "" { |
| 110 |
return ErrModuleEmptyURL |
| 111 |
} |
| 112 |
|
| 113 |
if dotdotPath.MatchString(m.Path) { |
| 114 |
return ErrModuleBadPath |
| 115 |
} |
| 116 |
|
| 117 |
return nil |
| 118 |
} |
| 119 |
|
| 120 |
// validSubmoduleName mirrors canonical Git's check_submodule_name in |
| 121 |
// submodule-config.c [1]: reject empty names and any name with a ".." |
| 122 |
// path component, using both '/' and '\\' as separators so the rule |
| 123 |
// is consistent across platforms. The component check is delegated to |
| 124 |
// `pathutil.IsHFSDot` and `pathutil.IsNTFSDot` with `.` as the needle, |
| 125 |
// which both cover the bare ".." case and reject components that |
| 126 |
// resolve to ".." after HFS+ Unicode normalisation (ignored code |
| 127 |
// points, e.g. `.<U+200C>.`) or NTFS trailing-space/dot/ADS |
| 128 |
// canonicalisation (e.g. `.. `, `..::$INDEX_ALLOCATION`). |
| 129 |
// `.gitmodules` is attacker-controlled by definition, so both checks |
| 130 |
// run unconditionally regardless of host OS. |
| 131 |
// |
| 132 |
// The additional checks (bare ".", NUL byte, leading or trailing |
| 133 |
// separator, drive-letter prefix) close go-git-specific edge cases |
| 134 |
// the canonical loop does not exercise: canonical Git treats names |
| 135 |
// as opaque C strings, while Go strings carry NULs through and the |
| 136 |
// billy filesystem layer is path-aware in ways Git's working storage |
| 137 |
// is not. |
| 138 |
// |
| 139 |
// [1]: https://github.com/git/git/blob/v2.54.0/submodule-config.c#L214-L237 |
| 140 |
func validSubmoduleName(name string) error { |
| 141 |
if name == "" || name == "." { |
| 142 |
return ErrModuleBadName |
| 143 |
} |
| 144 |
for _, seg := range strings.FieldsFunc(name, isPathSep) { |
| 145 |
if pathutil.IsHFSDot(seg, ".") || pathutil.IsNTFSDot(seg, ".", "") { |
| 146 |
return ErrModuleBadName |
| 147 |
} |
| 148 |
} |
| 149 |
// go-git-specific defensive checks beyond canonical Git. |
| 150 |
if strings.ContainsRune(name, 0) { |
| 151 |
return ErrModuleBadName |
| 152 |
} |
| 153 |
if isPathSep(rune(name[0])) || isPathSep(rune(name[len(name)-1])) { |
| 154 |
return ErrModuleBadName |
| 155 |
} |
| 156 |
if len(name) >= 2 && name[1] == ':' { |
| 157 |
return ErrModuleBadName |
| 158 |
} |
| 159 |
return nil |
| 160 |
} |
| 161 |
|
| 162 |
func isPathSep(r rune) bool { return r == '/' || r == '\\' } |
| 163 |
|
| 164 |
func (m *Submodule) unmarshal(s *format.Subsection) { |
| 165 |
m.raw = s |
| 166 |
|
| 167 |
m.Name = m.raw.Name |
| 168 |
m.Path = m.raw.Option(pathKey) |
| 169 |
m.URL = m.raw.Option(urlKey) |
| 170 |
m.Branch = m.raw.Option(branchKey) |
| 171 |
} |
| 172 |
|
| 173 |
func (m *Submodule) marshal() *format.Subsection { |
| 174 |
if m.raw == nil { |
| 175 |
m.raw = &format.Subsection{} |
| 176 |
} |
| 177 |
|
| 178 |
m.raw.Name = m.Name |
| 179 |
if m.raw.Name == "" { |
| 180 |
m.raw.Name = m.Path |
| 181 |
} |
| 182 |
|
| 183 |
m.raw.SetOption(pathKey, m.Path) |
| 184 |
m.raw.SetOption(urlKey, m.URL) |
| 185 |
|
| 186 |
if m.Branch != "" { |
| 187 |
m.raw.SetOption(branchKey, m.Branch) |
| 188 |
} |
| 189 |
|
| 190 |
return m.raw |
| 191 |
} |