Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

センシティブワードを正規表現、CWにも適用するように #10688

Merged
merged 18 commits into from
May 10, 2023

Conversation

nenohi
Copy link
Contributor

@nenohi nenohi commented Apr 22, 2023

What

CWにてセンシティブワードが含まれていた場合にホーム投稿とします。
また、センシティブワードを正規表現で指定できるようにしました。

Why

注釈を悪用するユーザーが懸念されるため

Additional info (optional)

ローカルにてテスト済み

Checklist

  • Read the contribution guide
  • Test working in a local environment
  • (If needed) Add story of storybook
  • (If needed) Update CHANGELOG.md
  • (If possible) Add tests

@nenohi nenohi marked this pull request as ready for review April 22, 2023 06:03
@github-actions github-actions bot added the packages/backend Server side specific issue/PR label Apr 22, 2023
@codecov
Copy link

codecov bot commented Apr 22, 2023

Codecov Report

Merging #10688 (5ecf9d3) into develop (1f9f63d) will increase coverage by 0.94%.
The diff coverage is n/a.

@@             Coverage Diff             @@
##           develop   #10688      +/-   ##
===========================================
+ Coverage    77.37%   78.31%   +0.94%     
===========================================
  Files          899      168     -731     
  Lines        89419    21023   -68396     
  Branches      6532      375    -6157     
===========================================
- Hits         69184    16465   -52719     
+ Misses       20235     4558   -15677     

see 741 files with indirect coverage changes

@nenohi
Copy link
Contributor Author

nenohi commented Apr 22, 2023

ちょっと正規表現のテストもしたい

@nenohi
Copy link
Contributor Author

nenohi commented Apr 22, 2023

正規表現めんどくさいかも、とりあえずこれでよさそう
(うんこがセンシティブワードに登録されている場合下記)
image

@nenohi
Copy link
Contributor Author

nenohi commented Apr 24, 2023

/test/gにも対応したい

@nenohi
Copy link
Contributor Author

nenohi commented Apr 24, 2023

対応した
image

@nenohi
Copy link
Contributor Author

nenohi commented Apr 24, 2023

なんかテスト通らなくなったかも?

@nenohi nenohi changed the title cwにセンシティブが効いてない センシティブワードを正規表現、CWにも適用するように Apr 24, 2023
@nenohi
Copy link
Contributor Author

nenohi commented Apr 24, 2023

#10701 これもクローズできるかも?(問題なければ

@syuilo
Copy link
Member

syuilo commented Apr 24, 2023

正規表現要る?

@u1-liquid
Copy link
Sponsor Contributor

少なくともワードバウンダリ(\b)はほしいかも

@acid-chicken
Copy link
Member

まんま ReDoS になりそう

@nenohi
Copy link
Contributor Author

nenohi commented Apr 24, 2023

ReDOSのこと考えてなかったかも

@nenohi
Copy link
Contributor Author

nenohi commented Apr 24, 2023

正規表現使うかどうかオプションにする?

@nenohi
Copy link
Contributor Author

nenohi commented Apr 24, 2023

これにすれば/test/系のみが正規表現になって良さそう?

@nenohi
Copy link
Contributor Author

nenohi commented Apr 24, 2023

正規表現要る?

1つの単語じゃなく、2つの単語が含まれていた場合とかで必要かも

@yuriha-chan
Copy link
Contributor

CWで隠された内容がセンシティブであるのにも関わらず、それ以外の理由でCWにしているという誤解を与えるような投稿をしていることを懸念しているのであれば、「(※この投稿はセンシティブな語句を含んでいます)」などの文言を注釈に自動で挿入すれば事足りると思われるのですがどうでしょうか。

@nenohi
Copy link
Contributor Author

nenohi commented Apr 24, 2023

CWに関して懸念しているものはCWを付けているにも関わらず注釈にてセンシティブな語句が含まれていた場合にホームに投稿されないという事象があります。
これはバグに値すると思っています

@yuriha-chan
Copy link
Contributor

なるほど理解しました。
ありがとうございます。

packages/backend/src/core/NoteCreateService.ts Outdated Show resolved Hide resolved
packages/backend/src/core/NoteCreateService.ts Outdated Show resolved Hide resolved
packages/backend/src/core/NoteCreateService.ts Outdated Show resolved Hide resolved
packages/backend/src/core/NoteCreateService.ts Outdated Show resolved Hide resolved
packages/backend/src/core/NoteCreateService.ts Outdated Show resolved Hide resolved
packages/backend/src/core/NoteCreateService.ts Outdated Show resolved Hide resolved
packages/backend/src/core/NoteCreateService.ts Outdated Show resolved Hide resolved
packages/backend/src/core/NoteCreateService.ts Outdated Show resolved Hide resolved
packages/backend/src/core/NoteCreateService.ts Outdated Show resolved Hide resolved
@acid-chicken
Copy link
Member

#10715 で調査した結果 RE2 であってもリストが大きいとめちゃくちゃ遅くなることがわかったので正規表現やりたくなさゲージが上昇した

@acid-chicken
Copy link
Member

というかよく考えたら misc/check-word-mute と共通化してよくね

@nenohi
Copy link
Contributor Author

nenohi commented Apr 26, 2023

たしかに

@nenohi
Copy link
Contributor Author

nenohi commented Apr 27, 2023

これ型が違うからマイグレーションが必要かも?
ワードミュート→jsonb
センシティブワード→varchar

@acid-chicken
Copy link
Member

というかよく考えたら misc/check-word-mute と共通化してよくね

センシティブワードの場合、CWにセンシティブのものが入っておらず

textにセンシティブワードが入っていた場合はスルーにするべきだと思うので

共通化と言うよりは抜き出しのほうがいい感じしそう

について

センシティブワードの場合、CWにセンシティブのものが入っておらず

textにセンシティブワードが入っていた場合はスルーにするべきだと思うので

が理由になっているのがよく分からない

@nenohi
Copy link
Contributor Author

nenohi commented May 6, 2023

というかよく考えたら misc/check-word-mute と共通化してよくね

センシティブワードの場合、CWにセンシティブのものが入っておらず
textにセンシティブワードが入っていた場合はスルーにするべきだと思うので
共通化と言うよりは抜き出しのほうがいい感じしそう

センシティブワードの場合、CWにセンシティブのものが入っておらず
textにセンシティブワードが入っていた場合はスルーにするべきだと思うので

共通化した場合、センシティブワードに含まれているものでCWにしているものでも
強制的にホームになってしまって理不尽を感じるのでは?ということです

@nenohi
Copy link
Contributor Author

nenohi commented May 6, 2023

センシティブワードに含まれていてもCWを使用して注意を促すような場合はOKにしたいというお気持ち

@acid-chicken
Copy link
Member

string を引数に取っているに過ぎない(note を表すオブジェクトを取っていない)ので呼び出し元で如何様にもできるのでは?

@acid-chicken
Copy link
Member

仮にそうなっていなかったとしてもそうなるようにリファクタすれば良い

@nenohi
Copy link
Contributor Author

nenohi commented May 6, 2023

note取るか

@nenohi
Copy link
Contributor Author

nenohi commented May 9, 2023

noteからにした

@syuilo syuilo merged commit c15b75e into misskey-dev:develop May 10, 2023
@syuilo
Copy link
Member

syuilo commented May 10, 2023

👍🏻

@acid-chicken
Copy link
Member

引数が変わっただけで共通化はされてなくないか

@bindThis
private isSensitive(note: Option, sensitiveWord: string[]): boolean {
if (sensitiveWord.length > 0) {
const text = note.cw ?? note.text ?? '';
if (text === '') return false;
const matched = sensitiveWord.some(filter => {
// represents RegExp
const regexp = filter.match(/^\/(.+)\/(.*)$/);
// This should never happen due to input sanitisation.
if (!regexp) {
const words = filter.split(' ');
return words.every(keyword => text.includes(keyword));
}
try {
return new RE2(regexp[1], regexp[2]).test(text);
} catch (err) {
// This should never happen due to input sanitisation.
return false;
}
});
if (matched) return true;
}
return false;
}

export async function checkWordMute(note: NoteLike, me: UserLike | null | undefined, mutedWords: Array<string | string[]>): Promise<boolean> {
// 自分自身
if (me && (note.userId === me.id)) return false;
if (mutedWords.length > 0) {
const text = ((note.cw ?? '') + '\n' + (note.text ?? '')).trim();
if (text === '') return false;
const acable = mutedWords.filter(filter => Array.isArray(filter) && filter.length === 1).map(filter => filter[0]).sort();
const unacable = mutedWords.filter(filter => !Array.isArray(filter) || filter.length !== 1);
const acCacheKey = acable.join('\n');
const ac = acCache.get(acCacheKey) ?? AhoCorasick.withPatterns(acable);
acCache.delete(acCacheKey);
for (const obsoleteKeys of acCache.keys()) {
if (acCache.size > 1000) {
acCache.delete(obsoleteKeys);
}
}
acCache.set(acCacheKey, ac);
if (ac.isMatch(text)) {
return true;
}
const matched = unacable.some(filter => {
if (Array.isArray(filter)) {
return filter.every(keyword => text.includes(keyword));
} else {
// represents RegExp
const regexp = filter.match(/^\/(.+)\/(.*)$/);
// This should never happen due to input sanitisation.
if (!regexp) return false;
try {
return new RE2(regexp[1], regexp[2]).test(text);
} catch (err) {
// This should never happen due to input sanitisation.
return false;
}
}
});
if (matched) return true;
}
return false;
}

このロジックが別々に存在しているのが嫌

@nenohi
Copy link
Contributor Author

nenohi commented May 11, 2023

共通化するにも型が別々なので難しいのでは・・・?

ワードミュート→jsonb
センシティブワード→varchar

@acid-chicken
Copy link
Member

なぜ DB の型が出てくるのか分からないが

JSの力を使えばそんなに難しくはない

@nenohi
Copy link
Contributor Author

nenohi commented May 11, 2023

こういう感じ・・・?
https://github.com/nenohi/misskey/tree/fix-regsensitive

@acid-chicken
Copy link
Member

こういう感じ・・・? https://github.com/nenohi/misskey/tree/fix-regsensitive

概ねそうだけど正規表現の判定を最初にやる必要がある→正規表現ならそのまま、違うなら配列に包む

@acid-chicken
Copy link
Member

ただこれはミュート側の方を変更する余地大有りかも

@nenohi
Copy link
Contributor Author

nenohi commented May 11, 2023

ミュート側はuserId: Note['userId'];が必須でセンシティブワード側にはないからという認識でいい?

@acid-chicken
Copy link
Member

acid-chicken commented May 11, 2023

ミュート側はuserId: Note['userId'];が必須でセンシティブワード側にはないからという認識でいい?

いや単純に (string | string)[] という引数がインターフェースとして意味がわからないため(配列なら文字そのまま、そうでないなら正規表現、などといった仕様になっていることを型から推察するのは困難すぎる)

syuilo added a commit that referenced this pull request May 12, 2023
* refactor(frontend): use css modules

* feat: 投稿したコンテンツのAIによる学習を軽減するオプションを追加

Resolve #10819

* enhance(backend): publicReactionsをデフォルトtrueに

* 念のためnoimageaiもつける

* add X-Robots-Tag: noai

* Update ja-JP.yml

* fix(frontend): ブラーエフェクトを有効にしている状態で高負荷になる問題を修正

* enhance(backend): graceful shutdown for job queue and refactor

* fix(backend): テスト時は一部のサービスを停止

* fix test

* New Crowdin updates (#10815)

* New translations ja-JP.yml (English)

* New translations ja-JP.yml (German)

* New translations ja-JP.yml (Korean)

* New translations ja-JP.yml (Chinese Traditional)

* New translations ja-JP.yml (Chinese Traditional)

* refactor

* bump

* refactor(frontend): use css module

* refactor(frontend): use css module

* delete unused component

* センシティブワードを正規表現、CWにも適用するように (#10688)

* cwにセンシティブが効いてない

* CWが無いときにTextを見るように

* 比較演算子間違えた

* とりあえずチェック

* 正規表現対応

* /test/giにも対応

* matchでしなくてもいいのでは感

* レビュー修正

* Update packages/backend/src/core/NoteCreateService.ts

Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>

* Update packages/backend/src/core/NoteCreateService.ts

Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>

* 修正

* wipかも

* wordsでスペース区切りのものできたかも

* なんか動いたかも

* test作成

* 文言の修正

* 修正

* note参照

---------

Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>

* Update CHANGELOG.md

* New Crowdin updates (#10823)

* New translations ja-JP.yml (English)

* New translations ja-JP.yml (German)

* ci: fix typo

* fix(frontend): より明確な説明にしたのとtypo修正

* fix typo

* fix(frontend): カラーバーがリプライには表示されないのを修正

* fix(frontend): チャンネル内の検索ボックスが挙動不審な問題を修正

Fix #10793

* enhance(backend): ノートのハッシュタグもMeilisearchに突っ込むように

今後ハッシュタグ検索とか実装するときのため

* feat(frontend): ユーザー指定ノート検索

* fix(frontend): fix retention chart rendering

* Update about-misskey.vue

* meta: Remove @rinsuki from reviewer-lottery (#10830)

* New Crowdin updates (#10824)

* New translations ja-JP.yml (English)

* New translations ja-JP.yml (German)

* New translations ja-JP.yml (Chinese Traditional)

* New translations ja-JP.yml (English)

* New translations ja-JP.yml (Chinese Traditional)

* New translations ja-JP.yml (German)

* New translations ja-JP.yml (English)

* New translations ja-JP.yml (Chinese Traditional)

* New translations ja-JP.yml (French)

* New translations ja-JP.yml (German)

* New translations ja-JP.yml (English)

* New translations ja-JP.yml (Japanese, Kansai)

* New translations ja-JP.yml (Chinese Traditional)

* New translations ja-JP.yml (Spanish)

* New translations ja-JP.yml (German)

* New translations ja-JP.yml (Italian)

* New translations ja-JP.yml (Korean)

* New translations ja-JP.yml (Norwegian)

* New translations ja-JP.yml (Russian)

* New translations ja-JP.yml (Chinese Simplified)

* New translations ja-JP.yml (Indonesian)

* New translations ja-JP.yml (Thai)

* enhance(frontend): アカウント初期設定ウィザードにプライバシー設定を追加

* Update CHANGELOG.md

* fix(backend): ひとつのMeilisearchサーバーを複数のMisskeyサーバーで使えない問題を修正

* fix MkUserSetupDialog.Privacy.vue

* ci: skip non-Japanese locale on TurboSnap

* ci: notify on changes for push events

* ci: fix missing branch

* Update basic.cy.js

* [ci skip] New Crowdin updates (#10834)

* New translations ja-JP.yml (English)

* New translations ja-JP.yml (Arabic)

* New translations ja-JP.yml (German)

* New translations ja-JP.yml (Chinese Simplified)

* New translations ja-JP.yml (Japanese, Kansai)

* New translations ja-JP.yml (Arabic)

* 🎨

* 🎨

* enhance(frontend): add retention line chart

* update deps

* refactor

* fix(frontend): Pageにおいて画像ブロックに画像を設定できない問題を修正

Fix #10837

---------

Co-authored-by: nenohi <kimutipartylove@gmail.com>
Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>
Co-authored-by: rinsuki <428rinsuki+git@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
packages/backend:test packages/backend Server side specific issue/PR packages/frontend Client side specific issue/PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants