-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect_file.go
44 lines (35 loc) · 1.1 KB
/
inspect_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
//go:build !tinygo && !coraza.disabled_operators.inspectFile
// +build !tinygo,!coraza.disabled_operators.inspectFile
package operators
import (
"context"
"os/exec"
"time"
"github.com/appsentinels/coraza/v3/experimental/plugins/plugintypes"
)
type inspectFile struct {
path string
}
var _ plugintypes.Operator = (*inspectFile)(nil)
func newInspectFile(options plugintypes.OperatorOptions) (plugintypes.Operator, error) {
return &inspectFile{path: options.Arguments}, nil
}
func (o *inspectFile) Evaluate(tx plugintypes.TransactionState, value string) bool {
// TODO parametrize timeout
// TODO add relative path capabilities
// TODO add lua special support
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Add /bin/bash to context?
cmd := exec.CommandContext(ctx, o.path, value)
_, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded || err != nil {
return false
}
return true
}
func init() {
Register("inspectFile", newInspectFile)
}