Skip to content

Commit

Permalink
Always check res.ok and content-type header
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Nov 20, 2024
1 parent c88afc5 commit 856014e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 25 deletions.
7 changes: 3 additions & 4 deletions lib/cln.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from 'cross-fetch'
import crypto from 'crypto'
import { getAgent } from '@/lib/proxy'
import { assertContentTypeJson } from './url'
import { assertContentTypeJson, assertResponseOk } from './url'

export const createInvoice = async ({ socket, rune, cert, label, description, msats, expiry }) => {
const agent = getAgent({ hostname: socket, cert })
Expand All @@ -27,9 +27,8 @@ export const createInvoice = async ({ socket, rune, cert, label, description, ms
})
})

if (!res.ok) {
assertContentTypeJson(res)
}
assertResponseOk(res)
assertContentTypeJson(res)

const inv = await res.json()
if (inv.error) {
Expand Down
6 changes: 6 additions & 0 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ export function parseNwcUrl (walletConnectUrl) {
return params
}

export function assertResponseOk (res) {
if (!res.ok) {
throw new Error(`POST ${res.url}: ${res.status} ${res.statusText}`)
}
}

export function assertContentTypeJson (res) {
const contentType = res.headers.get('content-type')
if (!contentType || !contentType.includes('application/json')) {
Expand Down
19 changes: 10 additions & 9 deletions wallets/blink/common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { assertContentTypeJson, assertResponseOk } from '@/lib/url'

export const galoyBlinkUrl = 'https://api.blink.sv/graphql'
export const galoyBlinkDashboardUrl = 'https://dashboard.blink.sv/'

Expand Down Expand Up @@ -37,15 +39,14 @@ export async function request (authToken, query, variables = {}) {
body: JSON.stringify({ query, variables })
}
const res = await fetch(galoyBlinkUrl, options)
if (res.status >= 400 && res.status <= 599) {
// consume res
res.text().catch(() => {})
if (res.status === 401) {
throw new Error('unauthorized')
} else {
throw new Error('API responded with HTTP ' + res.status)
}
}

// consume response body to avoid memory leaks
// see https://github.com/nodejs/node/issues/51162
res.text().catch(() => {})

assertResponseOk(res)
assertContentTypeJson(res)

return res.json()
}

Expand Down
10 changes: 8 additions & 2 deletions wallets/lightning-address/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { msatsSatsFloor } from '@/lib/format'
import { lnAddrOptions } from '@/lib/lnurl'
import { assertContentTypeJson, assertResponseOk } from '@/lib/url'

export * from '@/wallets/lightning-address'

Expand All @@ -24,8 +25,13 @@ export const createInvoice = async (
}

// call callback with amount and conditionally comment
const res = await (await fetch(callbackUrl.toString())).json()
if (res.status === 'ERROR') {
const res = await fetch(callbackUrl.toString())

assertResponseOk(res)
assertContentTypeJson(res)

const body = await res.json()
if (body.status === 'ERROR') {
throw new Error(res.reason)
}

Expand Down
9 changes: 6 additions & 3 deletions wallets/lnbits/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ async function getWallet ({ url, adminKey, invoiceKey }) {
headers.append('X-Api-Key', adminKey || invoiceKey)

const res = await fetch(url + path, { method: 'GET', headers })

assertContentTypeJson(res)
if (!res.ok) {
assertContentTypeJson(res)
const errBody = await res.json()
throw new Error(errBody.detail)
}
Expand All @@ -54,8 +55,9 @@ async function postPayment (bolt11, { url, adminKey }) {
const body = JSON.stringify({ bolt11, out: true })

const res = await fetch(url + path, { method: 'POST', headers, body })

assertContentTypeJson(res)
if (!res.ok) {
assertContentTypeJson(res)
const errBody = await res.json()
throw new Error(errBody.detail)
}
Expand All @@ -73,8 +75,9 @@ async function getPayment (paymentHash, { url, adminKey }) {
headers.append('X-Api-Key', adminKey)

const res = await fetch(url + path, { method: 'GET', headers })

assertContentTypeJson(res)
if (!res.ok) {
assertContentTypeJson(res)
const errBody = await res.json()
throw new Error(errBody.detail)
}
Expand Down
3 changes: 2 additions & 1 deletion wallets/lnbits/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export async function createInvoice (
agent,
body
})

assertContentTypeJson(res)
if (!res.ok) {
assertContentTypeJson(res)
const errBody = await res.json()
throw new Error(errBody.detail)
}
Expand Down
8 changes: 5 additions & 3 deletions wallets/phoenixd/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { assertContentTypeJson, assertResponseOk } from '@/lib/url'

export * from '@/wallets/phoenixd'

export async function testSendPayment (config, { logger }) {
Expand All @@ -24,9 +26,9 @@ export async function sendPayment (bolt11, { url, primaryPassword }) {
headers,
body
})
if (!res.ok) {
throw new Error(`POST ${res.url}: ${res.status} ${res.statusText}`)
}

assertResponseOk(res)
assertContentTypeJson(res)

const payment = await res.json()
const preimage = payment.paymentPreimage
Expand Down
7 changes: 4 additions & 3 deletions wallets/phoenixd/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { msatsToSats } from '@/lib/format'
import { assertContentTypeJson, assertResponseOk } from '@/lib/url'

export * from '@/wallets/phoenixd'

Expand Down Expand Up @@ -28,9 +29,9 @@ export async function createInvoice (
headers,
body
})
if (!res.ok) {
throw new Error(`POST ${res.url}: ${res.status} ${res.statusText}`)
}

assertResponseOk(res)
assertContentTypeJson(res)

const payment = await res.json()
return payment.serialized
Expand Down

0 comments on commit 856014e

Please sign in to comment.