summaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/taler-wallet-interaction-loader.ts
blob: 3b7cbcbb7f770a13a483ee635a8cff5c2efef757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 This file is part of GNU Taler
 (C) 2022 Taler Systems S.A.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

import { CoreApiResponse, TalerError, TalerErrorCode } from "@gnu-taler/taler-util";
import type { MessageFromBackend } from "./platform/api.js";

/**
 * This will modify all the pages that the user load when navigating with Web Extension enabled
 *
 * Can't do useful integration since it run in ISOLATED (or equivalent) mode.
 *
 * If taler support is expected, it will inject a script which will complete the integration.
 */

// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Chrome_incompatibilities#content_script_environment

// ISOLATED mode in chromium browsers
// https://chromium.googlesource.com/chromium/src/+/master/third_party/blink/renderer/bindings/core/v8/V8BindingDesign.md#world
// X-Ray vision in Firefox
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#xray_vision_in_firefox

// *** IMPORTANT ***

// Content script lifecycle during navigation
// In Firefox: Content scripts remain injected in a web page after the user has navigated away,
// however, window object properties are destroyed.
// In Chrome: Content scripts are destroyed when the user navigates away from a web page.

const documentDocTypeIsHTML =
  window.document.doctype && window.document.doctype.name === "html";
const suffixIsNotXMLorPDF =
  !window.location.pathname.endsWith(".xml") &&
  !window.location.pathname.endsWith(".pdf");
const rootElementIsHTML =
  document.documentElement.nodeName &&
  document.documentElement.nodeName.toLowerCase() === "html";
// const pageAcceptsTalerSupport = document.head.querySelector(
//   "meta[name=taler-support]",
// );



function validateTalerUri(uri: string): boolean {
  return (
    !!uri && (uri.startsWith("taler://") || uri.startsWith("taler+http://"))
  );
}

function convertURIToWebExtensionPath(uri: string) {
  const url = new URL(
    chrome.runtime.getURL(`static/wallet.html#/taler-uri/${encodeURIComponent(uri)}`),
  );
  return url.href;
}

// safe check, if one of this is true then taler handler is not useful
// or not expected
const shouldNotInject =
  !documentDocTypeIsHTML ||
  !suffixIsNotXMLorPDF ||
  // !pageAcceptsTalerSupport ||
  !rootElementIsHTML;

const logger = {
  debug: (...msg: any[]) => { },
  info: (...msg: any[]) =>
    console.log(`${new Date().toISOString()} TALER`, ...msg),
  error: (...msg: any[]) =>
    console.error(`${new Date().toISOString()} TALER`, ...msg),
};

// logger.debug = logger.info

/**
 */
function redirectToTalerActionHandler(element: HTMLMetaElement) {
  const name = element.getAttribute("name")
  if (!name) return;
  if (name !== "taler-uri") return;
  const uri = element.getAttribute("content");
  if (!uri) return;

  if (!validateTalerUri(uri)) {
    logger.error(`taler:// URI is invalid: ${uri}`);
    return;
  }

  const walletPage = convertURIToWebExtensionPath(uri)
  window.location.replace(walletPage)
}

function injectTalerSupportScript(head: HTMLHeadElement, trusted: boolean) {
  const meta = head.querySelector("meta[name=taler-support]")
  if (!meta) return;
  const content = meta.getAttribute("content");
  if (!content) return;
  const features = content.split(",")

  const debugEnabled = meta.getAttribute("debug") === "true";
  const hijackEnabled = features.indexOf("uri") !== -1
  const talerApiEnabled = features.indexOf("api") !== -1 && trusted

  const scriptTag = document.createElement("script");
  scriptTag.setAttribute("async", "false");
  const url = new URL(
    chrome.runtime.getURL("/dist/taler-wallet-interaction-support.js"),
  );
  url.searchParams.set("id", chrome.runtime.id);
  if (debugEnabled) {
    url.searchParams.set("debug", "true");
  }
  if (talerApiEnabled) {
    url.searchParams.set("api", "true");
  }
  if (hijackEnabled) {
    url.searchParams.set("hijack", "true");
  }
  scriptTag.src = url.href;

  try {
    head.insertBefore(scriptTag, head.children.length ? head.children[0] : null);
  } catch (e) {
    logger.info("inserting link handler failed!");
    logger.error(e);
  }
}


export interface ExtensionOperations {
  isAutoOpenEnabled: {
    request: void;
    response: boolean;
  };
  isDomainTrusted: {
    request: {
      domain: string;
    };
    response: boolean;
  };
}

export type MessageFromExtension<Op extends keyof ExtensionOperations> = {
  channel: "extension";
  operation: Op;
  payload: ExtensionOperations[Op]["request"];
};

export type MessageResponse = CoreApiResponse;

async function callBackground<Op extends keyof ExtensionOperations>(
  operation: Op,
  payload: ExtensionOperations[Op]["request"],
): Promise<ExtensionOperations[Op]["response"]> {
  const message: MessageFromExtension<Op> = {
    channel: "extension",
    operation,
    payload,
  };

  const response = await sendMessageToBackground(message);
  if (response.type === "error") {
    throw new Error(`Background operation "${operation}" failed`);
  }
  return response.result as any;
}


let nextMessageIndex = 0;
/**
 * 
 * @param message 
 * @returns 
 */
async function sendMessageToBackground<Op extends keyof ExtensionOperations>(
  message: MessageFromExtension<Op>,
): Promise<MessageResponse> {
  const messageWithId = { ...message, id: `id_${nextMessageIndex++ % 1000}` };

  if (!chrome.runtime.id) {
    return Promise.reject(TalerError.fromDetail(TalerErrorCode.WALLET_CORE_NOT_AVAILABLE, {}))
  }
  return new Promise<any>((resolve, reject) => {
    logger.debug("send operation to the wallet background", message, chrome.runtime.id);
    let timedout = false;
    const timerId = setTimeout(() => {
      timedout = true;
      reject(TalerError.fromDetail(TalerErrorCode.GENERIC_TIMEOUT, {
        requestMethod: "wallet",
        requestUrl: message.operation,
        timeoutMs: 20 * 1000,
      }))
    }, 20 * 1000); //five seconds
    try {
      chrome.runtime.sendMessage(messageWithId, (backgroundResponse) => {
        if (timedout) {
          return false; //already rejected
        }
        clearTimeout(timerId);
        if (chrome.runtime.lastError) {
          reject(chrome.runtime.lastError.message);
        } else {
          resolve(backgroundResponse);
        }
        // return true to keep the channel open
        return true;
      });
    } catch (e) {
      console.log(e)
    }
  });
}

let notificationPort: chrome.runtime.Port | undefined;
function listenToWalletBackground(listener: (m: any) => void): () => void {
  if (notificationPort === undefined) {
    notificationPort = chrome.runtime.connect({ name: "notifications" });
  }
  notificationPort.onMessage.addListener(listener);
  function removeListener(): void {
    if (notificationPort !== undefined) {
      notificationPort.onMessage.removeListener(listener);
    }
  }
  return removeListener;
}

const loaderSettings = {
  isAutoOpenEnabled: false,
  isDomainTrusted: false,
}

function start(
  onTalerMetaTagFound: (listener: (el: HTMLMetaElement) => void) => void,
  onHeadReady: (listener: (el: HTMLHeadElement) => void) => void
) {
  // do not run everywhere, this is just expected to run on site
  // that are aware of taler
  if (shouldNotInject) return;

  const isAutoOpenEnabled_promise = callBackground("isAutoOpenEnabled", undefined).then(result => {
    loaderSettings.isAutoOpenEnabled = result;
    return result;
  })
  const isDomainTrusted_promise = callBackground("isDomainTrusted", {
    domain: window.location.origin
  }).then(result => {
    loaderSettings.isDomainTrusted = result;
    return result;
  })

  onTalerMetaTagFound(async (el) => {
    await isAutoOpenEnabled_promise;
    if (!loaderSettings.isAutoOpenEnabled) {
      return;
    }
    redirectToTalerActionHandler(el)
  })

  onHeadReady(async (el) => {
    const trusted = await isDomainTrusted_promise
    injectTalerSupportScript(el, trusted)
  })

  listenToWalletBackground((e: MessageFromBackend) => {
    if (e.type === "web-extension" && e.notification.type === "settings-change") {
      const settings = e.notification.currentValue
      loaderSettings.isAutoOpenEnabled = settings.autoOpen
    }
  })

}

function isCorrectMetaElement(el: HTMLMetaElement): boolean {
  const name = el.getAttribute("name")
  if (!name) return false;
  if (name !== "taler-uri") return false;
  const uri = el.getAttribute("content");
  if (!uri) return false;
  return true
}

/**
 * Tries to find taler meta tag ASAP and report
 * @param notify 
 * @returns 
 */
function notifyWhenTalerUriIsFound(notify: (el: HTMLMetaElement) => void) {
  if (document.head) {
    const element = document.head.querySelector("meta[name=taler-uri]")
    if (!element) return;
    if (!(element instanceof HTMLMetaElement)) return;

    if (isCorrectMetaElement(element)) {
      notify(element)
    }
    return;
  }
  const obs = new MutationObserver(async function (mutations) {
    try {
      mutations.forEach((mut) => {
        if (mut.type === "childList") {
          mut.addedNodes.forEach((added) => {
            if (added instanceof HTMLMetaElement) {
              if (isCorrectMetaElement(added)) {
                notify(added)
                obs.disconnect()
              }
            }
          });
        }
      });
    } catch (e) {
      console.error(e)
    }
  })

  obs.observe(document, {
    childList: true,
    subtree: true,
    attributes: false,
  })

}

/**
 * Tries to find HEAD tag ASAP and report
 * @param notify 
 * @returns 
 */
function notifyWhenHeadIsFound(notify: (el: HTMLHeadElement) => void) {
  if (document.head) {
    notify(document.head)
    return;
  }
  const obs = new MutationObserver(async function (mutations) {
    try {
      mutations.forEach((mut) => {
        if (mut.type === "childList") {
          mut.addedNodes.forEach((added) => {
            if (added instanceof HTMLHeadElement) {
              notify(added)
              obs.disconnect()
            }
          });
        }
      });
    } catch (e) {
      console.error(e)
    }
  })

  obs.observe(document, {
    childList: true,
    subtree: true,
    attributes: false,
  })
}

start(notifyWhenTalerUriIsFound, notifyWhenHeadIsFound);