From 35243ec8ddf58ea9f074acf1246c61f137c0bd09 Mon Sep 17 00:00:00 2001 From: easrng Date: Sun, 8 Dec 2024 21:43:03 -0500 Subject: [PATCH] disallow "." at head and tail of username --- src/internal/parser.ts | 8 ++++---- test/parser.ts | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/internal/parser.ts b/src/internal/parser.ts index f4c1f49..5b8eac4 100644 --- a/src/internal/parser.ts +++ b/src/internal/parser.ts @@ -586,9 +586,9 @@ export const language = P.createLanguage({ } } } - // remove "-" of tail of username + // remove [.-] of tail of username let modifiedName = username; - result = /-+$/.exec(username); + result = /[.-]+$/.exec(username); if (result != null) { if (modifiedHost == null) { modifiedName = username.slice(0, (-1 * result[0].length)); @@ -597,8 +597,8 @@ export const language = P.createLanguage({ invalidMention = true; } } - // disallow "-" of head of username - if (modifiedName.length === 0 || modifiedName[0] === '-') { + // disallow [.-] of head of username + if (modifiedName.length === 0 || /^[.-]/.test(modifiedName)) { invalidMention = true; } // disallow [.-] of head of hostname diff --git a/test/parser.ts b/test/parser.ts index 3155481..36d98c3 100644 --- a/test/parser.ts +++ b/test/parser.ts @@ -751,6 +751,18 @@ hoge`; assert.deepStrictEqual(mfm.parse(input), output); }); + test('disallow "." in head of username', () => { + const input = '@.abc'; + const output = [TEXT('@.abc')]; + assert.deepStrictEqual(mfm.parse(input), output); + }); + + test('disallow "." in tail of username', () => { + const input = '@abc.'; + const output = [MENTION('abc', null, '@abc'), TEXT('.')]; + assert.deepStrictEqual(mfm.parse(input), output); + }); + test('disallow "." in head of hostname', () => { const input = '@abc@.aaa'; const output = [TEXT('@abc@.aaa')];