Skip to content

Conversation

@BitcoinErrorLog
Copy link
Owner

Phase 3: Medium Priority Fixes (Post-Launch)

This PR addresses 8 medium-priority issues identified in the production audit report.

Changes

1. Fix MutationObserver Memory Leak (1 hour)

  • File: src/content/PubkyURLHandler.ts
  • Issue: Observer created but never stored for cleanup
  • Fix:
    • Added private domObserver: MutationObserver | null property
    • Store observer reference in observeDOMForPubkyURLs()
    • Added cleanup() method to disconnect observer
  • Impact: Prevents memory leaks on long-running pages

2. Create SDK Type Definitions (4 hours)

  • New file: src/types/pubky.d.ts
  • Issue: 79 instances of any types due to missing SDK type definitions
  • Fix: Created comprehensive type definitions for:
    • Client, TestnetClient, AuthRequest, PublicKey, Session
    • SDK functions: validateCapabilities, setLogLevel, createRecoveryFile
  • Impact: Improved type safety, better IDE support, reduced runtime errors

3. Add setLogLevel Configuration (1 hour)

  • File: src/utils/pubky-client-factory.ts
  • Issue: SDK logging level not configured
  • Fix: Configure log level during singleton initialization:
    • Production: error level only
    • Development: debug level for verbose logging
  • Impact: Cleaner production console, better debugging in dev

4. Add Testnet Support (1 hour)

  • File: src/utils/pubky-client-factory.ts
  • Issue: Always uses mainnet, cannot test against testnet
  • Fix:
    • Support VITE_PUBKY_NETWORK environment variable
    • Use TestnetClient when testnet mode enabled
    • Allows development without mainnet data pollution
  • Impact: Safer development workflow

5. Remove console.log Statements (2 hours)

  • Files: src/background/background.ts, src/offscreen/offscreen.ts
  • Issue: 29 console.log/error statements instead of logger utility
  • Fix:
    • Replaced all console statements with logger calls
    • Added logger import to offscreen.ts
    • Consistent logging format across codebase
  • Impact: Better log management, consistent format

6. Document @ts-ignore Comments (30 min)

  • Files:
    • src/content/AnnotationManager.ts - dom-anchor-text-quote types
    • src/sidepanel/App.tsx - pagination cursor
    • src/config/config.ts - import.meta
  • Fix: Added explanatory comments with:
    • Reason for ignore
    • Tracking links (where applicable)
    • TODOs for future improvements
  • Impact: Better code documentation, easier maintenance

7. Document Manifest Security Decisions (30 min)

  • File: manifest.json
  • Issue: Broad permissions and CSP directives need justification
  • Fix: Added comments explaining:
    • web_accessible_resources scope required for pubky:// URL display
    • wasm-unsafe-eval required for Pubky SDK WebAssembly
  • Impact: Clear documentation for Chrome Web Store review

Verification

  • TypeScript compilation passes (tsc --noEmit)
  • MutationObserver cleanup method added
  • Type definitions created and imported
  • All console.log statements replaced
  • @ts-ignore comments documented
  • Run npm test - verify all tests pass
  • Manual testing of content script cleanup
  • Test testnet mode with VITE_PUBKY_NETWORK=testnet

Related

Part of comprehensive production audit remediation plan:

  • Phase 1: Critical fixes (✅ merged)
  • Phase 2: High priority fixes (✅ merged)
  • Phase 3: Medium priority fixes (this PR) - 12 hours
  • Phase 4: Technical debt - 11.5 hours

Audit Report

See COMPREHENSIVE_AUDIT_REPORT.md for full details.

…stnet

- Fix MutationObserver memory leak in PubkyURLHandler
  - Store observer reference and add cleanup() method
  - Prevents memory leaks on long-running pages

- Create SDK Type Definitions
  - New file: src/types/pubky.d.ts
  - Provides type safety for @synonymdev/pubky SDK
  - Reduces 'any' type usage across codebase

- Add setLogLevel Configuration
  - Configure SDK logging in pubky-client-factory
  - Error level in production, debug in development

- Add Testnet Support
  - Support VITE_PUBKY_NETWORK environment variable
  - Use TestnetClient when testnet mode enabled
  - Allows development against testnet without mainnet pollution

- Remove console.log Statements
  - Replace 20 console.log/error statements in background.ts
  - Replace 9 console.log/error statements in offscreen.ts
  - Use logger utility consistently for all logging

- Document @ts-ignore Comments
  - Add explanations for all 3 @ts-ignore instances
  - Include tracking links and TODOs where applicable

- Document Manifest Security Decisions
  - Add comments explaining web_accessible_resources scope
  - Add comment explaining CSP wasm-unsafe-eval requirement
  - Both are required for core functionality

Addresses medium priority issues from production audit:
- Memory leaks from unmanaged observers
- Type safety improvements (79 any types)
- Logging consistency
- Development environment support

Phase 3 of comprehensive production audit remediation plan.
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 78 to 88
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'"
// Note: 'wasm-unsafe-eval' is required for Pubky SDK WebAssembly support
},
"web_accessible_resources": [
{
"resources": ["src/profile/profile-renderer.html"],
"matches": ["<all_urls>"]
// Note: Profile renderer needs to be accessible from any page to display
// pubky:// URLs. This is a core feature requirement.
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 Badge Manifest JSON no longer parses with added comments

Manifest v3 files must be valid JSON, but manifest.json now includes // comments in the CSP and web_accessible_resources sections. Chrome rejects manifests containing comments, so the extension will fail to load in any environment until the file is valid JSON again.

Useful? React with 👍 / 👎.

Comment on lines +45 to +50
// Configure log level if available
if ('setLogLevel' in pubkyModule && typeof (pubkyModule as any).setLogLevel === 'function') {
const setLogLevel = (pubkyModule as any).setLogLevel;
if (process.env.NODE_ENV === 'production') {
setLogLevel('error');
} else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Using process.env in browser breaks Pubky client init

Both client initialization paths now branch on process.env.NODE_ENV, but the extension is built with Vite (import.meta.env) and no process polyfill; accessing process in the service worker/offscreen context will throw ReferenceError: process is not defined, preventing the Pubky client from ever instantiating and blocking auth/API calls.

Useful? React with 👍 / 👎.

@BitcoinErrorLog BitcoinErrorLog merged commit 320249b into main Dec 12, 2025
1 of 3 checks passed
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