fix: regular expressions in word mutes (#8254)
* fix: handle regex exceptions for word mutes * add i18n strings Co-authored-by: rinsuki <428rinsuki+git@gmail.com> * stricter input validation in backend * add migration for hard mutes * fix * use correct regex library in migration * use query builder to avoid SQL injection Co-authored-by: Robin B <robflop98@outlook.com> Co-authored-by: rinsuki <428rinsuki+git@gmail.com>
This commit is contained in:
parent
7ba5512a65
commit
afb6304979
6 changed files with 173 additions and 31 deletions
|
@ -1,3 +1,4 @@
|
|||
const RE2 = require('re2');
|
||||
import $ from 'cafy';
|
||||
import * as mfm from 'mfm-js';
|
||||
import { ID } from '@/misc/cafy-id';
|
||||
|
@ -117,7 +118,7 @@ export const meta = {
|
|||
},
|
||||
|
||||
mutedWords: {
|
||||
validator: $.optional.arr($.arr($.str)),
|
||||
validator: $.optional.arr($.either($.arr($.str.min(1)).min(1), $.str)),
|
||||
},
|
||||
|
||||
mutedInstances: {
|
||||
|
@ -163,6 +164,12 @@ export const meta = {
|
|||
code: 'NO_SUCH_PAGE',
|
||||
id: '8e01b590-7eb9-431b-a239-860e086c408e',
|
||||
},
|
||||
|
||||
invalidRegexp: {
|
||||
message: 'Invalid Regular Expression.',
|
||||
code: 'INVALID_REGEXP',
|
||||
id: '0d786918-10df-41cd-8f33-8dec7d9a89a5',
|
||||
}
|
||||
},
|
||||
|
||||
res: {
|
||||
|
@ -191,6 +198,18 @@ export default define(meta, async (ps, _user, token) => {
|
|||
if (ps.avatarId !== undefined) updates.avatarId = ps.avatarId;
|
||||
if (ps.bannerId !== undefined) updates.bannerId = ps.bannerId;
|
||||
if (ps.mutedWords !== undefined) {
|
||||
// validate regular expression syntax
|
||||
ps.mutedWords.filter(x => !Array.isArray(x)).forEach(x => {
|
||||
const regexp = x.match(/^\/(.+)\/(.*)$/);
|
||||
if (!regexp) throw new ApiError(meta.errors.invalidRegexp);
|
||||
|
||||
try {
|
||||
new RE2(regexp[1], regexp[2]);
|
||||
} catch (err) {
|
||||
throw new ApiError(meta.errors.invalidRegexp);
|
||||
}
|
||||
});
|
||||
|
||||
profileUpdates.mutedWords = ps.mutedWords;
|
||||
profileUpdates.enableWordMute = ps.mutedWords.length > 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue