# Watched word 正则表达式崩溃 (2025)

**URL:** https://meta.discourse.org/t/watched-word-regular-expression-crash-2025/382417
**Category:** Bug
**Tags:** regex, watched-words
**Created:** [2025 年9 月 12 日 09:11 UTC](https://meta.discourse.org/t/watched-word-regular-expression-crash-2025/382417 "2025-09-12T09:11:45Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2025 年9 月 12 日 09:11 UTC](https://meta.discourse.org/t/watched-word-regular-expression-crash-2025/382417/1 "2025-09-12T09:11:45Z")

</div>

与[此问题](https://meta.discourse.org/t/watched-word-regular-expression-crash/275701)类似

### 重现步骤

- 启用了监视词正则表达式
- 监视词标签操作
- 正则表达式 `*abc`

### 结果

无法创建主题或帖子

```plaintext
RegexpError (重复运算符的目标未指定：/(*abc)/i)
app/services/word_watcher.rb:239:in `initialize'
app/services/word_watcher.rb:239:in `new'
app/services/word_watcher.rb:239:in `word_matches?'
lib/topic_creator.rb:214:in `block in setup_tags'
lib/topic_creator.rb:213:in `each'
lib/topic_creator.rb:213:in `setup_tags'
lib/topic_creator.rb:48:in `create'
lib/post_creator.rb:493:in `create_topic'

```

### 原因

- `*` 表示“将前一个标记重复零次或多次”
- 没有前一个标记

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2025 年9 月 12 日 11:18 UTC](https://meta.discourse.org/t/watched-word-regular-expression-crash-2025/382417/2 "2025-09-12T11:18:18Z")

</div>

所以正则表达式在被允许添加之前应该被验证？并且应该有一个救援，这样无效的正则表达式就不会导致整个世界崩溃？

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2025 年9 月 12 日 23:03 UTC](https://meta.discourse.org/t/watched-word-regular-expression-crash-2025/382417/3 "2025-09-12T23:03:37Z")

</div>

> [@pfaffman](#):
>
> 应在允许添加正则表达式之前对其进行验证

这里明显发生的情况是，监视的单词是常规的监视单词，而 \* 是通配符，然后启用了“watched words regular expressions enabled”。

所以这个

> [@pfaffman](#):
>
> 应该有一个救援机制，这样无效的正则表达式就不会导致整个系统崩溃

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2025 年9 月 12 日 23:16 UTC](https://meta.discourse.org/t/watched-word-regular-expression-crash-2025/382417/4 "2025-09-12T23:16:51Z")

</div>

> [@RGJ](#):
>
> 这里显然发生的是，被监视的词是常规被监视的词，而\*是通配符，然后启用了“启用被监视词正则表达式”。

嗯， **现在** 你这么说了就显而易见了。:rofl:

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2025 年9 月 15 日 00:19 UTC](https://meta.discourse.org/t/watched-word-regular-expression-crash-2025/382417/5 "2025-09-15T00:19:22Z")

</div>

在保存时验证正则表达式似乎是一个非常明智的做法。

对此添加 #pr-welcome 标签，但团队将进行分类并决定在未来几周内是否可以这样做，以节省我们未来的支持。

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2025 年9 月 15 日 08:17 UTC](https://meta.discourse.org/t/watched-word-regular-expression-crash-2025/382417/6 "2025-09-15T08:17:27Z")

</div>

> [@sam](#):
>
> 在保存时验证正则表达式

这并不能阻止人们输入“普通”通配符表达式，_然后_启用 `watched words regular expressions enabled`，而这正是这里发生的情况。

我认为只需要在 regexp 调用 [周围](https://github.com/discourse/discourse/blob/main/app/services/word_watcher.rb#L239) 添加一个异常处理程序

```rb
  def word_matches?(word, case_sensitive: false)
    options = case_sensitive ? nil : Regexp::IGNORECASE
    Regexp.new(WordWatcher.word_to_regexp(word), options).match?(@raw)
  end

```

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [2025 年10 月 6 日 17:25 UTC](https://meta.discourse.org/t/watched-word-regular-expression-crash-2025/382417/7 "2025-10-06T17:25:19Z")

</div>

感谢您的报告@RGJ，此问题将在以下链接中修复：

> <https://github.com/discourse/discourse/pull/35217>
>
> If you have a watched word with a wildcard and then enable the "watched words re…gexp" site setting, you might end up in a situation where you can't post anymore because of the invalid regexp.
> 
> This ensures we correctly skip invalid regexps.
> 
> Ref - https://meta.discourse.org/t/-/382417

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [2025 年10 月 7 日 06:00 UTC](https://meta.discourse.org/t/watched-word-regular-expression-crash-2025/382417/9 "2025-10-07T06:00:11Z")

</div>

该主题已在 10 小时后自动关闭。不再允许回复。
