Skip to content

feat: add admin edit endpoint for server moderation - #315

Merged
domdomegg merged 2 commits into
mainfrom
adamj/admin-edit-endpoint
Aug 27, 2025
Merged

domdomegg merged 2 commits into
mainfrom
adamj/admin-edit-endpoint

Conversation

@domdomegg

@domdomegg domdomegg commented Aug 27, 2025 •

Copy link
Copy Markdown
Member

Summary

Implements PUT /v0/servers/{id} admin edit endpoint to address part of issue #182, providing an API-based solution for server moderation without requiring direct database access.

  • New endpoint: PUT /v0/servers/{id} for editing existing servers
  • Security: Uses existing JWT auth with PermissionActionEdit permissions
  • Anti-undelete protection: Added ServerStatusDeleted status that cannot be reverted
  • Full implementation: Database, service, API, and comprehensive test coverage

Key Use Cases

  • Mark malicious servers as deleted for takedown
  • Update server metadata for moderation actions

Related to: #182 (but that is not done yet because we need admins to be able to login (probably with #283), and need to add docs on how to do the takedown, and probably guidelines on what we will/won't takedown)

Implements PUT /v0/servers/{id} endpoint to allow admins to modify server metadata for moderation purposes. Addresses part of issue #182 by providing an API-based "escape hatch" for handling malicious servers, spam, and data corruption without requiring direct database access.

Key features:
- Resource-level edit permissions using existing PermissionActionEdit
- Added ServerStatusDeleted with anti-undelete protection
- Full CRUD operations across database, service, and API layers
- Comprehensive test coverage including edge cases

Related to: #182

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>

:house: Remote-Dev: homespace
@domdomegg

Copy link
Copy Markdown
Member Author

@claude please review

@claude

claude Bot commented Aug 27, 2025 •

Copy link
Copy Markdown
Contributor

Claude finished @domdomegg's task —— View job


PR Review: Admin Edit Endpoint

This PR implements a well-designed admin edit endpoint with comprehensive security measures. Here's my detailed analysis:

**Code Review Analysis**

✅ Strengths

Security Implementation

  • JWT Authentication: Proper Bearer token extraction and validation at internal/api/handlers/v0/edit.go:42-54
  • Permission System: Uses existing PermissionActionEdit with resource pattern matching at edit.go:68-70
  • Anti-undelete Protection: Strong safeguard prevents reverting deleted servers at edit.go:87-90
  • Input Validation: Validates both JWT claims and request body structure

API Design

  • RESTful Design: Follows HTTP PUT semantics for updates at path /v0/servers/{id}
  • Consistent Error Handling: Uses Huma framework's standardized error responses
  • Request Reuse: Smart reuse of PublishRequest model for edit operations

Database Layer

  • Transaction Safety: Uses proper SQL updates with parameter binding
  • Dual Table Updates: Correctly updates both servers and server_extensions tables in postgres.go:547-603
  • Existence Validation: Checks server exists before update at postgres.go:541-544

Test Coverage

  • Comprehensive Test Cases: 11 test scenarios covering success, auth failures, validation, and edge cases
  • Mock Architecture: Proper service layer mocking for isolated testing
  • Real JWT Testing: Uses actual JWT token generation for auth scenarios

⚠️ Areas for Improvement

1. Database Transaction Consistency
Location: internal/database/postgres.go:547-603

// Updates servers and server_extensions separately without transaction
_, err = db.conn.Exec(ctx, serverQuery, ...)  // Line 570
_, err = db.conn.Exec(ctx, extQuery, ...)     // Line 597

Issue: Two separate database operations could leave data in inconsistent state if second update fails
Recommendation: Wrap both updates in a transaction

2. Remote URL Conflict Validation Missing
Location: internal/service/registry_service.go:258-297
Issue: EditServer doesn't validate duplicate remote URLs like PublishServer does at lines 234-256
Recommendation: Add remote URL conflict validation to prevent duplicate remotes

3. Resource Pattern Permission Logic
Location: internal/api/handlers/v0/edit.go:68-70

if !jwtManager.HasPermission(editRequest.Server.Name, auth.PermissionActionEdit, claims.Permissions) {

Issue: Permission check happens after JSON parsing but the server name could be changed in the edit
Recommendation: Consider checking permissions against the existing server name, not the new name

4. Error Response Consistency
Location: internal/api/handlers/v0/edit.go:98
Issue: Service layer errors return as 400 Bad Request, but some might be more appropriate as 500 Internal Server Error
Recommendation: More granular error handling based on error types

📋 Minor Observations

Schema Documentation

  • Schema correctly updated with deleted status and clear description at server.schema.json
  • Good documentation of the intended use case for deleted status

Code Style

  • Consistent with existing codebase patterns
  • Good separation of concerns between layers
  • Proper use of context timeouts

🔒 Security Assessment

Authorization: ✅ Well implemented

  • JWT validation with proper error handling
  • Resource-based permissions with pattern matching
  • Bearer token format validation

Input Validation: ✅ Comprehensive

  • JSON structure validation
  • Extension field validation
  • Server detail validation using existing validators

Anti-abuse: ✅ Strong protections

  • Cannot undelete servers once marked as deleted
  • Proper permission checking for edit operations
  • Reuses existing validation logic to prevent bypasses

✅ Final Assessment

This is a solid implementation that adds important admin functionality while maintaining security standards. The anti-undelete protection is particularly well-designed for moderation use cases.

Recommendation: Approve with minor improvements suggested above. The transaction consistency issue should be addressed before production deployment.


Todo List

  • Read and analyze all changed files
  • Review API endpoint implementation and security
  • Check database layer changes
  • Analyze service layer logic
  • Review test coverage and quality
  • Check schema/documentation updates
  • Provide comprehensive review feedback

🏠 Remote-Dev: homespace
@domdomegg
domdomegg merged commit 6431f23 into main Aug 27, 2025
@domdomegg
domdomegg deleted the adamj/admin-edit-endpoint branch August 27, 2025 20:28
domdomegg added a commit that referenced this pull request Aug 28, 2025
…ement (#323)

## Summary
- Added comprehensive admin operations documentation
- Included scripts for common server management tasks

Together with #315, #316, this fixes #182
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants