aboutsummaryrefslogtreecommitdiff
path: root/packages/web-util/src/utils/http-impl.sw.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/web-util/src/utils/http-impl.sw.ts')
-rw-r--r--packages/web-util/src/utils/http-impl.sw.ts19
1 files changed, 18 insertions, 1 deletions
diff --git a/packages/web-util/src/utils/http-impl.sw.ts b/packages/web-util/src/utils/http-impl.sw.ts
index 3c269e695..2ae4ccd86 100644
--- a/packages/web-util/src/utils/http-impl.sw.ts
+++ b/packages/web-util/src/utils/http-impl.sw.ts
@@ -27,6 +27,7 @@ import {
import {
DEFAULT_REQUEST_TIMEOUT_MS,
Headers,
+ HttpLibArgs,
HttpRequestLibrary,
HttpRequestOptions,
HttpResponse,
@@ -41,6 +42,12 @@ import {
export class ServiceWorkerHttpLib implements HttpRequestLibrary {
private throttle = new RequestThrottler();
private throttlingEnabled = true;
+ private requireTls = false;
+
+ public constructor(args?: HttpLibArgs) {
+ this.throttlingEnabled = args?.enableThrottling ?? true;
+ this.requireTls = args?.requireTls ?? false;
+ }
async fetch(
requestUrl: string,
@@ -52,8 +59,8 @@ export class ServiceWorkerHttpLib 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,
{
@@ -64,6 +71,16 @@ export class ServiceWorkerHttpLib 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" ? encodeBody(requestBody) : undefined;