jsoncompare

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

File: internal/jsoncompare/interface.go

File: internal/jsoncompare/service.go

File: internal/jsoncompare/types.go

Index

Constants

View Source
const (
	PlaceholderDynamicKey   = "__DYNAMIC_KEY__"
	PlaceholderDynamicValue = "__DYNAMIC_VALUE__"
)

Placeholders used during normalization.

Variables

This section is empty.

Functions

This section is empty.

Types

type ComparisonResult

type ComparisonResult struct {
	// AreEquivalent indicates if the inputs are considered equal based on the comparison rules.
	AreEquivalent bool
	// Diff provides details if the content differs.
	Diff string
	// IsJSON indicates if the comparison was successfully performed on JSON structures.
	IsJSON bool
	// NormalizedA and NormalizedB are included for enhanced debuggability (only populated if IsJSON is true).
	NormalizedA interface{}
	NormalizedB interface{}
}

ComparisonResult holds the outcome of the comparison and a detailed diff.

type HeuristicRules

type HeuristicRules struct {
	// KeyPatterns identifies map keys that are likely dynamic (e.g., "session_id").
	KeyPatterns []*regexp.Regexp
	// CheckValueForUUID enables detection of UUIDs in string values.
	CheckValueForUUID bool
	// CheckValueForTimestamp enables detection of timestamps (strings or numbers).
	CheckValueForTimestamp bool
	// TimestampFormats defines the layouts to try when parsing string timestamps.
	TimestampFormats []string
	// CheckValueForHighEntropy enables detection of high-entropy strings (e.g., tokens).
	CheckValueForHighEntropy bool
	// EntropyThreshold defines the minimum Shannon entropy to classify a string as dynamic.
	EntropyThreshold float64
}

HeuristicRules defines the configurable set of rules for identifying dynamic data based on patterns and types. Migrated from internal/analysis/auth/idor/types.go and internal/jsoncompare/heuristics.go

func DefaultRules

func DefaultRules() HeuristicRules

DefaultRules provides a sensible default configuration for common dynamic data.

func (HeuristicRules) DeepCopy

func (h HeuristicRules) DeepCopy() HeuristicRules

DeepCopy creates a concurrency-safe copy of the HeuristicRules.

type JSONComparison

type JSONComparison interface {
	// Compare performs a semantic comparison of two byte arrays using the service's default options.
	// It handles both JSON and non-JSON content gracefully.
	Compare(bodyA, bodyB []byte) (*ComparisonResult, error)

	// CompareWithOptions performs a semantic comparison using the specified options.
	CompareWithOptions(bodyA, bodyB []byte, opts Options) (*ComparisonResult, error)
}

JSONComparison defines the interface for the JSON comparison service.

func NewService

func NewService() JSONComparison

NewService creates a new instance of the JSON comparison service.

type Options

type Options struct {
	// Rules define how dynamic data (Timestamps, UUIDs, etc.) is identified and normalized.
	Rules HeuristicRules

	// IgnoreArrayOrder enables order-agnostic comparison of arrays.
	IgnoreArrayOrder bool
	// EquateEmpty treats nil (JSON null) and empty slices/maps ({}, []) as equal.
	EquateEmpty bool

	// SpecificValuesToIgnore forces normalization of specific string or numeric values.
	SpecificValuesToIgnore map[string]struct{}
	// NormalizeAllValuesForStructure normalizes all primitive values (leaf nodes)
	// to focus purely on the structural equivalence (keys, nesting).
	NormalizeAllValuesForStructure bool
}

Options allows customization of both normalization heuristics and comparison behavior.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns a standard configuration suitable for most API comparisons.

func (Options) DeepCopy

func (o Options) DeepCopy() Options

DeepCopy creates a concurrency-safe copy of the Options. This is crucial when modifying options in concurrent analyzers (like IDOR).

Jump to

Keyboard shortcuts

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