scan

package
v1.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package scan provides content scanning middleware for Trove. It scans uploaded content for threats using a pluggable Provider interface. When a threat is detected, the upload is blocked with trove.ErrContentBlocked.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClamAVOption

type ClamAVOption func(*ClamAVProvider)

ClamAVOption configures the ClamAV provider.

func WithTimeout

func WithTimeout(d time.Duration) ClamAVOption

WithTimeout sets the scan timeout for ClamAV connections.

type ClamAVProvider

type ClamAVProvider struct {
	// contains filtered or unexported fields
}

ClamAVProvider implements Provider using the ClamAV INSTREAM protocol. It connects to a ClamAV daemon over TCP and streams content for scanning.

func NewClamAVProvider

func NewClamAVProvider(addr string, opts ...ClamAVOption) *ClamAVProvider

NewClamAVProvider creates a ClamAV scan provider. The addr is the ClamAV daemon address (e.g., "localhost:3310").

func (*ClamAVProvider) Scan

func (p *ClamAVProvider) Scan(ctx context.Context, r io.Reader) (*Result, error)

Scan sends content to ClamAV using the INSTREAM protocol and returns the scan result.

INSTREAM protocol:

  1. Send "zINSTREAM\x00"
  2. For each chunk: send 4-byte big-endian length + data
  3. Send 4-byte zero to signal end of stream
  4. Read response: "stream: OK\x00" or "stream: <THREAT> FOUND\x00"

type OnDetectFunc

type OnDetectFunc func(ctx context.Context, key string, result *Result)

OnDetectFunc is a callback invoked when a threat is detected.

type Option

type Option func(*Scan)

Option configures the Scan middleware.

func WithMaxSize

func WithMaxSize(size int64) Option

WithMaxSize sets the maximum size of content to scan. Content larger than this is passed through without scanning. Default: 25MB.

func WithOnDetect

func WithOnDetect(fn OnDetectFunc) Option

WithOnDetect sets a callback for when threats are detected.

func WithProvider

func WithProvider(p Provider) Option

WithProvider sets the scan provider.

func WithSkipExtensions

func WithSkipExtensions(exts ...string) Option

WithSkipExtensions sets file extensions to skip scanning.

type Provider

type Provider interface {
	// Scan inspects the content and returns a result indicating whether
	// the content is clean or contains a threat.
	Scan(ctx context.Context, r io.Reader) (*Result, error)
}

Provider defines the interface for content scanning backends.

type Result

type Result struct {
	// Clean is true if no threats were found.
	Clean bool

	// Threat is the name/identifier of the detected threat (empty if clean).
	Threat string

	// Details provides additional information about the scan.
	Details string
}

Result holds the outcome of a content scan.

type Scan

type Scan struct {
	// contains filtered or unexported fields
}

Scan provides content scanning middleware for uploads.

func New

func New(opts ...Option) *Scan

New creates a new scan middleware with the given options.

func (*Scan) Direction

func (s *Scan) Direction() middleware.Direction

Direction returns Write since scanning only applies to uploads.

func (*Scan) Name

func (s *Scan) Name() string

Name returns the middleware identifier.

func (*Scan) WrapWriter

func (s *Scan) WrapWriter(ctx context.Context, w io.WriteCloser, key string) (io.WriteCloser, error)

WrapWriter wraps a writer to scan content before it reaches the driver. Data is buffered, scanned on Close(), and blocked with ErrContentBlocked if a threat is detected.

Each scan records a "scan object" client span (a scanner such as ClamAV is a separate service) with the result and the size scanned, and counts trove_scans_total on the Recorder of the Trove that runs the middleware. The span carries neither the key nor the threat name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL