1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +02:00

internal/types/blacklist.go: implement wildcard for contracts

This commit is contained in:
Henrik Hautakoski 2024-12-04 15:14:10 +01:00
parent 120c2acdc6
commit eb2032e233
2 changed files with 29 additions and 2 deletions

View file

@ -31,8 +31,18 @@ func (bl *Blacklist) Add(contract string, action string) {
bl.table[contract] = append(bl.table[contract], action)
}
func (bl Blacklist) list(contracts ...string) [][]string {
ret := [][]string{}
for _, contract := range contracts {
if v, ok := bl.table[contract]; ok {
ret = append(ret, v)
}
}
return ret
}
func (bl Blacklist) IsAllowed(contract string, action string) bool {
if v, ok := bl.table[contract]; ok {
for _, v := range bl.list(contract, "*") {
for _, act := range v {
if act == action || act == "*" {
return bl.isWhitelist == true

View file

@ -52,20 +52,32 @@ func TestBlacklist_IsAllowed(t *testing.T) {
func TestBlacklist_IsAllowedWildcard(t *testing.T) {
bl := Blacklist{
table: map[string][]string{
"mycontract": {"*"},
"mycontract": {"*"},
"*": {"action1", "action2"},
"evilcontract": {"evilaction"},
},
}
require.False(t, bl.IsAllowed("mycontract", "myaction"))
require.False(t, bl.IsAllowed("mycontract", "noop"))
require.False(t, bl.IsAllowed("mycontract", "xxx"))
// Wildcard contract
require.False(t, bl.IsAllowed("somecontract", "action1"))
require.False(t, bl.IsAllowed("someothercontract", "action1"))
require.False(t, bl.IsAllowed("randomcontract", "action2"))
require.False(t, bl.IsAllowed("evilcontract", "action2"))
require.False(t, bl.IsAllowed("evilcontract", "evilaction"))
require.True(t, bl.IsAllowed("xxx", "yyy"))
require.True(t, bl.IsAllowed("evilcontract", "alloweaction"))
}
func TestBlacklist_Whitelist(t *testing.T) {
bl := Blacklist{
table: map[string][]string{
"mycontract": {"myaction", "noop"},
"*": {"goodaction1", "goodaction2"},
},
}
@ -73,6 +85,11 @@ func TestBlacklist_Whitelist(t *testing.T) {
require.True(t, bl.IsAllowed("mycontract", "myaction"))
require.True(t, bl.IsAllowed("mycontract", "noop"))
// Wildcard contract
require.True(t, bl.IsAllowed("mycontract", "goodaction1"))
require.True(t, bl.IsAllowed("someothercontract", "goodaction2"))
require.False(t, bl.IsAllowed("mycontract", "xxx"))
require.False(t, bl.IsAllowed("xxx", "yyy"))
}