From 86f37d6003010989fbfaabe9344b2762fdba49a7 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Tue, 11 Feb 2025 10:17:52 -0800 Subject: [PATCH] Clean up nip96 upload validation and make it less strict --- nip55.test.ts | 2 +- nip96.ts | 89 +++++++++++++++++++++------------------------------ 2 files changed, 37 insertions(+), 54 deletions(-) diff --git a/nip55.test.ts b/nip55.test.ts index d2bdb1b..43dd742 100644 --- a/nip55.test.ts +++ b/nip55.test.ts @@ -2,7 +2,7 @@ import { test, expect } from 'bun:test' import * as nip55 from './nip55.js' // Function to parse the NostrSigner URI -function parseNostrSignerUri(uri) { +function parseNostrSignerUri(uri: string) { const [base, query] = uri.split('?') const basePart = base.replace('nostrsigner:', '') diff --git a/nip96.ts b/nip96.ts index 9e970da..05b23cf 100644 --- a/nip96.ts +++ b/nip96.ts @@ -267,13 +267,11 @@ export async function readServerConfig(serverUrl: string): Promise t[0] === 'url')) { + if (tags.some(t => t.length < 2 || t.some(x => typeof x !== 'string'))) { return false } - if (!(response.nip94_event.tags as string[]).find(t => t[0] === 'ox')) { + if (!tags.some(t => t[0] === 'url')) { + return false + } + + if (!tags.some(t => t[0] === 'ox')) { return false } } @@ -385,17 +377,13 @@ export async function uploadFile( throw new Error('Unknown error in uploading file!') } - try { - const parsedResponse = await response.json() + const parsedResponse = await response.json() - if (!validateFileUploadResponse(parsedResponse)) { - throw new Error('Invalid response from the server!') - } - - return parsedResponse - } catch (error) { - throw new Error('Error parsing JSON response!') + if (!validateFileUploadResponse(parsedResponse)) { + throw new Error('Failed to validate upload response!') } + + return parsedResponse } /** @@ -512,33 +500,28 @@ export async function checkFileProcessingStatus( } // Parse the response - try { - const parsedResponse = await response.json() + const parsedResponse = await response.json() - // 201 Created: Indicates the processing is over. - if (response.status === 201) { - // Validate the response - if (!validateFileUploadResponse(parsedResponse)) { - throw new Error('Invalid response from the server!') - } - - return parsedResponse + // 201 Created: Indicates the processing is over. + if (response.status === 201) { + if (!validateFileUploadResponse(parsedResponse)) { + throw new Error('Failed to validate upload response!') } - // 200 OK: Indicates the processing is still ongoing. - if (response.status === 200) { - // Validate the response - if (!validateDelayedProcessingResponse(parsedResponse)) { - throw new Error('Invalid response from the server!') - } - - return parsedResponse - } - - throw new Error('Invalid response from the server!') - } catch (error) { - throw new Error('Error parsing JSON response!') + return parsedResponse as FileUploadResponse } + + // 200 OK: Indicates the processing is still ongoing. + if (response.status === 200) { + // Validate the response + if (!validateDelayedProcessingResponse(parsedResponse)) { + throw new Error('Invalid response from the server!') + } + + return parsedResponse + } + + throw new Error('Invalid response from the server!') } /**