Skip to content

Commit 4b2b049

Browse files
committed
packp/capability: Skip argument validations for unknown capabilities
Fixes src-d#623
1 parent b345ed7 commit 4b2b049

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

plumbing/protocol/packp/capability/capability.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ const (
234234

235235
const DefaultAgent = "go-git/4.x"
236236

237-
var valid = map[Capability]bool{
237+
var known = map[Capability]bool{
238238
MultiACK: true, MultiACKDetailed: true, NoDone: true, ThinPack: true,
239239
Sideband: true, Sideband64k: true, OFSDelta: true, Agent: true,
240240
Shallow: true, DeepenSince: true, DeepenNot: true, DeepenRelative: true,

plumbing/protocol/packp/capability/list.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,27 @@ func (l *List) Add(c Capability, values ...string) error {
108108
return nil
109109
}
110110

111-
if !multipleArgument[c] && len(l.m[c].Values) > 0 {
111+
if known[c] && !multipleArgument[c] && len(l.m[c].Values) > 0 {
112112
return ErrMultipleArguments
113113
}
114114

115115
l.m[c].Values = append(l.m[c].Values, values...)
116116
return nil
117117
}
118118

119+
func (l *List) validateNoEmptyArgs(values []string) error {
120+
for _, v := range values {
121+
if v == "" {
122+
return ErrEmtpyArgument
123+
}
124+
}
125+
return nil
126+
}
127+
119128
func (l *List) validate(c Capability, values []string) error {
129+
if !known[c] {
130+
return l.validateNoEmptyArgs(values)
131+
}
120132
if requiresArgument[c] && len(values) == 0 {
121133
return ErrArgumentsRequired
122134
}
@@ -128,14 +140,7 @@ func (l *List) validate(c Capability, values []string) error {
128140
if !multipleArgument[c] && len(values) > 1 {
129141
return ErrMultipleArguments
130142
}
131-
132-
for _, v := range values {
133-
if v == "" {
134-
return ErrEmtpyArgument
135-
}
136-
}
137-
138-
return nil
143+
return l.validateNoEmptyArgs(values)
139144
}
140145

141146
// Supports returns true if capability is present

plumbing/protocol/packp/capability/list_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ func (s *SuiteCapabilities) TestDecodeWithUnknownCapability(c *check.C) {
6565
c.Assert(cap.Supports(Capability("foo")), check.Equals, true)
6666
}
6767

68+
func (s *SuiteCapabilities) TestDecodeWithUnknownCapabilityWithArgument(c *check.C) {
69+
cap := NewList()
70+
err := cap.Decode([]byte("oldref=HEAD:refs/heads/v2 thin-pack"))
71+
c.Assert(err, check.IsNil)
72+
73+
c.Assert(cap.m, check.HasLen, 2)
74+
c.Assert(cap.Get("oldref"), check.DeepEquals, []string{"HEAD:refs/heads/v2"})
75+
c.Assert(cap.Get(ThinPack), check.IsNil)
76+
}
77+
78+
func (s *SuiteCapabilities) TestDecodeWithUnknownCapabilityWithMultipleArgument(c *check.C) {
79+
cap := NewList()
80+
err := cap.Decode([]byte("foo=HEAD:refs/heads/v2 foo=HEAD:refs/heads/v1 thin-pack"))
81+
c.Assert(err, check.IsNil)
82+
83+
c.Assert(cap.m, check.HasLen, 2)
84+
c.Assert(cap.Get("foo"), check.DeepEquals, []string{"HEAD:refs/heads/v2", "HEAD:refs/heads/v1"})
85+
c.Assert(cap.Get(ThinPack), check.IsNil)
86+
}
87+
6888
func (s *SuiteCapabilities) TestString(c *check.C) {
6989
cap := NewList()
7090
cap.Set(Agent, "bar")
@@ -153,7 +173,7 @@ func (s *SuiteCapabilities) TestAddErrArgumentsNotAllowed(c *check.C) {
153173
c.Assert(err, check.Equals, ErrArguments)
154174
}
155175

156-
func (s *SuiteCapabilities) TestAddErrArgumendts(c *check.C) {
176+
func (s *SuiteCapabilities) TestAddErrArguments(c *check.C) {
157177
cap := NewList()
158178
err := cap.Add(SymRef, "")
159179
c.Assert(err, check.Equals, ErrEmtpyArgument)

0 commit comments

Comments
 (0)