From d5a46a3bc4025d9c2abd2d73831bf033272f174d Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 30 Nov 2023 10:30:32 -0300 Subject: check conversion URL --- packages/taler-util/src/codec.ts | 33 +++++++++++++++++++++++++++++++++ packages/taler-util/src/taler-types.ts | 3 ++- 2 files changed, 35 insertions(+), 1 deletion(-) (limited to 'packages/taler-util') diff --git a/packages/taler-util/src/codec.ts b/packages/taler-util/src/codec.ts index 695f3c147..670111b75 100644 --- a/packages/taler-util/src/codec.ts +++ b/packages/taler-util/src/codec.ts @@ -321,6 +321,39 @@ export function codecForString(): Codec { }; } +/** + * Return a codec for a value that must be a string. + */ +export function codecForStringURL(shouldEndWithSlash?: boolean): Codec { + return { + decode(x: any, c?: Context): string { + if (typeof x !== "string") { + throw new DecodingError( + `expected string at ${renderContext(c)} but got ${typeof x}`, + ); + } + if (shouldEndWithSlash && !x.endsWith("/")) { + throw new DecodingError( + `expected URL string that ends with slash at ${renderContext(c)} but got ${x}`, + ); + } + try { + const url = new URL(x) + return x; + } catch(e) { + if (e instanceof Error) { + throw new DecodingError(e.message) + } else { + throw new DecodingError( + `expected an URL string at ${renderContext(c)} but got "${x}"`, + ); + } + + } + }, + }; +} + /** * Codec that allows any value. */ diff --git a/packages/taler-util/src/taler-types.ts b/packages/taler-util/src/taler-types.ts index e32c5a99d..3b96c52fe 100644 --- a/packages/taler-util/src/taler-types.ts +++ b/packages/taler-util/src/taler-types.ts @@ -38,6 +38,7 @@ import { codecForMap, codecForNumber, codecForString, + codecForStringURL, codecOptional, } from "./codec.js"; import { strcmp } from "./helpers.js"; @@ -2372,7 +2373,7 @@ export interface ExchangeWireAccount { export const codecForExchangeWireAccount = (): Codec => buildCodecForObject() - .property("conversion_url", codecOptional(codecForString())) + .property("conversion_url", codecOptional(codecForStringURL())) .property("credit_restrictions", codecForList(codecForAny())) .property("debit_restrictions", codecForList(codecForAny())) .property("master_sig", codecForString()) -- cgit v1.2.3