|
1 | 1 | # waffle-go |
2 | 2 |
|
3 | | -Waffle is a library that provides in-app WAF (Web Application Firewall) and RASP (Runtime Application Self Protection) capabilities for your Go web applications. |
| 3 | +Waffle is a library for integrating a Web Application Firewall (WAF) into Go applications. |
4 | 4 |
|
5 | | -## Features: |
| 5 | +By embedding the WAF directly within the application rather than at the network boundary, you can achieve more accurate and flexible detection and defense against attacks. |
6 | 6 |
|
7 | | -- Adapts to your application stack without configuration |
8 | | -- Protects against common attacks like injection, XSS, and account takeover |
9 | | -- Context-aware precise detection |
| 7 | +## Features |
| 8 | + |
| 9 | +- Integration with minimal code changes |
| 10 | +- Protection against common web attacks including XSS, SQL injection, and SSRF |
| 11 | +- Protection against business logic vulnerabilities like Account Takeover |
| 12 | +- Support for popular Go web frameworks and libraries |
| 13 | + |
| 14 | +## Use Cases |
| 15 | + |
| 16 | +- Protecting web applications and APIs from common web attacks |
| 17 | +- Alternative to traditional network-based WAFs for application-level protection |
| 18 | +- Enhanced security for applications using database access and file operations |
10 | 19 |
|
11 | 20 | ## Getting Started |
12 | 21 |
|
13 | | -You can find a getting started guide on [waffle website](https://sitebatch.github.io/waffle-website/). |
| 22 | +First, set up the Waffle library. |
| 23 | + |
| 24 | +```bash |
| 25 | +go get github.com/sitebatch/waffle-go |
| 26 | +``` |
| 27 | + |
| 28 | +```go |
| 29 | +package main |
| 30 | + |
| 31 | +import ( |
| 32 | + "net/http" |
| 33 | + "github.com/sitebatch/waffle-go" |
| 34 | +) |
| 35 | + |
| 36 | +func main() { |
| 37 | + // Start Waffle |
| 38 | + if err := waffle.Start(); err != nil { |
| 39 | + // handle error |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +Finally, depending on which libraries your application uses, install the Waffle contrib package and apply the middleware or wrapper function. |
| 45 | +The following libraries are supported: |
| 46 | + |
| 47 | +| Library | Contrib Package | |
| 48 | +| :----------- | :------------------------------------------------------------- | |
| 49 | +| Gin | [contrib/gin-gonic/gin](contrib/gin-gonic/gin/README.md) | |
| 50 | +| Echo | [contrib/labstack/echo](contrib/labstack/echo/README.md) | |
| 51 | +| net/http | [contrib/net/http](contrib/net/http/README.md) | |
| 52 | +| gqlgen | [contrib/99designs/gqlgen](contrib/99designs/gqlgen/README.md) | |
| 53 | +| database/sql | [contrib/database/sql](contrib/database/sql/README.md) | |
| 54 | +| os | [contrib/os](contrib/os/README.md) | |
| 55 | + |
| 56 | +## Configuration |
| 57 | + |
| 58 | +### Custom Rules |
| 59 | + |
| 60 | +You can provide custom WAF rules: |
| 61 | + |
| 62 | +```go |
| 63 | +waffle.Start(waffle.WithRule(customRuleJSON)) |
| 64 | +``` |
| 65 | + |
| 66 | +### Error Handling |
| 67 | + |
| 68 | +Set a custom handler to handle Waffle's internal errors. |
| 69 | + |
| 70 | +```go |
| 71 | +waffle.SetErrorHandler(customErrorHandler) |
| 72 | +``` |
| 73 | + |
| 74 | +### Event Export |
| 75 | + |
| 76 | +To retrieve events detected by Waffle, configure an exporter using `SetExporter()`. |
| 77 | + |
| 78 | +Waffle provides built-in exporters like `StdoutExporter` for logging detection events and `ChanExporter` for writing to a specified channel, but you can also implement and configure your own custom exporter that meets the required interface. |
| 79 | + |
| 80 | +```go |
| 81 | +waffle.SetExporter(customExporter) |
| 82 | +``` |
| 83 | + |
| 84 | +### Logging |
| 85 | + |
| 86 | +Set a custom logger to capture Waffle's internal logs. |
| 87 | + |
| 88 | +```go |
| 89 | +waffle.SetLogger(logger) |
| 90 | +``` |
| 91 | + |
| 92 | +## Handling blocking event |
| 93 | + |
| 94 | +When Waffle detects an attack and blocks the request, it returns a `waf.SecurityBlockingError` error type. If you catch this error, you should handle it appropriately—for example, by returning a proper error response to the client. |
| 95 | +This error type can be checked using the `waf.IsSecurityBlockingError` function. |
| 96 | + |
| 97 | +When Waffle's HTTP middleware blocks a request, it automatically returns an HTTP 403 Forbidden response, but it is your responsibility to handle the blocked function call. |
| 98 | +For instance, if a function called during processing at an endpoint attempts to execute a potentially vulnerable SQL query (such as SQL Injection), that function call will be blocked and terminated by returning an error of type `waf.SecurityBlockingError`. |
| 99 | +You can determine whether the block was initiated by the WAF using either `errors.As` or `waf.IsSecurityBlockingError`. |
| 100 | + |
| 101 | +```go |
| 102 | +// Example of handling a blocked SQL query |
| 103 | + |
| 104 | +// Will be blocked due to SQL Injection attempt |
| 105 | +userInput := "1 OR 1 = 1" |
| 106 | +_, err := db.QueryContext(ctx, fmt.Sprintf("SELECT * FROM users WHERE id = '%s'", userInput)) |
| 107 | +if err != nil { |
| 108 | + if waf.IsSecurityBlockingError(err) { |
| 109 | + // Handle blocked request |
| 110 | + log.Printf("Blocked request: %v", err) |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + // Handle other errors |
| 115 | + log.Fatal(err) |
| 116 | +} |
| 117 | +``` |
0 commit comments