Browse Source

bug-fixes

master
Silberengel 4 weeks ago
parent
commit
c2172acc2d
  1. 3
      src/lib/services/nostr/event-hierarchy.ts
  2. 45
      src/lib/services/nostr/nostr-client.ts
  3. 25
      src/routes/api/gitea-proxy/[...path]/+server.ts

3
src/lib/services/nostr/event-hierarchy.ts

@ -75,7 +75,8 @@ export async function buildEventHierarchy(event: NostrEvent): Promise<EventHiera @@ -75,7 +75,8 @@ export async function buildEventHierarchy(event: NostrEvent): Promise<EventHiera
const kind = parseInt(parts[0], 10);
const pubkey = parts[1];
const dTag = parts[2];
if (!isNaN(kind) && pubkey && dTag) {
// Validate pubkey is a valid 64-character hex string before adding to fetch list
if (!isNaN(kind) && pubkey && dTag && /^[a-f0-9]{64}$/i.test(pubkey)) {
const key = `${kind}:${pubkey}:${dTag}`;
if (!replaceableEventsToFetch.has(key)) {
replaceableEventsToFetch.set(key, { kind, pubkey, dTag });

45
src/lib/services/nostr/nostr-client.ts

@ -1348,11 +1348,56 @@ class NostrClient { @@ -1348,11 +1348,56 @@ class NostrClient {
return parts.length > 0 ? parts.join(' ') : 'empty filter';
}
/**
* Validate that a string is a valid hex pubkey (64 hex characters)
*/
private isValidPubkey(pubkey: string): boolean {
return typeof pubkey === 'string' && /^[a-f0-9]{64}$/i.test(pubkey);
}
/**
* Sanitize filters by removing invalid pubkeys to prevent relay errors
*/
private sanitizeFilters(filters: Filter[]): Filter[] {
return filters.map(filter => {
if (filter.authors && filter.authors.length > 0) {
// Filter out invalid pubkeys
const validAuthors = filter.authors.filter(author => this.isValidPubkey(author));
if (validAuthors.length === 0) {
// If no valid authors remain, remove the authors field entirely
const { authors, ...rest } = filter;
return rest;
}
return { ...filter, authors: validAuthors };
}
return filter;
}).filter(filter => {
// Remove filters that have no valid query parameters
return (filter.ids && filter.ids.length > 0) ||
(filter.authors && filter.authors.length > 0) ||
(filter.kinds && filter.kinds.length > 0) ||
(filter['#e'] && filter['#e'].length > 0) ||
(filter['#p'] && filter['#p'].length > 0) ||
(filter['#d'] && filter['#d'].length > 0) ||
filter.since !== undefined ||
filter.until !== undefined ||
filter.search !== undefined;
});
}
async fetchEvents(
filters: Filter[],
relays: string[],
options: FetchOptions = {}
): Promise<NostrEvent[]> {
// Sanitize filters to remove invalid pubkeys before processing
filters = this.sanitizeFilters(filters);
// If all filters were invalid, return empty array
if (filters.length === 0) {
return [];
}
const { cacheResults = true, onUpdate, timeout = 10000, priority = 'medium', caller: providedCaller } = options;
const caller = providedCaller || this.getCallerInfo();

25
src/routes/api/gitea-proxy/[...path]/+server.ts

@ -275,7 +275,8 @@ function buildTargetUrl(baseUrl: string, apiPath: string, searchParams: URLSearc @@ -275,7 +275,8 @@ function buildTargetUrl(baseUrl: string, apiPath: string, searchParams: URLSearc
export const GET: RequestHandler = async ({ params, url }) => {
try {
const baseUrl = url.searchParams.get('baseUrl');
const apiPath = params.path;
// params.path might be a string or array depending on SvelteKit version
const apiPath = Array.isArray(params.path) ? params.path.join('/') : params.path;
if (!baseUrl) {
return createErrorResponse('Missing baseUrl query parameter', 400);
@ -303,8 +304,26 @@ export const GET: RequestHandler = async ({ params, url }) => { @@ -303,8 +304,26 @@ export const GET: RequestHandler = async ({ params, url }) => {
// Log error responses for debugging
if (!response.ok) {
console.error('[Gitea Proxy] Error response:', response.status, response.statusText);
console.error('[Gitea Proxy] Response body:', body.substring(0, 500));
// Skip logging 404s for README file requests - these are expected when trying multiple file extensions
const isReadmeRequest = apiPath.includes('contents') &&
(apiPath.toLowerCase().includes('readme') || apiPath.toLowerCase().includes('readme'));
if (response.status === 404 && isReadmeRequest) {
// Silently skip - expected when trying README.adoc, README.md, etc.
} else if (response.status === 404) {
// Log other 404s with context
console.warn('[Gitea Proxy] 404 Not Found:', {
apiPath,
targetUrl,
baseUrl,
body: body.substring(0, 200)
});
} else {
// Log non-404 errors
console.error('[Gitea Proxy] Error response:', response.status, response.statusText);
console.error('[Gitea Proxy] Request URL:', targetUrl);
console.error('[Gitea Proxy] Response body:', body.substring(0, 500));
}
}
return new Response(body, {

Loading…
Cancel
Save