policyfixes.md raw

Policy System Fixes Summary

Overview

This document summarizes the comprehensive fixes made to the ORLY policy system based on Issue #5 requirements and user feedback. The policy system now correctly implements relay access control with predictable, secure behavior.

Critical Conceptual Fixes

1. Write/Read Allow Lists Control Submitters, Not Authors

Problem: The policy system was incorrectly checking if the EVENT AUTHOR was in the allow/deny lists.

Solution: Changed to check the loggedInPubkey (the authenticated user submitting/reading events), not ev.Pubkey (event author).

// Before (WRONG):
if utils.FastEqual(ev.Pubkey, allowedPubkey) {

// After (CORRECT):
if utils.FastEqual(loggedInPubkey, allowedPubkey) {

This correctly implements relay access control (who can authenticate and perform operations), not content validation.

2. Privileged Flag Only Affects Read Operations

Problem: The privileged flag was incorrectly being applied to both read and write operations.

Solution: Privileged flag now ONLY affects read operations. It allows parties involved in an event (author or p-tagged users) to read it, but doesn't restrict who can write such events.

3. Read Access Uses OR Logic

Problem: When both read_allow and privileged were set, the allow list was overriding privileged access, blocking involved parties.

Solution: Implemented OR logic for read access - users can read if they are:

4. Implicit Kind Whitelist

Problem: All kinds were allowed by default even when specific rules existed.

Solution: Kinds with defined rules are now implicitly whitelisted. If specific rules exist, only kinds with rules are allowed. This provides automatic kind filtering based on rule presence.

5. Security: Reject All Unauthenticated Access

Problem: Unauthenticated users could access certain content.

Solution: Added blanket rejection of all unauthenticated requests at the beginning of policy evaluation. No authentication = no access, regardless of policy rules.

Policy Evaluation Order

1. Authentication Check
   - Reject if no authenticated pubkey

2. Global Rules (if configured)
   - Skip if no global rules exist

3. Kind Whitelist/Blacklist
   - Explicit whitelist/blacklist if configured
   - Implicit whitelist based on rule presence
   - Allow all if no rules defined

4. Script Execution (if configured and enabled)

5. Rule-based Filtering:
   a. Universal Constraints (size, tags, timestamps)
   b. Explicit Denials (highest priority)
   c. Read Access (OR logic):
      - With allow list: user in list OR (privileged AND involved)
      - Without allow list but privileged: only involved parties
      - Neither: continue to other checks
   d. Write Access:
      - Allow lists control submitters (not affected by privileged)
      - Empty list = allow all
      - Non-empty list = ONLY those users
   e. Deny-Only Lists (if no allow lists, non-denied users allowed)
   f. Default Policy

Important Behavioral Rules

Authentication Required

Allow/Deny Lists Control Submitters

Allow Lists

Deny Lists

Privileged Events (READ ONLY)

Kind Filtering (Implicit Whitelist)

Default Policy

Bug Fixes

1. Global Rule Processing

2. Empty Allow List Semantics

3. Deny-Only List Logic

4. Privileged with Empty Allow List

5. Binary Cache Optimization

Test Updates

- Submitter-based access control (not author-based) - Privileged read-only behavior - OR logic for read access - Authentication requirement - Implicit kind whitelisting

Files Modified

  1. /pkg/policy/policy.go - Core implementation fixes
  2. /pkg/policy/policy_test.go - Updated tests for correct behavior
  3. /pkg/policy/comprehensive_test.go - New test for all 5 requirements
  4. /pkg/policy/precedence_test.go - New test for evaluation order
  5. /pkg/policy/read_access_test.go - Updated for OR logic
  6. /pkg/policy/policy_integration_test.go - Updated for privileged behavior
  7. Documentation files in /docs/ - Comprehensive documentation

Result

The policy system now provides:

All 5 requirements from Issue #5 are now correctly implemented and verified.