Test Watched Words is Broken

When using the “test” feature in the watched words configuration screen, the error “raw bracket is not allowed in regular expression with unicode flag” is repeatedly printed to the console.

  1. Go to https://meta.discourse.org/admin/customize/watched_words/action/require_approval
  2. Open the browser console
  3. Press “Test”
  4. Enter anything
  5. Watch errors

The error comes from watched-words.js, and it’s because the RegEx being passed in is (?:[^[:word:]]|^)(word1|word2)(?=[^[:word:]]|$), which is valid for Ruby, but not for JavaScript. The RegEx originates from an API, https://meta.discourse.org/admin/customize/watched_words.json. Which gets its JSON from the watched words serializer, which then calls word watcher with only action passed in.

A few weeks ago that would’ve been fine, but a PR introduced a change that added two more extra parameters. One of these parameters was engine, engine is a parameter which defaults to Ruby, and it is not specified by the aforementioned call. So the returned RegEx is ruby-specific but it is consumed by the JavaScript RegEx engine

5 Likes

I can replicate this on my test site.

  • Set up a blocked Watched Word
  • Use the test function to try it out
  • Get this error in console:
Uncaught SyntaxError: raw bracket is not allowed in regular expression with unicode flag
    createWatchedWordRegExp watched-words.js:12
    matches admin-watched-word-test.js:62
    matches admin-watched-word-test.js:61
    o handle-descriptor.js:61
    get Ember
    untrack validator.js:681
    Ember 3
    o reference.js:175
    tag reference.js:136
    track validator.js:668
    f reference.js:135
    <anonymous> runtime.js:2077
    tag reference.js:136
    track validator.js:668
    f reference.js:135
    evaluate runtime.js:2089
    _execute runtime.js:4306
    execute runtime.js:4291
    rerender runtime.js:4606
    Ember 2
    It runtime.js:4139
    Ember 3
    invoke queue.ts:201
    flush queue.ts:98
    flush deferred-action-queues.ts:75
    _end index.ts:616
    end index.ts:298
    _runExpiredTimers index.ts:739
    setTimeout handler*setTimeout platform.ts:50
    _installTimerTimeout index.ts:783
    _scheduleExpiredTimers index.ts:764
    _runExpiredTimers index.ts:738
watched-words.js:12

Thanks for the detailed report and I’ll see if we can get someone to take a look. :slight_smile: :+1:

5 Likes

Thanks for detailed report @Arthri

This has been fixed.

3 Likes