badwords icon indicating copy to clipboard operation
badwords copied to clipboard

Don't crash w/strings with no word boundaries

Open brianreavis opened this issue 4 years ago • 11 comments

Fixes https://github.com/web-mech/badwords/issues/93

TypeError: Cannot read property '0' of null

brianreavis avatar Dec 04 '20 20:12 brianreavis

can this get merged? it seems like a huge oversight to crash with a wordless string.

aksperiod avatar Jan 27 '21 20:01 aksperiod

Any news on this PR when it can be merged?

fenix20113 avatar Sep 06 '21 13:09 fenix20113

Any updates?

heartz66 avatar Oct 20 '21 13:10 heartz66

I would just like to place another vote to merge this PR. I would love to use this package in production but can't due to this error.

StanFlint avatar Dec 13 '21 14:12 StanFlint

I would just like to place another vote to merge this PR. I would love to use this package in production but can't due to this error.

I yoinked some code from the changes in this pr, but I just overrode the clean method to fit my needs. Kinda a pain in the behind if you have multiple filters but guess this is all we can do unless we want to fork and make our own... or wait.

let customFilter = new Filter({
    placeHolder: "*",
});
customFilter.clean = (string) => {
    try {
        const splitMap = string.split(ProfaneFilter.splitRegex).map((word) => {
            return ProfaneFilter.isProfane(word) ? ProfaneFilter.replaceWord(word) : word;
        });
        if (splitMap.length < 2) return string;
        return splitMap.join(ProfaneFilter.splitRegex.exec(string)[0]);
    } catch (e) {
        return string;
    }
};
customFilter.clean("you are a meanie");

IsaiahPapa avatar Jan 05 '22 20:01 IsaiahPapa

Would love this PR to be merged to prevent crashing on strings like :)

iplanwebsites avatar Apr 15 '22 22:04 iplanwebsites

or like this ...

import BaseBadWordsFilter from 'bad-words';

class BadWordsFilter extends BaseBadWordsFilter {
  private readonly splitRegex!: RegExp;

  clean(string: string): string {
    try {
      const joinMatch = this.splitRegex.exec(string);
      const joinString = joinMatch?.[0] || '';
      return string
        .split(this.splitRegex)
        .map(word => {
          return this.isProfane(word) ? this.replaceWord(word) : word;
        })
        .join(joinString);
    } catch (e) {
      return string;
    }
  }
}

spravo avatar Jun 07 '22 11:06 spravo

This really needs to be addressed the Library is poor without it

UberMC avatar Feb 20 '23 08:02 UberMC

thanks for the fix

skyelynwaddell avatar Apr 17 '24 22:04 skyelynwaddell