diff --git a/filter/render.go b/filter/render.go index ba4422c92..018e5206f 100644 --- a/filter/render.go +++ b/filter/render.go @@ -2,6 +2,8 @@ package filter import ( "fmt" + "regexp" + "strconv" "strings" "github.com/pkg/errors" @@ -16,9 +18,7 @@ type renderer struct { } type renderResult struct { - sql string - trivial bool - unsatisfiable bool + sql string } func newRenderer(schema Schema, opts RenderOptions) *renderer { @@ -30,6 +30,17 @@ func newRenderer(schema Schema, opts RenderOptions) *renderer { } func (r *renderer) Render(cond Condition) (Statement, error) { + // Fold boolean constants first: a subtree that folds away must never reach + // the renderer, or its arguments would outlive its discarded SQL and shift + // every later positional bind. + cond = foldConstants(cond) + if c, ok := cond.(*ConstantCondition); ok { + if c.Value { + return Statement{SQL: "", Args: []any{}}, nil + } + return Statement{SQL: "1 = 0", Args: []any{}}, nil + } + result, err := r.renderCondition(cond) if err != nil { return Statement{}, err @@ -38,24 +49,71 @@ func (r *renderer) Render(cond Condition) (Statement, error) { if args == nil { args = []any{} } - - switch { - case result.unsatisfiable: - return Statement{ - SQL: "1 = 0", - Args: args, - }, nil - case result.trivial: - return Statement{ - SQL: "", - Args: args, - }, nil - default: - return Statement{ - SQL: result.sql, - Args: args, - }, nil + if err := checkPlaceholderParity(result.sql, args, r.dialect, r.placeholderOffset); err != nil { + return Statement{}, err } + return Statement{SQL: result.sql, Args: args}, nil +} + +// foldConstants removes boolean constants from the condition tree, so every +// subtree the renderer visits contributes its SQL and its arguments together. +// The result is either a single ConstantCondition or a tree with no constants. +func foldConstants(cond Condition) Condition { + switch c := cond.(type) { + case *LogicalCondition: + left := foldConstants(c.Left) + right := foldConstants(c.Right) + leftConst, leftOK := left.(*ConstantCondition) + rightConst, rightOK := right.(*ConstantCondition) + // AND is decided by a false operand, OR by a true one; the other + // constant is the operator's identity and drops out. + decisive := c.Operator == LogicalOr + if (leftOK && leftConst.Value == decisive) || (rightOK && rightConst.Value == decisive) { + return &ConstantCondition{Value: decisive} + } + if leftOK { + return right + } + if rightOK { + return left + } + return &LogicalCondition{Operator: c.Operator, Left: left, Right: right} + case *NotCondition: + child := foldConstants(c.Expr) + if childConst, ok := child.(*ConstantCondition); ok { + return &ConstantCondition{Value: !childConst.Value} + } + return &NotCondition{Expr: child} + default: + return cond + } +} + +var postgresPlaceholderPattern = regexp.MustCompile(`\$(\d+)`) + +// checkPlaceholderParity rejects a fragment whose placeholders and arguments +// disagree. Callers bind positionally after appending their own predicates, so +// a surplus argument would silently bind to a later placeholder. +func checkPlaceholderParity(sql string, args []any, dialect DialectName, offset int) error { + if dialect != DialectPostgres { + if count := strings.Count(sql, "?"); count != len(args) { + return errors.Errorf("filter rendered %d placeholders for %d arguments", count, len(args)) + } + return nil + } + matches := postgresPlaceholderPattern.FindAllStringSubmatch(sql, -1) + if len(matches) != len(args) { + return errors.Errorf("filter rendered %d placeholders for %d arguments", len(matches), len(args)) + } + seen := make(map[int]bool, len(matches)) + for _, match := range matches { + n, err := strconv.Atoi(match[1]) + if err != nil || n <= offset || n > offset+len(args) || seen[n] { + return errors.Errorf("filter rendered out-of-range placeholder $%s", match[1]) + } + seen[n] = true + } + return nil } func (r *renderer) renderCondition(cond Condition) (renderResult, error) { @@ -79,10 +137,7 @@ func (r *renderer) renderCondition(cond Condition) (renderResult, error) { case *ListComprehensionCondition: return r.renderListComprehension(c) case *ConstantCondition: - if c.Value { - return renderResult{trivial: true}, nil - } - return renderResult{sql: "1 = 0", unsatisfiable: true}, nil + return renderResult{}, errors.New("constant condition must be folded before rendering") default: return renderResult{}, errors.Errorf("unsupported condition type %T", c) } @@ -99,10 +154,8 @@ func (r *renderer) renderLogicalCondition(cond *LogicalCondition) (renderResult, } switch cond.Operator { - case LogicalAnd: - return combineAnd(left, right), nil - case LogicalOr: - return combineOr(left, right), nil + case LogicalAnd, LogicalOr: + return renderResult{sql: fmt.Sprintf("(%s %s %s)", left.sql, cond.Operator, right.sql)}, nil default: return renderResult{}, errors.Errorf("unsupported logical operator %s", cond.Operator) } @@ -114,12 +167,6 @@ func (r *renderer) renderNotCondition(cond *NotCondition) (renderResult, error) return renderResult{}, err } - if child.trivial { - return renderResult{sql: "1 = 0", unsatisfiable: true}, nil - } - if child.unsatisfiable { - return renderResult{trivial: true}, nil - } return renderResult{ sql: fmt.Sprintf("NOT (%s)", child.sql), }, nil @@ -758,36 +805,6 @@ func (r *renderer) jsonBoolPredicate(field Field) (string, error) { } } -func combineAnd(left, right renderResult) renderResult { - if left.unsatisfiable || right.unsatisfiable { - return renderResult{sql: "1 = 0", unsatisfiable: true} - } - if left.trivial { - return right - } - if right.trivial { - return left - } - return renderResult{ - sql: fmt.Sprintf("(%s AND %s)", left.sql, right.sql), - } -} - -func combineOr(left, right renderResult) renderResult { - if left.trivial || right.trivial { - return renderResult{trivial: true} - } - if left.unsatisfiable { - return right - } - if right.unsatisfiable { - return left - } - return renderResult{ - sql: fmt.Sprintf("(%s OR %s)", left.sql, right.sql), - } -} - func (r *renderer) addArg(value any) string { r.placeholderCounter++ r.args = append(r.args, value) diff --git a/filter/render_test.go b/filter/render_test.go new file mode 100644 index 000000000..634e47875 --- /dev/null +++ b/filter/render_test.go @@ -0,0 +1,121 @@ +package filter + +import ( + "context" + "fmt" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +// Callers append their own predicates after the filter fragment and bind +// positionally, so an argument left behind by a folded-away operand would bind +// to the caller's next placeholder. Folded operands must take their arguments +// with them. +func TestRenderFoldedOperandsDropTheirArgs(t *testing.T) { + t.Parallel() + + engine, err := NewEngine(NewSchema()) + require.NoError(t, err) + + tests := []struct { + filter string + sql string // "" for trivially true, "1 = 0" for unsatisfiable, "*" for any non-constant fragment + args []any + }{ + {filter: `creator_id == 1 || true`, sql: "", args: []any{}}, + {filter: `true || creator_id == 1`, sql: "", args: []any{}}, + {filter: `creator_id == 1 && false`, sql: "1 = 0", args: []any{}}, + {filter: `false && creator_id == 1`, sql: "1 = 0", args: []any{}}, + {filter: `!(creator_id == 1 || true)`, sql: "1 = 0", args: []any{}}, + {filter: `!(creator_id == 1 && false)`, sql: "", args: []any{}}, + {filter: `(creator_id == 1 || true) && visibility == "PUBLIC"`, sql: "*", args: []any{"PUBLIC"}}, + {filter: `visibility == "PUBLIC" && (creator_id == 1 || true)`, sql: "*", args: []any{"PUBLIC"}}, + {filter: `(creator_id == 1 && false) || visibility == "PUBLIC"`, sql: "*", args: []any{"PUBLIC"}}, + {filter: `visibility == "PUBLIC" || (creator_id == 1 && false)`, sql: "*", args: []any{"PUBLIC"}}, + {filter: `!(creator_id == 1 || true) || visibility == "PUBLIC"`, sql: "*", args: []any{"PUBLIC"}}, + {filter: `(creator_id == 1 && true) || (tags.exists(t, t == "a") && false)`, sql: "*", args: []any{int64(1)}}, + { + filter: `(visibility == "PRIVATE" || true) && (creator_id == 1 || true) && visibility == "PUBLIC" && creator_id == 2`, + sql: "*", + args: []any{"PUBLIC", int64(2)}, + }, + } + + for _, dialect := range []DialectName{DialectSQLite, DialectMySQL, DialectPostgres} { + for _, offset := range []int{0, 3} { + for _, tc := range tests { + t.Run(fmt.Sprintf("%s/offset=%d/%s", dialect, offset, tc.filter), func(t *testing.T) { + stmt, err := engine.CompileToStatement(context.Background(), tc.filter, RenderOptions{ + Dialect: dialect, + PlaceholderOffset: offset, + }) + require.NoError(t, err) + require.Equal(t, tc.args, stmt.Args) + if tc.sql == "*" { + require.NotContains(t, []string{"", "1 = 0"}, stmt.SQL) + } else { + require.Equal(t, tc.sql, stmt.SQL) + } + require.Equal(t, expectedPlaceholders(dialect, offset, len(tc.args)), placeholdersIn(dialect, stmt.SQL)) + }) + } + } + } +} + +func TestCheckPlaceholderParityRejectsMismatch(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + dialect DialectName + sql string + args []any + offset int + wantErr bool + }{ + {name: "sqlite match", dialect: DialectSQLite, sql: "a = ? AND b = ?", args: []any{1, 2}}, + {name: "sqlite surplus arg", dialect: DialectSQLite, sql: "a = ?", args: []any{1, 2}, wantErr: true}, + {name: "mysql missing arg", dialect: DialectMySQL, sql: "a = ? AND b = ?", args: []any{1}, wantErr: true}, + {name: "postgres match with offset", dialect: DialectPostgres, sql: "a = $3 AND b = $4", args: []any{1, 2}, offset: 2}, + {name: "postgres surplus arg", dialect: DialectPostgres, sql: "a = $1", args: []any{1, 2}, wantErr: true}, + {name: "postgres below offset", dialect: DialectPostgres, sql: "a = $1", args: []any{1}, offset: 1, wantErr: true}, + {name: "postgres beyond args", dialect: DialectPostgres, sql: "a = $3", args: []any{1}, offset: 1, wantErr: true}, + {name: "postgres repeated", dialect: DialectPostgres, sql: "a = $1 AND b = $1", args: []any{1, 2}, wantErr: true}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := checkPlaceholderParity(tc.sql, tc.args, tc.dialect, tc.offset) + if tc.wantErr { + require.Error(t, err) + } else { + require.NoError(t, err) + } + }) + } +} + +func expectedPlaceholders(dialect DialectName, offset, count int) []string { + placeholders := []string{} + for i := 1; i <= count; i++ { + if dialect == DialectPostgres { + placeholders = append(placeholders, fmt.Sprintf("$%d", offset+i)) + } else { + placeholders = append(placeholders, "?") + } + } + return placeholders +} + +func placeholdersIn(dialect DialectName, sql string) []string { + if dialect == DialectPostgres { + matches := postgresPlaceholderPattern.FindAllString(sql, -1) + if matches == nil { + return []string{} + } + return matches + } + return expectedPlaceholders(dialect, 0, strings.Count(sql, "?")) +} diff --git a/store/test/memo_access_test.go b/store/test/memo_access_test.go index 70e3b6c6e..afae98500 100644 --- a/store/test/memo_access_test.go +++ b/store/test/memo_access_test.go @@ -208,6 +208,39 @@ func TestMemoAccessScopeAllowsArchivedCreatorButFiltersMissingCreatorBeforePagin "a newer dangling-creator row must be removed before LIMIT is applied") } +func TestMemoAccessScopeIgnoresFoldedFilterOperands(t *testing.T) { + ctx := context.Background() + ts := NewTestingStore(ctx, t) + defer ts.Close() + + owner, err := ts.CreateUser(ctx, &store.User{Username: "access-folded-owner", Role: store.RoleUser, PasswordHash: "hash"}) + require.NoError(t, err) + viewer, err := ts.CreateUser(ctx, &store.User{Username: "access-folded-viewer", Role: store.RoleUser, PasswordHash: "hash"}) + require.NoError(t, err) + + _, err = ts.CreateMemo(ctx, &store.Memo{UID: "access-folded-private", CreatorID: owner.ID, Content: "private", Visibility: store.Private}) + require.NoError(t, err) + public, err := ts.CreateMemo(ctx, &store.Memo{UID: "access-folded-public", CreatorID: owner.ID, Content: "public", Visibility: store.Public}) + require.NoError(t, err) + + // Each filter folds an operand away. Its arguments must go with it, or they + // would bind to the row-status and access-scope placeholders that follow. + for _, memoFilter := range []string{ + fmt.Sprintf(`(creator_id == %d || true) && visibility == "PUBLIC"`, owner.ID), + fmt.Sprintf(`(creator_id == %d && false) || visibility == "PUBLIC"`, owner.ID), + fmt.Sprintf(`!(creator_id == %d || true) || visibility == "PUBLIC"`, owner.ID), + } { + normal := store.Normal + memos, err := ts.ListMemos(ctx, &store.FindMemo{ + RowStatus: &normal, + Filters: []string{memoFilter}, + Access: &store.MemoAccessScope{UserID: &viewer.ID, AllowPublic: true, AllowProtected: true}, + }) + require.NoError(t, err, memoFilter) + require.Equal(t, []int32{public.ID}, memoIDs(memos), memoFilter) + } +} + func memoIDs(memos []*store.Memo) []int32 { ids := make([]int32, 0, len(memos)) for _, memo := range memos {