Skip to content

Commit c1c1695

Browse files
committed
Merge pull request #25 from jqln-0/master
Validate ObjectPath before use in Conn.GetUnitProperties
2 parents c57eef5 + 06f3a2b commit c1c1695

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

dbus/methods.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package dbus
1818

1919
import (
20+
"errors"
2021
"github.com/guelfey/go.dbus"
2122
)
2223

@@ -145,6 +146,9 @@ func (c *Conn) GetUnitProperties(unit string) (map[string]interface{}, error) {
145146
var props map[string]dbus.Variant
146147

147148
path := ObjectPath("/org/freedesktop/systemd1/unit/" + unit)
149+
if !path.IsValid() {
150+
return nil, errors.New("invalid unit name: " + unit)
151+
}
148152

149153
obj := c.sysconn.Object("org.freedesktop.systemd1", path)
150154
err = obj.Call("org.freedesktop.DBus.Properties.GetAll", 0, "org.freedesktop.systemd1.Unit").Store(&props)

dbus/methods_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,18 @@ func TestGetUnitProperties(t *testing.T) {
138138
t.Fatal("unexpected wants for /")
139139
}
140140
}
141+
142+
// TestGetUnitPropertiesRejectsInvalidName attempts to get the properties for a
143+
// unit with an invalid name. This test should be run with --test.timeout set,
144+
// as a fail will manifest as GetUnitProperties hanging indefinitely.
145+
func TestGetUnitPropertiesRejectsInvalidName(t *testing.T) {
146+
conn := setupConn(t)
147+
148+
unit := "//invalid#$^/"
149+
150+
_, err := conn.GetUnitProperties(unit)
151+
152+
if err == nil {
153+
t.Fatal("Expected an error, got nil")
154+
}
155+
}

dbus/set_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
// TestBasicSetActions asserts that Add & Remove behavior is correct
88
func TestBasicSetActions(t *testing.T) {
9-
s := set{}
9+
s := newSet()
1010

1111
if s.Contains("foo") {
1212
t.Fatal("set should not contain 'foo'")

0 commit comments

Comments
 (0)