Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

7.4 KB
Raw Blame
package events

import (
	"fmt"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestPathCleanerClean(t *testing.T) {
	tests := []struct {
		name  string
		rules string
		path  string
		want  string
	}{
		{
			name:  "id segment collapses to alias",
			rules: `[{"alias": "/classes/:id", "regex": "/classes/[^/]+"}]`,
			path:  "/classes/928q3hr9paw8hfe",
			want:  "/classes/:id",
		},
		{
			name:  "backreference expands the captured group",
			rules: `[{"alias": "/item/\\1", "regex": "/item/(\\d+)-.*"}]`,
			path:  "/item/42-blue-widget",
			want:  "/item/42",
		},
		{
			name:  "digit after a backreference stays literal, not group 10",
			rules: `[{"alias": "/v\\10/x", "regex": "/version/(\\d)"}]`,
			path:  "/version/7",
			want:  "/v70/x",
		},
		{
			name:  "dollar sign in alias stays literal",
			rules: `[{"alias": "/$price", "regex": "/cost/.*"}]`,
			path:  "/cost/123",
			want:  "/$price",
		},
		{
			name:  "escaped backslash in alias is one literal backslash",
			rules: `[{"alias": "/a\\\\b", "regex": "/raw/.*"}]`,
			path:  "/raw/x",
			want:  `/a\b`,
		},
		{
			name:  "rules chain in list order, each fed the previous output",
			rules: `[{"alias": "/classes/:id", "regex": "/classes/[^/]+"}, {"alias": "/c/:id", "regex": "/classes/:id"}]`,
			path:  "/classes/abc123",
			want:  "/c/:id",
		},
		{
			name:  "invalid regex is skipped, later rules still apply",
			rules: `[{"alias": "x", "regex": "("}, {"alias": "/classes/:id", "regex": "/classes/[^/]+"}]`,
			path:  "/classes/abc123",
			want:  "/classes/:id",
		},
		{
			name:  "malformed legacy entry is skipped, later rules still apply",
			rules: `["legacy", {"alias": "/users/:id", "regex": "/users/\\d+"}]`,
			path:  "/users/42",
			want:  "/users/:id",
		},
		{
			name:  "inline re2 flags work",
			rules: `[{"alias": "/docs", "regex": "(?i)/DOCS/.*"}]`,
			path:  "/docs/setup",
			want:  "/docs",
		},
		{
			name:  "missing alias replaces with empty string",
			rules: `[{"regex": "/classes/[^/]+"}]`,
			path:  "/classes/abc123",
			want:  "",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cleaner := NewPathCleanerFromJSON(tt.rules)
			require.NotNil(t, cleaner)
			assert.Equal(t, tt.want, cleaner.Clean(tt.path))
		})
	}
}

func TestNewPathCleanerFromJSONReturnsNilWhenNothingApplies(t *testing.T) {
	// Truncating past the cap would clean with a prefix of the ruleset and
	// group paths differently from the backfill query, so it must be all or
	// nothing: the frontend falls back to full client-side cleaning on nil.
	tooManyRules := "[" + strings.Repeat(`{"alias": "/x", "regex": "/x/.*"},`, maxPathCleaningRules) + `{"alias": "/x", "regex": "/x/.*"}]`
	for name, raw := range map[string]string{
		"empty string":        "",
		"whitespace":          "   ",
		"not json":            "not-json",
		"empty array":         "[]",
		"only invalid rules":  `[{"alias": "x", "regex": "("}, {"alias": "y", "regex": ""}]`,
		"more rules than cap": tooManyRules,
	} {
		t.Run(name, func(t *testing.T) {
			assert.Nil(t, NewPathCleanerFromJSON(raw))
		})
	}
}

func TestPathCleanerSkipsCaptureGroupBomb(t *testing.T) {
	// Sizing a replacement materializes indexes for every capture group per
	// match, so a rule stuffed with empty groups forces large allocations per
	// event. Such a rule is dropped at compile time; the valid rule still applies.
	bomb := strings.Repeat("()", maxPathCleaningCaptureGroups+1)
	rules := fmt.Sprintf(`[{"alias": "x", "regex": %q}, {"alias": "/b", "regex": "/a/.*"}]`, bomb)
	cleaner := NewPathCleanerFromJSON(rules)
	require.NotNil(t, cleaner)

	assert.Equal(t, "/b", cleaner.Clean("/a/123"))
}

func TestPathCleanerBoundsOutputAmplification(t *testing.T) {
	// A `(.) -> \1\1` rule doubles the path, and rules chain each fed the
	// previous output, so enough of them would grow a short path past any memory
	// limit. Cleaning must bail and return the raw path instead of amplifying
	// unbounded in the shared fan-out loop. Without the size cap this Clean call
	// allocates gigabytes and never returns.
	// 14 doublings of a 4-char path cross the ceiling at rule 12, so cleaning
	// bails there. Kept just past the bail point: if the cap regresses, the
	// fallback is a 64 KiB string that fails the assert cleanly, not a multi-
	// terabyte allocation that OOMs the runner.
	doubling := `{"alias": "\\1\\1", "regex": "(.)"}`
	rules := "[" + strings.Repeat(doubling+",", 13) + doubling + "]"
	cleaner := NewPathCleanerFromJSON(rules)
	require.NotNil(t, cleaner)

	path := "/abc"
	got := cleaner.Clean(path)
	assert.Equal(t, path, got, "amplifying ruleset must fall back to the raw path")
	assert.LessOrEqual(t, len(got), maxCleanedPathLen)
}

func TestPathCleanerBoundsSingleRuleAmplification(t *testing.T) {
	// One rule whose replacement repeats a full-input capture many times would, under a
	// plain ReplaceAllString, allocate a multi-megabyte string before any size check.
	// Cleaning must size the result up front and fall back to the raw path instead.
	alias := strings.Repeat(`\1`, 400)
	rules := fmt.Sprintf(`[{"alias": %q, "regex": "(.*)"}]`, alias)
	cleaner := NewPathCleanerFromJSON(rules)
	require.NotNil(t, cleaner)

	path := "/" + strings.Repeat("a", 4000)
	got := cleaner.Clean(path)
	assert.Equal(t, path, got, "amplifying single rule must fall back to the raw path")
	assert.LessOrEqual(t, len(got), maxCleanedPathLen)
}

func TestPathCleanerReturnsOversizedInputUnchanged(t *testing.T) {
	cleaner := NewPathCleanerFromJSON(`[{"alias": "/x", "regex": "/.*"}]`)
	require.NotNil(t, cleaner)

	path := "/" + strings.Repeat("a", maxCleanedPathLen)
	assert.Equal(t, path, cleaner.Clean(path), "a path over the ceiling must skip cleaning, not run rules on it")
}

func TestPathCleanerSkipsOverlongAlias(t *testing.T) {
	longAlias := strings.Repeat("x", maxPathCleaningAliasLen+1)
	// The over-long alias rule is dropped; the valid rule after it still applies.
	rules := fmt.Sprintf(`[{"alias": %q, "regex": "/a/.*"}, {"alias": "/b", "regex": "/a/.*"}]`, longAlias)
	cleaner := NewPathCleanerFromJSON(rules)
	require.NotNil(t, cleaner)

	assert.Equal(t, "/b", cleaner.Clean("/a/123"))
}

func TestConvertInjectsCleanedPathnameWithoutMutatingSharedEvent(t *testing.T) {
	cleaner := NewPathCleanerFromJSON(`[{"alias": "/classes/:id", "regex": "/classes/[^/]+"}]`)
	require.NotNil(t, cleaner)

	event := PostHogEvent{
		Uuid:       "uuid-1",
		DistinctId: "user-1",
		Event:      "$pageview",
		Properties: map[string]interface{}{"$pathname": "/classes/928q3hr9paw8hfe"},
	}

	// columns == nil normally aliases the shared event map; the cleaner must
	// copy so its per-subscriber property never leaks into other subscriptions.
	response := convertToResponsePostHogEvent(event, 1, nil, cleaner)
	assert.Equal(t, "/classes/:id", response.Properties["$virt_cleaned_pathname"])
	assert.Equal(t, "/classes/928q3hr9paw8hfe", response.Properties["$pathname"])
	assert.NotContains(t, event.Properties, "$virt_cleaned_pathname")

	// With a column allowlist the property is injected after filtering, so
	// subscribers don't need to request it explicitly.
	withColumns := convertToResponsePostHogEvent(event, 1, []string{"$pathname"}, cleaner)
	assert.Equal(t, "/classes/:id", withColumns.Properties["$virt_cleaned_pathname"])

	// No cleaner, no property.
	withoutCleaner := convertToResponsePostHogEvent(event, 1, nil, nil)
	assert.NotContains(t, withoutCleaner.Properties, "$virt_cleaned_pathname")
}