Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions examples/request-block/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,54 @@ func TestSetDomainNameAndGetMatchConfig(t *testing.T) {
})
}

func TestDomainMatchASCIICaseBlocksRequest(t *testing.T) {
test.RunTest(t, func(t *testing.T) {
tests := []struct {
name string
configuredDomain string
requestDomain string
}{
{name: "exact", configuredDomain: "blocked.example.com", requestDomain: "blocked.example.com"},
{name: "configured uppercase", configuredDomain: "BLOCKED.EXAMPLE.COM", requestDomain: "blocked.example.com"},
{name: "configured lowercase", configuredDomain: "blocked.example.com", requestDomain: "BLOCKED.EXAMPLE.COM"},
{name: "mixed case", configuredDomain: "BlOcKeD.eXaMpLe.CoM", requestDomain: "bLoCkEd.ExAmPlE.cOm"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config, err := json.Marshal(map[string]interface{}{
"_rules_": []map[string]interface{}{
{
"_match_domain_": []string{tt.configuredDomain},
"blocked_code": 403,
"blocked_message": "domain blocked",
"case_sensitive": true,
"block_urls": []string{"unused"},
"block_exact_urls": []string{"/blocked"},
},
},
})
require.NoError(t, err)
host, status := test.NewTestHost(config)
defer host.Reset()
require.Equal(t, types.OnPluginStartStatusOK, status)

action := host.CallOnHttpRequestHeaders([][2]string{
{":authority", tt.requestDomain},
{":path", "/blocked"},
})

require.Equal(t, types.HeaderStopIteration, action)
localResponse := host.GetLocalResponse()
require.NotNil(t, localResponse)
require.Equal(t, uint32(403), localResponse.StatusCode)
require.Equal(t, "domain blocked", string(localResponse.Data))
host.CompleteHttp()
})
}
})
}

func TestBlockUrlByKeyword(t *testing.T) {
test.RunTest(t, func(t *testing.T) {
host, status := test.NewTestHost(testConfig)
Expand Down
36 changes: 33 additions & 3 deletions pkg/matcher/rule_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,20 +378,50 @@ func stripPortFromHost(reqHost string) string {
return reqHost
}

func equalFoldASCII(s1, s2 string) bool {
if s1 == s2 {
return true
}
if len(s1) != len(s2) {
return false
}
for index := 0; index < len(s1); index++ {
char1, char2 := s1[index], s2[index]
if 'A' <= char1 && char1 <= 'Z' {
char1 += 'a' - 'A'
}
if 'A' <= char2 && char2 <= 'Z' {
char2 += 'a' - 'A'
}
if char1 != char2 {
return false
}
}
return true
}

func hasPrefixFoldASCII(s, prefix string) bool {
return len(s) >= len(prefix) && equalFoldASCII(s[:len(prefix)], prefix)
}

func hasSuffixFoldASCII(s, suffix string) bool {
return len(s) >= len(suffix) && equalFoldASCII(s[len(s)-len(suffix):], suffix)
}

func (m RuleMatcher[PluginConfig]) hostMatch(rule RuleConfig[PluginConfig], reqHost string) bool {
reqHost = stripPortFromHost(reqHost)
for _, hostMatch := range rule.hosts {
switch hostMatch.matchType {
case Suffix:
if strings.HasSuffix(reqHost, hostMatch.host) {
if hasSuffixFoldASCII(reqHost, hostMatch.host) {
return true
}
case Prefix:
if strings.HasPrefix(reqHost, hostMatch.host) {
if hasPrefixFoldASCII(reqHost, hostMatch.host) {
return true
}
case Exact:
if reqHost == hostMatch.host {
if equalFoldASCII(reqHost, hostMatch.host) {
return true
}
default:
Expand Down
51 changes: 51 additions & 0 deletions pkg/matcher/rule_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,47 @@ func TestHostMatch(t *testing.T) {
}
}

func TestHostMatchASCIICase(t *testing.T) {
tests := []struct {
name string
matchType MatchType
configured string
requestHost string
want bool
}{
{name: "exact identical", matchType: Exact, configured: "www.example.com", requestHost: "www.example.com", want: true},
{name: "exact configured uppercase", matchType: Exact, configured: "WWW.EXAMPLE.COM", requestHost: "www.example.com", want: true},
{name: "exact configured lowercase", matchType: Exact, configured: "www.example.com", requestHost: "WWW.EXAMPLE.COM", want: true},
{name: "exact mixed case", matchType: Exact, configured: "WwW.ExAmPlE.CoM", requestHost: "wWw.eXaMpLe.cOm", want: true},
{name: "prefix configured uppercase", matchType: Prefix, configured: "WWW.", requestHost: "www.example.com", want: true},
{name: "prefix configured lowercase", matchType: Prefix, configured: "www.", requestHost: "WWW.EXAMPLE.COM", want: true},
{name: "prefix mixed case", matchType: Prefix, configured: "WwW.ExAm", requestHost: "wWw.eXaMpLe.cOm", want: true},
{name: "suffix configured uppercase", matchType: Suffix, configured: ".EXAMPLE.COM", requestHost: "www.example.com", want: true},
{name: "suffix configured lowercase", matchType: Suffix, configured: ".example.com", requestHost: "WWW.EXAMPLE.COM", want: true},
{name: "suffix mixed case", matchType: Suffix, configured: ".ExAmPlE.CoM", requestHost: "www.eXaMpLe.cOm", want: true},
{name: "exact port", matchType: Exact, configured: "WWW.EXAMPLE.COM", requestHost: "www.example.com:8080", want: true},
{name: "prefix port", matchType: Prefix, configured: "WWW.", requestHost: "www.example.com:8080", want: true},
{name: "suffix port", matchType: Suffix, configured: ".EXAMPLE.COM", requestHost: "www.example.com:8080", want: true},
{name: "empty suffix", matchType: Suffix, configured: "", requestHost: "MiXeD.Example.COM", want: true},
{name: "non ASCII identical", matchType: Exact, configured: "bücher.example", requestHost: "bücher.example", want: true},
{name: "non ASCII not folded", matchType: Exact, configured: "bücher.example", requestHost: "BÜCHER.EXAMPLE", want: false},
{name: "ASCII around non ASCII folded", matchType: Exact, configured: "Exämple.COM", requestHost: "exämple.com", want: true},
{name: "IPv6 hexadecimal case", matchType: Exact, configured: "[2001:db8::1]", requestHost: "[2001:DB8::1]", want: true},
{name: "IPv6 hexadecimal case with port", matchType: Exact, configured: "[2001:db8::1]", requestHost: "[2001:DB8::1]:8443", want: true},
{name: "IPv6 text not normalized", matchType: Exact, configured: "[2001:0db8::1]", requestHost: "[2001:DB8::1]", want: false},
{name: "exact trailing dot not added", matchType: Exact, configured: "Example.com", requestHost: "example.com.", want: false},
{name: "suffix trailing dot not added", matchType: Suffix, configured: ".Example.com", requestHost: "www.example.com.", want: false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rule := RuleConfig[customConfig]{hosts: []HostMatcher{{matchType: tt.matchType, host: tt.configured}}}
var matcher RuleMatcher[customConfig]
assert.Equal(t, tt.want, matcher.hostMatch(rule, tt.requestHost))
})
}
}

func TestServiceMatch(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -245,6 +286,16 @@ func TestServiceMatch(t *testing.T) {
service: "outbound|443||qwen.dns",
result: false,
},
{
name: "case remains significant",
config: RuleConfig[customConfig]{
services: map[string]struct{}{
"qwen.dns": {},
},
},
service: "outbound|443||QWEN.DNS",
result: false,
},
{
name: "error config format",
config: RuleConfig[customConfig]{
Expand Down
55 changes: 55 additions & 0 deletions pkg/wrapper/plugin_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package wrapper

import (
"fmt"
"testing"

"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/proxytest"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)

type bodyDeliveryHarness struct {
Expand Down Expand Up @@ -262,3 +264,56 @@ func TestRequestBodyLegacySkips(t *testing.T) {
require.Equal(t, 1, h.requestBodyCalls)
})
}

func TestDomainMatchASCIICaseInvokesHandler(t *testing.T) {
type domainRuleConfig struct {
marker string
}

tests := []struct {
name string
configuredDomain string
requestDomain string
expectedCallbacks int
}{
{name: "exact", configuredDomain: "example.com", requestDomain: "example.com", expectedCallbacks: 1},
{name: "configured uppercase", configuredDomain: "EXAMPLE.COM", requestDomain: "example.com", expectedCallbacks: 1},
{name: "configured lowercase", configuredDomain: "example.com", requestDomain: "EXAMPLE.COM", expectedCallbacks: 1},
{name: "mixed case", configuredDomain: "ExAmPlE.CoM", requestDomain: "eXaMpLe.cOm", expectedCallbacks: 1},
{name: "unmatched", configuredDomain: "example.com", requestDomain: "other.com", expectedCallbacks: 0},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
callbackCalls := 0
vm := NewCommonVmCtx[domainRuleConfig](
"domain-match-test",
ParseConfig(func(json gjson.Result, config *domainRuleConfig) error {
config.marker = json.Get("marker").String()
return nil
}),
ProcessRequestHeaders(func(_ HttpContext, config domainRuleConfig) types.Action {
callbackCalls++
require.Equal(t, "domain-rule", config.marker)
return types.ActionContinue
}),
)
configuration := fmt.Sprintf(`{"_rules_":[{"_match_domain_":[%q],"marker":"domain-rule"}]}`, tt.configuredDomain)
host, reset := proxytest.NewHostEmulator(proxytest.NewEmulatorOption().
WithVMContext(vm).
WithPluginConfiguration([]byte(configuration)))
t.Cleanup(reset)
host.RegisterForeignFunction("get_log_level", func([]byte) []byte { return make([]byte, 4) })
require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())
contextID := host.InitializeHttpContext()

action := host.CallOnRequestHeaders(contextID, [][2]string{
{":authority", tt.requestDomain},
{":path", "/"},
}, true)

require.Equal(t, types.ActionContinue, action)
require.Equal(t, tt.expectedCallbacks, callbackCalls)
})
}
}
Loading