Browse Source

fixed relay validation

imwald
Silberengel 4 months ago
parent
commit
90023e4f88
  1. 6
      src/components/PostEditor/PostRelaySelector.tsx
  2. 4
      src/lib/event-metadata.ts
  3. 17
      src/services/relay-selection.service.ts

6
src/components/PostEditor/PostRelaySelector.tsx

@ -96,7 +96,11 @@ export default function PostRelaySelector({
...cacheRelayList.originalRelays ...cacheRelayList.originalRelays
.filter(relay => (relay.scope === 'both' || relay.scope === 'write') && isLocalNetworkUrl(relay.url)) .filter(relay => (relay.scope === 'both' || relay.scope === 'write') && isLocalNetworkUrl(relay.url))
.map(relay => relay.url) .map(relay => relay.url)
].filter(url => isLocalNetworkUrl(url)) ].filter(url => {
// Filter out invalid/empty URLs
if (!url || typeof url !== 'string' || url.trim() === '' || url === 'ws://' || url === 'wss://') return false
return isLocalNetworkUrl(url)
})
const existingUrls = new Set(userWriteRelays.map(url => normalizeUrl(url) || url)) const existingUrls = new Set(userWriteRelays.map(url => normalizeUrl(url) || url))
const newCacheRelays = cacheRelays const newCacheRelays = cacheRelays
.map(url => normalizeUrl(url) || url) .map(url => normalizeUrl(url) || url)

4
src/lib/event-metadata.ts

@ -22,7 +22,9 @@ export function getRelayListFromEvent(event?: Event | null, blockedRelays?: stri
const normalizedBlockedRelays = (blockedRelays || []).map(url => normalizeUrl(url) || url) const normalizedBlockedRelays = (blockedRelays || []).map(url => normalizeUrl(url) || url)
event.tags.filter(tagNameEquals('r')).forEach(([, url, type]) => { event.tags.filter(tagNameEquals('r')).forEach(([, url, type]) => {
if (!url || !isWebsocketUrl(url)) return // Filter out empty, invalid, or malformed URLs
if (!url || typeof url !== 'string' || url.trim() === '' || url === 'ws://' || url === 'wss://') return
if (!isWebsocketUrl(url)) return
const normalizedUrl = normalizeUrl(url) const normalizedUrl = normalizeUrl(url)
if (!normalizedUrl) return if (!normalizedUrl) return

17
src/services/relay-selection.service.ts

@ -127,6 +127,13 @@ class RelaySelectionService {
return this.filterBlockedRelays(deduplicatedRelays, context.blockedRelays) return this.filterBlockedRelays(deduplicatedRelays, context.blockedRelays)
} }
/**
* Validate that a URL is a valid, non-empty relay URL
*/
private isValidRelayUrl(url: string | undefined | null): url is string {
return !!(url && typeof url === 'string' && url.trim() !== '' && url !== 'ws://' && url !== 'wss://')
}
/** /**
* Get relay list from IndexedDB cache (kind 10002 and 10432 merged) * Get relay list from IndexedDB cache (kind 10002 and 10432 merged)
* If not in cache, fetch from relays before returning empty * If not in cache, fetch from relays before returning empty
@ -162,9 +169,15 @@ class RelaySelectionService {
if (cacheRelayListEvent) { if (cacheRelayListEvent) {
const cacheRelayList = getRelayListFromEvent(cacheRelayListEvent) const cacheRelayList = getRelayListFromEvent(cacheRelayListEvent)
// Filter out invalid/empty URLs before merging
const validCacheRead = cacheRelayList.read.filter(this.isValidRelayUrl)
const validCacheWrite = cacheRelayList.write.filter(this.isValidRelayUrl)
const validRelayRead = relayList.read.filter(this.isValidRelayUrl)
const validRelayWrite = relayList.write.filter(this.isValidRelayUrl)
// Merge read relays - cache relays first, then others // Merge read relays - cache relays first, then others
const mergedRead = [...cacheRelayList.read, ...relayList.read] const mergedRead = [...validCacheRead, ...validRelayRead]
const mergedWrite = [...cacheRelayList.write, ...relayList.write] const mergedWrite = [...validCacheWrite, ...validRelayWrite]
const mergedOriginalRelays = new Map<string, { url: string; scope: 'read' | 'write' | 'both' }>() const mergedOriginalRelays = new Map<string, { url: string; scope: 'read' | 'write' | 'both' }>()
// Add cache relay original relays first (prioritized) // Add cache relay original relays first (prioritized)

Loading…
Cancel
Save