Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

FilterGuard

FilterGuard is a Go package that validates whether required filter fields are present in SQL WHERE clauses. It's particularly useful for ensuring that queries contain necessary filters like group_id for multi-tenant applications or row-level security.

Features

  • Validates GORM queries to ensure required fields are present in WHERE clauses
  • Supports direct SQL string validation using SQL parser
  • Handles complex WHERE conditions including AND, OR, and parentheses
  • Fallback to regex-based parsing for SQLs that can't be parsed by the SQL parser

Installation

go get github.com/po3rin/filterguard

Usage

Basic Usage with GORM

package main

import (
    "fmt"
    "log"

    "github.com/po3rin/filterguard"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
)

type User struct {
    ID      uint   `gorm:"primarykey"`
    GroupID string
    Name    string
}

func main() {
    db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
    if err != nil {
        log.Fatal(err)
    }

    // Create a filter checker that requires 'group_id' field
    checker := filterguard.New("group_id")

    // Valid query - contains group_id filter
    validQuery := db.Where("group_id = ? AND name = ?", "group1", "Alice")
    if err := checker.CheckQuery(validQuery); err != nil {
        fmt.Printf("Error: %v\n", err)
    } else {
        fmt.Println("✓ Query is valid")
    }

    // Invalid query - missing group_id filter
    invalidQuery := db.Where("name = ?", "Alice")
    if err := checker.CheckQuery(invalidQuery); err != nil {
        fmt.Printf("Error: %v\n", err) // Will print error
    }
}

Direct SQL Validation

checker := filterguard.New("group_id")

// Valid SQL
sqlStr := "SELECT * FROM users WHERE group_id = 'group1' AND name = 'Alice'"
if err := checker.CheckSQL(sqlStr); err != nil {
    fmt.Printf("Error: %v\n", err)
} else {
    fmt.Println("✓ SQL is valid")
}

Multiple Required Fields

// Require both group_id and status fields
checker := filterguard.New("group_id", "status")

query := db.Where("group_id = ? AND status = ? AND name = ?", "group1", "active", "Alice")
if err := checker.CheckQuery(query); err != nil {
    fmt.Printf("Error: %v\n", err)
} else {
    fmt.Println("✓ Query contains both required fields")
}

API Reference

New(fields ...string) *FilterChecker

Creates a new FilterChecker instance with the specified required fields.

CheckQuery(db *gorm.DB) error

Validates that a GORM query contains all required fields in its WHERE clause. Returns an error if any required field is missing.

CheckSQL(sqlStr string) error

Validates that a SQL string contains all required fields in its WHERE clause. Returns an error if any required field is missing or if the SQL cannot be parsed.

Use Cases

  • Multi-tenant applications: Ensure all queries include group isolation filters
  • Row-level security: Validate that queries contain necessary security filters
  • Data compliance: Ensure queries follow data access policies
  • Query auditing: Validate queries before execution in middleware

Example

Run the example:

cd example
go run main.go

Testing

go test -v

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

⌂ 换网址