-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dbusx): 🐛 introspect a method before calling to santize arguments
- add an introspection type which can be used to perform D-Bus introspection - before calling a method, introspect it to retrieve its argument details - sanitize a methods arguments to ensure they are the correct D-Bus type
- Loading branch information
Showing
2 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2024 Joshua Rich <joshua.rich@gmail.com> | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
package dbusx | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/godbus/dbus/v5/introspect" | ||
) | ||
|
||
var ErrMethodNotFound = errors.New("method not found") | ||
|
||
type Introspection introspect.Node | ||
|
||
func (i Introspection) GetMethod(name string) (*Method, error) { | ||
for _, intr := range i.Interfaces { | ||
found := slices.IndexFunc(intr.Methods, func(e introspect.Method) bool { | ||
return strings.HasSuffix(name, e.Name) | ||
}) | ||
|
||
if found != -1 { | ||
return &Method{ | ||
name: name, | ||
intr: intr.Name, | ||
path: i.Name, | ||
obj: &intr.Methods[found], | ||
}, nil | ||
} | ||
} | ||
|
||
return nil, ErrMethodNotFound | ||
} | ||
|
||
func NewIntrospection(bus *Bus, intr, path string) (*Introspection, error) { | ||
obj := bus.getObject(intr, path) | ||
|
||
node, err := introspect.Call(obj) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to introspect: %w", err) | ||
} | ||
|
||
nodeObj := Introspection(*node) | ||
|
||
return &nodeObj, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters