Skip to content

Commit a2bed78

Browse files
committed
feat: prevent attacks even without HTTP requests
Signed-off-by: Kohei Morita <[email protected]>
1 parent e0841a5 commit a2bed78

File tree

9 files changed

+29
-10
lines changed

9 files changed

+29
-10
lines changed

exporter/exporter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ type eventExporterProvider struct {
1515
ep EventExporter
1616
}
1717

18+
// EventExporter exports WAF detection events to any desired location.
1819
type EventExporter interface {
20+
// Export transforms and transmits event data to any desired location.
1921
Export(ctx context.Context, event waf.ReadOnlyDetectionEvents) error
2022
}
2123

internal/emitter/account_takeover/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func IsSuspiciousLoginActivity(
3737
) error {
3838
parent, _ := operation.FindOperationFromContext(ctx)
3939
if parent == nil {
40-
return nil
40+
parent = operation.NewOperation(nil)
4141
}
4242

4343
var wafop *waf.WafOperation

internal/emitter/http/client_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var _ http.RoundTripper = &Transport{}
4444
func ProtectRoundTrip(ctx context.Context, url string) error {
4545
parent, _ := operation.FindOperationFromContext(ctx)
4646
if parent == nil {
47-
return nil
47+
parent = operation.NewOperation(nil)
4848
}
4949

5050
var wafop *waf.WafOperation

internal/emitter/http/client_handler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func TestWrapClient(t *testing.T) {
3535
},
3636
"when not through http operation": {
3737
ctx: context.Background(),
38-
url: "https://example.com",
39-
expectErr: false,
38+
url: "http://169.254.169.254",
39+
expectErr: true,
4040
},
4141
}
4242

internal/emitter/os/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (r *FileOperationResult) IsBlock() bool {
3232
func ProtectFileOperation(ctx context.Context, path string) error {
3333
parent, _ := operation.FindOperationFromContext(ctx)
3434
if parent == nil {
35-
return nil
35+
parent = operation.NewOperation(nil)
3636
}
3737

3838
var wafop *waf.WafOperation

internal/emitter/os/handler_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99
"github.com/sitebatch/waffle-go/internal/emitter/os"
1010
"github.com/sitebatch/waffle-go/waf"
1111
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestProtectFileOperation(t *testing.T) {
1516
t.Parallel()
1617

17-
waffle.Start()
18+
require.NoError(t, waffle.Start())
1819

1920
testCases := map[string]struct {
2021
ctx context.Context
@@ -36,6 +37,11 @@ func TestProtectFileOperation(t *testing.T) {
3637
filePath: "file.txt",
3738
expectErr: false,
3839
},
40+
"not through http operation and attack request": {
41+
ctx: context.Background(),
42+
filePath: "/var/run/secrets/kubernetes.io/serviceaccount/token",
43+
expectErr: true,
44+
},
3945
}
4046

4147
for name, tt := range testCases {

internal/emitter/sql/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (r *SQLOperationResult) IsBlock() bool {
3232
func ProtectSQLOperation(ctx context.Context, query string) error {
3333
parent, _ := operation.FindOperationFromContext(ctx)
3434
if parent == nil {
35-
return nil
35+
parent = operation.NewOperation(nil)
3636
}
3737

3838
var wafop *waf.WafOperation

internal/emitter/sql/handler_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99
"github.com/sitebatch/waffle-go/internal/emitter/sql"
1010
"github.com/sitebatch/waffle-go/waf"
1111
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestProtectSQLOperation(t *testing.T) {
1516
t.Parallel()
1617

17-
waffle.Start()
18+
require.NoError(t, waffle.Start())
1819

1920
testCases := map[string]struct {
2021
ctx context.Context
@@ -36,11 +37,14 @@ func TestProtectSQLOperation(t *testing.T) {
3637
query: "SELECT * FROM users",
3738
expectErr: false,
3839
},
40+
"not through http operation and attack request": {
41+
ctx: context.Background(),
42+
query: "SELECT * FROM users WHERE id = '1' OR 1=1--",
43+
expectErr: true,
44+
},
3945
}
4046

4147
for name, tt := range testCases {
42-
tt := tt
43-
4448
t.Run(name, func(t *testing.T) {
4549
t.Parallel()
4650

waffle.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func WithRule(ruleJSON []byte) Options {
4646
}
4747
}
4848

49+
// Start initializes and starts Waffle with the provided options.
4950
func Start(opts ...Options) error {
5051
response.InitResponseWriterFeature()
5152

@@ -90,6 +91,12 @@ func SetErrorHandler(h handler.ErrorHandler) {
9091
handler.SetErrorHandler(h)
9192
}
9293

94+
// SetExporter sets a exporter of WAF detection event.
95+
//
96+
// Waffle can export WAF detection events to any desired location using the provided exporter.
97+
// By default, Waffle uses a no-operation exporter that does not export any events.
98+
// You can implement your own exporter by implementing the exporter.EventExporter interface
99+
// and set it using this function.
93100
func SetExporter(eventExporter exporter.EventExporter) {
94101
exporter.SetExporter(eventExporter)
95102
}

0 commit comments

Comments
 (0)