isomorphic-dompurify icon indicating copy to clipboard operation
isomorphic-dompurify copied to clipboard

Bumping to 0.16.0 - ReferenceError: TextEncoder is not defined

Open danpottshimself opened this issue 3 years ago • 24 comments

When installing - "isomorphic-dompurify": "^0.16.0" I get the following error within a jest test that previously passed.

Error: ReferenceError: TextEncoder is not defined

js file:

import DOMPurify from "isomorphic-dompurify";

return DOMPurify.sanitize(input.trim(), { ADD_ATTR: ["target"] });

Test:

import sanitize from "./sanitizeHtml";

describe("sanitize", () => {
    it("remove js from input", () => {
        expect(sanitize("<img onload='alert(1)' />")).toEqual("<img>");
    });
});

Any ideas what this may be? Thanks in advance

danpottshimself avatar Nov 01 '21 16:11 danpottshimself

Hi @danpottshimself ,

Thanks for reporting the issue.

First of all, let's make sure that you don't have any dependency conflicts by removing node_modules folder and then installing all dependencies again the following way:

npm install
# or 
yarn

kkomelin avatar Nov 01 '21 17:11 kkomelin

By the way, I reformatted the issue description a little bit.

kkomelin avatar Nov 01 '21 17:11 kkomelin

I've just added an additional automated test to cover your particular case and it passed.

kkomelin avatar Nov 01 '21 18:11 kkomelin

@kkomelin odd, I guess this could be some form of clash between dependencies I have installed elsewhere. I have tried multiple node module cleans/rm's and re-installs to no avail. I guess the issue is my side so you can close this issue, thanks for the support

danpottshimself avatar Nov 02 '21 10:11 danpottshimself

I'm also getting this fwiw, so it is slightly broader. Not entirely sure what the conflict is, perhaps one of the dependencies here isn't tightly bound enough so something else is pinning it to an older version?

cnorthwood avatar Nov 02 '21 10:11 cnorthwood

That makes me feel less crazy @cnorthwood. I was originally thinking it could be jsdom environment potentially since in version 16, it looks like jest etc have been bumped?

danpottshimself avatar Nov 02 '21 10:11 danpottshimself

ah yes - that would explain it, and why @kkomelin's automated test passed. Jest with JSDom polyfills that call by itself, so it'll work in a Jest environment because JSDom has polyfilled that function, but won't when not running under Jest.

cnorthwood avatar Nov 02 '21 10:11 cnorthwood

Thanks guys. I will try to reproduce the issue without Jest. Will keep you updated.

kkomelin avatar Nov 02 '21 10:11 kkomelin

Just tested through a console Node.js script (Node 14.16):

require('isomorphic-dompurify');
DOMPurify.setConfig({ALLOWED_TAGS: ['b']});
console.log(DOMPurify.sanitize('<b>dddd</b><iframe>Iframe!</iframe>'));

And it worked fine.

Could you please give me more details on what are your environments so I could try to reproduce the issue locally? Server/client? Next.js/React/others?

kkomelin avatar Nov 02 '21 11:11 kkomelin

By the way, do you use Yarn or NPM for dependency management?

kkomelin avatar Nov 02 '21 11:11 kkomelin

I use Yarn - I've rolled back for now but will try and reproduce.

This is server-side Node 14 (Apollo Server) with TypeScript. I dont' use DOMPurify as a global like that, this is how I use it:

import { addHook, sanitize as _sanitize } from "isomorphic-dompurify";

// only let target="_blank" exist on A tags and enforce rel="noopener"
addHook("afterSanitizeAttributes", (node) => {
  if ("target" in node) {
    if (node.tagName === "A" && node.getAttribute("target") === "_blank") {
      node.setAttribute("rel", "noopener");
    } else {
      node.removeAttribute("target");
    }
  }
});

export const sanitize = (body: string) => _sanitize(body, { ADD_TAGS: ["iframe"], ADD_ATTR: ["target"] });

cnorthwood avatar Nov 02 '21 11:11 cnorthwood

Thanks @cnorthwood Will try to play with your example.

Although I don't think it helps but I've just updated jsdom to the latest minor https://github.com/kkomelin/isomorphic-dompurify/commit/e466c367689d18aa675cbde3cb6c2f9acd95497b . If you want to test the updated dev snapshot, just install it like this yarn add kkomelin/isomorphic-dompurify#master

kkomelin avatar Nov 02 '21 11:11 kkomelin

sadly not. For more info, here's the full stack trace I get:

 FAIL  api/resolvers/Mutation/createArticle.test.ts
  ● Test suite failed to run

    ReferenceError: TextEncoder is not defined

      1 | import { ForbiddenError } from "apollo-server-errors";
    > 2 | import { addHook, sanitize as _sanitize } from "isomorphic-dompurify";
        | ^
      3 | import { Model } from "sequelize/types";
      4 |
      5 | import User from "../../db/models/User";

      at Object.<anonymous> (node_modules/whatwg-url/lib/encoding.js:2:21)
      at Object.<anonymous> (node_modules/whatwg-url/lib/url-state-machine.js:5:34)
      at Object.<anonymous> (node_modules/whatwg-url/lib/URL-impl.js:2:13)
      at Object.<anonymous> (node_modules/whatwg-url/lib/URL.js:442:14)
      at Object.<anonymous> (node_modules/whatwg-url/webidl2js-wrapper.js:3:13)
      at Object.<anonymous> (node_modules/whatwg-url/index.js:3:34)
      at Object.<anonymous> (node_modules/jsdom/lib/api.js:7:19)
      at node_modules/isomorphic-dompurify/index.js:1:278
      at Object.<anonymous> (node_modules/isomorphic-dompurify/index.js:1:344)
      at Object.<anonymous> (api/validators/index.ts:2:1)
      at Object.<anonymous> (db/models/Question.ts:4:1)
      at Object.<anonymous> (db/models/Answer.ts:13:1)
      at Object.<anonymous> (db/createTestAccounts.ts:3:1)
      at Object.<anonymous> (api/resolvers/Mutation/createArticle.test.ts:3:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:401:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

cnorthwood avatar Nov 02 '21 11:11 cnorthwood

so the issue appears to be in whatwg-url

$ yarn why whatwg-url
yarn why v1.22.13
[1/4] 🤔  Why do we have the module "whatwg-url"...?
[2/4] 🚚  Initialising dependency graph...
[3/4] 🔍  Finding dependency...
[4/4] 🚡  Calculating file sizes...
=> Found "[email protected]"
info Has been hoisted to "whatwg-url"
info Reasons this module exists
   - Hoisted from "isomorphic-dompurify#jsdom#whatwg-url"
   - Hoisted from "isomorphic-dompurify#jsdom#data-urls#whatwg-url"
info Disk size without dependencies: "140KB"
info Disk size with unique dependencies: "412KB"
info Disk size with transitive dependencies: "460KB"
info Number of shared dependencies: 3
=> Found "node-fetch#[email protected]"
info This module exists because "node-fetch" depends on it.
info Disk size without dependencies: "76KB"
info Disk size with unique dependencies: "348KB"
info Disk size with transitive dependencies: "396KB"
info Number of shared dependencies: 3
=> Found "jest-environment-jsdom#[email protected]"
info Reasons this module exists
   - "jest-cli#jest-config#jest-environment-jsdom#jsdom" depends on it
   - Hoisted from "jest-cli#jest-config#jest-environment-jsdom#jsdom#whatwg-url"
   - Hoisted from "jest-cli#jest-config#jest-environment-jsdom#jsdom#data-urls#whatwg-url"
info Disk size without dependencies: "136KB"
info Disk size with unique dependencies: "5.28MB"
info Disk size with transitive dependencies: "5.32MB"
info Number of shared dependencies: 4

cnorthwood avatar Nov 02 '21 11:11 cnorthwood

Just for reference https://github.com/jsdom/whatwg-url/issues/209

kkomelin avatar Nov 02 '21 11:11 kkomelin

Ah, so it's a version mismatch with Jest and that breaking it: https://stackoverflow.com/questions/57712235/referenceerror-textencoder-is-not-defined-when-running-react-scripts-test

cnorthwood avatar Nov 02 '21 11:11 cnorthwood

Good catch @cnorthwood ! Now when we know the cause, what solution would you recommend?

kkomelin avatar Nov 02 '21 11:11 kkomelin

Jsdom is a complex project which has already caused some dependency pain https://github.com/kkomelin/isomorphic-dompurify/issues/54

kkomelin avatar Nov 02 '21 11:11 kkomelin

For anyone hitting this same issue, I was able to fix this on a project (which had a failing test due to this error) by including the following in my jest.setup.js file:

import { TextDecoder, TextEncoder } from 'util'

global.TextEncoder = TextEncoder
global.TextDecoder = TextDecoder

tinykite avatar Jan 14 '22 00:01 tinykite

I installed and I'm facing a similar issue. Everything seems fine.

The test suite failed to run

    ReferenceError: TextEncoder is not defined

I tried to print the process version using console.log(process.version) and it's 14.7.0, still giving an error.

What is the right solution?

Node version: 14.7.0

aakashbacancy avatar Dec 15 '22 21:12 aakashbacancy

I've tried

import { TextDecoder, TextEncoder } from 'util'

global.TextEncoder = TextEncoder
global.TextDecoder = TextDecoder

in my jest setup

but I just get this error:

    TypeError: TextEncoder is not a constructor

    > 5 | import DOMPurify from 'isomorphic-dompurify';

any ideas?

edit:

for anybody having this issue, I fixed it by importing from 'node:util' so:

import { TextDecoder, TextEncoder } from 'node:util'

dannyyu92 avatar Jan 03 '23 17:01 dannyyu92

I've tried each one of the solutions provided above but since i'm working with nextJest the tests were failing. I solved it by adding

setupFiles: ["path/to/personalizedJest.setup.js"],

to the personalized configuration for jest, in this file i just imported TextEncoder and TextDecoder from util:

import { TextDecoder, TextEncoder } from 'util';

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

Vavaste avatar Jan 17 '23 11:01 Vavaste

For me, I had to add the following to jest.setup.js

import { TextDecoder, TextEncoder } from 'util';

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

and import the lib like import * as dompurify from 'isomorphic-dompurify';

donald-boosted avatar May 31 '23 14:05 donald-boosted

For me, the TextDecoder had a type mismatch issue with the above approach, so I solved it with this syntax instead:

import { TextEncoder, TextDecoder } from 'util';

Object.assign(global, { TextDecoder, TextEncoder });

Thanks to leonheess - https://stackoverflow.com/questions/68468203/why-am-i-getting-textencoder-is-not-defined-in-jest

stevedeighton avatar Jun 29 '23 08:06 stevedeighton