Skip to content

Commit 5430e6d

Browse files
committed
perms: add a ForceWhiteListURL
Which will result in a specific URL returning `true` for `IsWhiteListedURL` while still returning the original permissions in `URIPermissions`. This will be used for situations where we want to explicitly handle the verification of a call to a URL in a code path that happens after path that hits `IsWhiteListed`.
1 parent db94d55 commit 5430e6d

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

perms/manager.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package perms
22

33
import (
4+
"fmt"
45
"regexp"
56
"strings"
67
"sync"
@@ -36,6 +37,13 @@ type Manager struct {
3637
// available LND sub-server permissions.
3738
perms map[string][]bakery.Op
3839

40+
// forcedWhiteListPerms holds a set of URIs that should be considered
41+
// white listed even if they do have associated required permissions.
42+
// IsWhiteListedURL will return true for any of these URIs but
43+
// URIPermissions will continue to return the real permissions of the
44+
// URI.
45+
forcedWhiteListPerms map[string]struct{}
46+
3947
mu sync.RWMutex
4048
}
4149

@@ -95,19 +103,48 @@ func NewManager(withAllSubServers bool) (*Manager, error) {
95103
}
96104

97105
return &Manager{
98-
lndSubServerPerms: lndSubServerPerms,
99-
fixedPerms: permissions,
100-
perms: allPerms,
106+
lndSubServerPerms: lndSubServerPerms,
107+
fixedPerms: permissions,
108+
perms: allPerms,
109+
forcedWhiteListPerms: make(map[string]struct{}),
101110
}, nil
102111
}
103112

113+
// ForceWhiteListURL will whitelist the given URL resulting in
114+
// IsWhiteListedURL returning true for this URL in future calls. It will return
115+
// an error if this URL is unknown to the Manager. URIPermissions will continue
116+
// to return the real set of permissions for the URL.
117+
//
118+
// NOTE: URLs should be whitelisted with caution. This should only be done if
119+
// the caller will explicitly handle the verification of a URL in a location
120+
// other than where the usual permission verification is done (ie, in a code
121+
// path other than the one calling IsWhiteListedURL.
122+
func (pm *Manager) ForceWhiteListURL(url string) error {
123+
pm.mu.Lock()
124+
defer pm.mu.Unlock()
125+
126+
_, ok := pm.perms[url]
127+
if !ok {
128+
return fmt.Errorf("only known URLs can be white listed")
129+
}
130+
131+
pm.forcedWhiteListPerms[url] = struct{}{}
132+
133+
return nil
134+
}
135+
104136
// IsWhiteListedURL returns true if the given URL has been whitelisted meaning
105137
// that it does not require a macaroon for validation. A URL is considered
106-
// white-listed if it has no operations associated with a URL.
138+
// white-listed if it has no operations associated with a URL or if it has
139+
// been explicitly whitelisted.
107140
func (pm *Manager) IsWhiteListedURL(url string) bool {
108141
pm.mu.Lock()
109142
defer pm.mu.Unlock()
110143

144+
if _, ok := pm.forcedWhiteListPerms[url]; ok {
145+
return true
146+
}
147+
111148
ops, ok := pm.perms[url]
112149

113150
return ok && len(ops) == 0

0 commit comments

Comments
 (0)