Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ coverage/
*.swo
.claude-memory.md
scripts/manual-release.sh
scripts/manual-release.sh
146 changes: 146 additions & 0 deletions IMPLEMENTATION_PLAN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Security Fixes Implementation Plan

## Status: IN PROGRESS

This document tracks the implementation of all security fixes from the security assessment.

## Completed βœ…

1. **P0-1: Error Sanitization** - βœ… DONE
- Created `src/core/security-utils.ts` with sanitizeError() function
- Updated `zhtp-api-core.ts` to sanitize errors before logging
- Prevents credential leakage in debug logs

2. **P2-2: Configurable Timeouts** - βœ… DONE
- Added optional `timeoutMs` parameter to request() method
- Allows per-operation timeout configuration

3. **P2-4: Content-Type Validation** - βœ… DONE
- Added Content-Type header validation in request() method
- Rejects non-JSON responses before parsing

4. **Security Utils Created** - βœ… DONE
- Input validation functions (DID, identity ID, contract ID, etc.)
- Passphrase strength validation (16+ chars, 60+ bits entropy, complexity)
- Rate limiting helpers
- URL construction helpers

## In Progress πŸ”„

5. **P0-2: Input Validation** - πŸ”„ NEXT
- Need to apply validation to all API methods in zhtp-api-methods.ts
- Files to update:
- `src/core/zhtp-api-methods.ts` (all methods with ID parameters)

## Pending πŸ“‹

### Critical (P0/P1)

6. **Default ZHTP Configuration**
- Update default URLs in all config providers
- Change from `http://localhost:8000` to proper QUIC config
- Files: `vanilla-js/config-provider.ts`, `react-native/config-provider.ts`, `electron/config-provider.ts`

7. **P1-2: Passphrase Requirements**
- Apply validatePassphraseStrength() to exportBackup() and importBackup()
- File: `src/core/zhtp-api-methods.ts`

8. **P1-3: Seed Phrase Security**
- Remove seedPhrases from Identity type (make separate secure retrieval)
- Update mapSignupResponseToIdentity() to not include seeds by default
- Add explicit retrieveSeedPhrases() method with warnings
- Files: `src/core/types.ts`, `src/core/zhtp-api-methods.ts`

9. **P1-4: CSRF Protection**
- Add CSRF token generation/validation helpers
- Include CSRF tokens in state-changing operations
- File: `src/core/security-utils.ts`, update all POST/DELETE/PUT methods

### Medium (P2)

10. **P2-1: Client-Side Rate Limiting**
- Apply isRateLimited() to login, signup, backup import
- Files: `src/core/zhtp-api-methods.ts`

11. **P2-5: URL Construction**
- Replace manual query string construction with constructUrl()
- Files: `src/core/zhtp-api-methods.ts` (multiple methods)

12. **P2-6: Electron Config Validation**
- Add schema validation for IPC config responses
- File: `src/electron/config-provider.ts`

13. **P2-7: Initialization Guards**
- Add ensureInitialized() checks to all public methods
- File: `src/core/zhtp-api.ts`

14. **P2-8: Dependency Updates**
- Run `npm audit fix`
- Update vulnerable dependencies
- File: `package.json`

### Documentation & Testing

15. **SECURITY.md**
- Create comprehensive security documentation
- Include best practices, known limitations, reporting procedures

16. **Security Tests**
- Create `src/core/security-utils.test.ts`
- Add tests for all validation functions
- Add integration tests for security features

17. **Final Validation**
- Run `npm run type-check`
- Run `npm run build`
- Run `npm test`
- Verify all tests pass

## Implementation Strategy

### Phase 1: Core Security (Items 5-9) - HIGHEST PRIORITY
These are blocking issues that prevent secure production use.

### Phase 2: Additional Protections (Items 10-14) - HIGH PRIORITY
These improve defense-in-depth.

### Phase 3: Documentation & Testing (Items 15-17) - REQUIRED FOR RELEASE
These ensure maintainability and proper usage.

## Estimated Timeline

- **Phase 1**: 2-3 hours (critical fixes)
- **Phase 2**: 1-2 hours (additional protections)
- **Phase 3**: 1-2 hours (documentation & testing)
- **Total**: 4-7 hours for complete implementation

## Files Modified So Far

1. βœ… `src/core/security-utils.ts` (created)
2. βœ… `src/core/zhtp-api-core.ts` (updated)

## Files Remaining

3. πŸ“‹ `src/core/zhtp-api-methods.ts` (major updates needed)
4. πŸ“‹ `src/core/types.ts` (seed phrase security)
5. πŸ“‹ `src/core/zhtp-api.ts` (initialization guards)
6. πŸ“‹ `src/vanilla-js/config-provider.ts` (default URL)
7. πŸ“‹ `src/react-native/config-provider.ts` (default URL)
8. πŸ“‹ `src/electron/config-provider.ts` (config validation)
9. πŸ“‹ `package.json` (dependency updates)
10. πŸ“‹ `SECURITY.md` (create)
11. πŸ“‹ `src/core/security-utils.test.ts` (create)

## Next Steps

1. Update zhtp-api-methods.ts with input validation
2. Apply passphrase strength validation
3. Secure seed phrase handling
4. Add rate limiting to sensitive operations
5. Fix URL construction
6. Update config providers
7. Add initialization guards
8. Update dependencies
9. Create documentation
10. Write tests
11. Final validation
152 changes: 152 additions & 0 deletions PATH_UPDATE_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# API Path Update Summary - Issue #18

## Overview
Updated all API methods in `src/core/zhtp-api-methods.ts` to use the standardized `/api/v1` path prefix, ensuring compatibility with the ZHTP node's current API structure.

## Changes Made

### Wallet Operations
All wallet methods now use `/api/v1/wallet` prefix:

1. **getWallets()** (line 381)
- ❌ Old: `/wallet/balance?address=...`
- βœ… New: `/api/v1/wallet/balance?address=...`

2. **getTransactionHistory()** (line 395)
- ❌ Old: `/wallet/transactions?address=...`
- βœ… New: `/api/v1/wallet/transactions?address=...`

3. **getAssets()** (line 403)
- ❌ Old: `/wallet/assets?address=...`
- βœ… New: `/api/v1/wallet/assets?address=...`

### DAO Operations
All DAO methods now use `/api/v1/dao` prefix:

4. **getProposalDetails()** (line 475)
- ❌ Old: `/dao/proposals/${proposalId}`
- βœ… New: `/api/v1/dao/proposals/${proposalId}`

5. **getDelegateProfile()** (line 487)
- ❌ Old: `/dao/delegates/${delegateId}`
- βœ… New: `/api/v1/dao/delegates/${delegateId}`

6. **getVotingPower()** (line 521)
- ❌ Old: `/dao/voting-power/${userDid}`
- βœ… New: `/api/v1/dao/voting-power/${userDid}`

7. **getUserVotes()** (line 532)
- ❌ Old: `/dao/user-votes/${userDid}`
- βœ… New: `/api/v1/dao/user-votes/${userDid}`

## Already Correct Paths

The following sections were already using `/api/v1` paths correctly:

βœ… **Identity Operations** (lines 44-370)
- All `/api/v1/identity/*` endpoints

βœ… **Backup Operations** (lines 198-219)
- All `/api/v1/identity/backup/*` endpoints

βœ… **Guardian Management** (lines 238-305)
- All `/api/v1/guardian/*` and `/api/v1/identity/guardians/*` endpoints

βœ… **Network Operations** (lines 375, 607)
- `/api/v1/blockchain/network/peers`

βœ… **Blockchain Operations** (lines 595-642)
- All `/api/v1/blockchain/*` endpoints

βœ… **Smart Contract Operations** (lines 641-684)
- All `/api/v1/blockchain/contracts/*` endpoints
- All `/api/v1/contract/*` endpoints

βœ… **Zero-Knowledge Proof Operations** (lines 689-708)
- `/api/v1/zkp/generate`
- `/api/v1/zkp/verify`

βœ… **Protocol Operations** (lines 599, 603, 726)
- `/api/v1/protocol/info`
- `/api/v1/network/gas`

βœ… **Web4/DHT Operations** (lines 539-590)
- All `/api/v1/dht/*` and `/api/v1/web4/*` endpoints

## Backward Compatibility

The ZHTP node supports backward compatibility through path aliases (see `zhtp/src/server/http/router.rs:187-256`):

### Legacy Path Mappings:
- `/wallet/*` β†’ `/api/v1/wallet/*` βœ…
- `/dao/*` β†’ `/api/v1/dao/*` βœ…
- `/mesh/peers` β†’ `/api/v1/blockchain/network/peers` βœ…
- `/node/status` β†’ `/api/v1/protocol/info` βœ…
- `/blockchain/info` β†’ `/api/v1/blockchain/status` βœ…
- `/contract/*` β†’ `/api/v1/blockchain/contracts/*` βœ…

**Result:** Even though we updated the client to use `/api/v1` paths, the old paths would have still worked due to node-side aliasing. However, using the standard paths directly is cleaner and more future-proof.

## Verification

Total API methods checked: **70+ methods**

**Path Distribution:**
- Methods using `/api/v1`: **70 methods** βœ…
- Methods using `/health`: **1 method** βœ… (health check endpoint)
- Methods using legacy paths: **0 methods** βœ…

All paths now conform to the `/api/v1` standard!

## Testing Recommendations

1. **Wallet Methods Test:**
```typescript
await client.getWallets(testDid);
await client.getTransactionHistory(testAddress);
await client.getAssets(testAddress);
```

2. **DAO Methods Test:**
```typescript
await client.getProposalDetails(testProposalId);
await client.getDelegateProfile(testDelegateId);
await client.getVotingPower(testDid);
await client.getUserVotes(testDid);
```

3. **End-to-End Test:**
- Run full API client test suite against ZHTP node
- Verify all methods return expected responses
- Check error handling for invalid paths

## Impact Assessment

**Breaking Changes:** ❌ None
- All changes are path updates only
- No method signatures changed
- No request/response formats changed
- Backward compatible with existing code

**Benefits:**
- βœ… Consistent API path structure
- βœ… Future-proof against potential removal of legacy aliases
- βœ… Clearer API documentation
- βœ… Easier to maintain and understand

## Files Modified

1. `src/core/zhtp-api-methods.ts` - 7 path updates

## Related Issues

- Issue #18: [P1] Verify and update API paths to /api/v1 standard
- Parent Issue #17: Implementation Guide - Complete ZHTP Node API Endpoint Reference
- Node Issue SOVEREIGN-NET/The-Sovereign-Network#112 (closed - all endpoints implemented)

## Next Steps

1. Run TypeScript compilation check
2. Run test suite
3. Update issue #18 with completion status
4. Move to Priority 2: Backup & Recovery methods (Issue #19)
Loading