kkomelin/isomorphic-dompurify

Bumping to 0.16.0 - ReferenceError: TextEncoder is not defined

danpottshimself opened this issue ยท 26 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

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

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

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

@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

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?

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?

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.

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

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?

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

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"] });

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 e466c36 . If you want to test the updated dev snapshot, just install it like this yarn add kkomelin/isomorphic-dompurify#master

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)

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 "whatwg-url@10.0.0"
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#whatwg-url@5.0.0"
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#whatwg-url@8.7.0"
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

Just for reference jsdom/whatwg-url#209

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

Jsdom is a complex project which has already caused some dependency pain #54

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

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

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'

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;

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';

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

Not reproducible for me anymore in 2.13.0
I guess, issue can be closed

@kopach Thanks for the update. Yes, it's a very old issue, so let's close it for now.