summaryrefslogtreecommitdiff
path: root/packages/web-util/src/utils/http-impl.browser.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/web-util/src/utils/http-impl.browser.ts')
-rw-r--r--packages/web-util/src/utils/http-impl.browser.ts19
1 files changed, 18 insertions, 1 deletions
diff --git a/packages/web-util/src/utils/http-impl.browser.ts b/packages/web-util/src/utils/http-impl.browser.ts
index 18140ef13..5d65c3903 100644
--- a/packages/web-util/src/utils/http-impl.browser.ts
+++ b/packages/web-util/src/utils/http-impl.browser.ts
@@ -33,6 +33,7 @@ import {
getDefaultHeaders,
encodeBody,
DEFAULT_REQUEST_TIMEOUT_MS,
+ HttpLibArgs,
} from "@gnu-taler/taler-util/http";
const logger = new Logger("browserHttpLib");
@@ -44,6 +45,12 @@ const logger = new Logger("browserHttpLib");
export class BrowserHttpLib implements HttpRequestLibrary {
private throttle = new RequestThrottler();
private throttlingEnabled = true;
+ private requireTls = false;
+
+ constructor(args?: HttpLibArgs) {
+ this.throttlingEnabled = args?.enableThrottling ?? true;
+ this.requireTls = args?.requireTls ?? false;
+ }
fetch(
requestUrl: string,
@@ -55,8 +62,8 @@ export class BrowserHttpLib implements HttpRequestLibrary {
const requestTimeout =
options?.timeout ?? Duration.fromMilliseconds(DEFAULT_REQUEST_TIMEOUT_MS);
+ const parsedUrl = new URL(requestUrl);
if (this.throttlingEnabled && this.throttle.applyThrottle(requestUrl)) {
- const parsedUrl = new URL(requestUrl);
throw TalerError.fromDetail(
TalerErrorCode.WALLET_HTTP_REQUEST_THROTTLED,
{
@@ -67,6 +74,16 @@ export class BrowserHttpLib implements HttpRequestLibrary {
`request to origin ${parsedUrl.origin} was throttled`,
);
}
+ if (this.requireTls && parsedUrl.protocol !== "https:") {
+ throw TalerError.fromDetail(
+ TalerErrorCode.WALLET_NETWORK_ERROR,
+ {
+ requestMethod: requestMethod,
+ requestUrl: requestUrl,
+ },
+ `request to ${parsedUrl.origin} is not possible with protocol ${parsedUrl.protocol}`,
+ );
+ }
let myBody: ArrayBuffer | undefined =
requestMethod === "POST" || requestMethod === "PUT" || requestMethod === "PATCH"