Как напечатать ненадежный html blob в Javascript - PullRequest
0 голосов
/ 09 мая 2020

Я загружаю большой двоичный объект из API с типом содержимого text / html, этот большой двоичный объект полностью основан на вводе пользователя и, следовательно, не является доверенным, он может содержать, например, вредоносный сценарий, который захватывает access_token из local_storage и отправляет его в какой-то веб-сервер. Как лучше всего безопасно печатать такой большой двоичный объект?

В настоящее время мой код загружает большой двоичный объект в скрытый изолированный iFrame в песочнице и вызывает print (). Однако идеальная песочница предотвратит вызов print () для содержимого iFrame, поэтому мне пришлось добавить исключения 'allow-same-origin' и 'allow-modals' для песочницы. Но я не уверен, нарушают ли эти исключения безопасность и каким образом:

/**
 * Attaches the given blob in a hidden iFrame and calls print() on
 * that iFrame, and then takes care of cleanup duty afterwards.
 * This function was inspired from MDN: https://mzl.la/2YfOs1v
 */
export function printBlob(blob: Blob): void {
  const url = window.URL.createObjectURL(blob);
  const iframe = document.createElement('iframe');
  iframe.onload = setPrintFactory(url);
  iframe.style.position = 'fixed';
  iframe.style.right = '0';
  iframe.style.bottom = '0';
  iframe.style.width = '0';
  iframe.style.height = '0';
  iframe.style.border = '0';
  iframe.sandbox.add('allow-same-origin');
  iframe.sandbox.add('allow-modals');
  iframe.src = url;
  document.body.appendChild(iframe);
}

function setPrintFactory(url: string): () => void {
  // As soon as the iframe is loaded and ready
  return function() {
    this.contentWindow.__container__ = this;
    this.contentWindow.__url__ = url;
    this.contentWindow.onbeforeunload = closePrint;
    this.contentWindow.onafterprint = closePrint;
    this.contentWindow.focus(); // Required for IE
    this.contentWindow.print();
  };
}

function closePrint() {
  // Cleanup durty once the user closes the print dialog
  document.body.removeChild(this.__container__);
  window.URL.revokeObjectURL(this.__url__);
}

Спасибо за вашу помощь,

...