|
1 | 1 | package perms |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "regexp" |
5 | 6 | "strings" |
6 | 7 | "sync" |
@@ -36,6 +37,13 @@ type Manager struct { |
36 | 37 | // available LND sub-server permissions. |
37 | 38 | perms map[string][]bakery.Op |
38 | 39 |
|
| 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 | + |
39 | 47 | mu sync.RWMutex |
40 | 48 | } |
41 | 49 |
|
@@ -95,19 +103,48 @@ func NewManager(withAllSubServers bool) (*Manager, error) { |
95 | 103 | } |
96 | 104 |
|
97 | 105 | 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{}), |
101 | 110 | }, nil |
102 | 111 | } |
103 | 112 |
|
| 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 | + |
104 | 136 | // IsWhiteListedURL returns true if the given URL has been whitelisted meaning |
105 | 137 | // 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. |
107 | 140 | func (pm *Manager) IsWhiteListedURL(url string) bool { |
108 | 141 | pm.mu.Lock() |
109 | 142 | defer pm.mu.Unlock() |
110 | 143 |
|
| 144 | + if _, ok := pm.forcedWhiteListPerms[url]; ok { |
| 145 | + return true |
| 146 | + } |
| 147 | + |
111 | 148 | ops, ok := pm.perms[url] |
112 | 149 |
|
113 | 150 | return ok && len(ops) == 0 |
|
0 commit comments