/
MarkdownSanitizer.java
633 lines (596 loc) · 22.2 KB
/
MarkdownSanitizer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.api.utils;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.regex.Pattern;
/**
* Implements an algorithm that can strip or replace markdown in any supplied string.
*
* @see #sanitize(String, net.dv8tion.jda.api.utils.MarkdownSanitizer.SanitizationStrategy)
*
* @since 4.0.0
*/
public class MarkdownSanitizer
{
/** Normal characters that are not special for markdown, ignoring this has no effect */
public static final int NORMAL = 0;
/** Bold region such as "**Hello**" */
public static final int BOLD = 1 << 0;
/** Italics region for underline such as "_Hello_" */
public static final int ITALICS_U = 1 << 1;
/** Italics region for asterisks such as "*Hello*" */
public static final int ITALICS_A = 1 << 2;
/** Monospace region such as "`Hello`" */
public static final int MONO = 1 << 3;
/** Monospace region such as "``Hello``" */
public static final int MONO_TWO = 1 << 4;
/** Codeblock region such as "```Hello```" */
public static final int BLOCK = 1 << 5;
/** Spoiler region such as "||Hello||" */
public static final int SPOILER = 1 << 6;
/** Underline region such as "__Hello__" */
public static final int UNDERLINE = 1 << 7;
/** Strikethrough region such as "~~Hello~~" */
public static final int STRIKE = 1 << 8;
/** Quote region such as {@code "> text here"} */
public static final int QUOTE = 1 << 9;
/** Quote block region such as {@code ">>> text here"} */
public static final int QUOTE_BLOCK = 1 << 10;
private static final int ESCAPED_BOLD = Integer.MIN_VALUE | BOLD;
private static final int ESCAPED_ITALICS_U = Integer.MIN_VALUE | ITALICS_U;
private static final int ESCAPED_ITALICS_A = Integer.MIN_VALUE | ITALICS_A;
private static final int ESCAPED_MONO = Integer.MIN_VALUE | MONO;
private static final int ESCAPED_MONO_TWO = Integer.MIN_VALUE | MONO_TWO;
private static final int ESCAPED_BLOCK = Integer.MIN_VALUE | BLOCK;
private static final int ESCAPED_SPOILER = Integer.MIN_VALUE | SPOILER;
private static final int ESCAPED_UNDERLINE = Integer.MIN_VALUE | UNDERLINE;
private static final int ESCAPED_STRIKE = Integer.MIN_VALUE | STRIKE;
private static final int ESCAPED_QUOTE = Integer.MIN_VALUE | QUOTE;
private static final int ESCAPED_QUOTE_BLOCK = Integer.MIN_VALUE | QUOTE_BLOCK;
private static final Pattern codeLanguage = Pattern.compile("^\\w+\n.*", Pattern.MULTILINE | Pattern.DOTALL);
private static final Pattern quote = Pattern.compile("> +.*", Pattern.DOTALL | Pattern.MULTILINE);
private static final Pattern quoteBlock = Pattern.compile(">>>\\s+\\S.*", Pattern.DOTALL | Pattern.MULTILINE);
private static final TIntObjectMap<String> tokens;
static
{
tokens = new TIntObjectHashMap<>();
tokens.put(NORMAL, "");
tokens.put(BOLD, "**");
tokens.put(ITALICS_U, "_");
tokens.put(ITALICS_A, "*");
tokens.put(BOLD | ITALICS_A, "***");
tokens.put(MONO, "`");
tokens.put(MONO_TWO, "``");
tokens.put(BLOCK, "```");
tokens.put(SPOILER, "||");
tokens.put(UNDERLINE, "__");
tokens.put(STRIKE, "~~");
}
private int ignored;
private SanitizationStrategy strategy;
public MarkdownSanitizer()
{
this.ignored = NORMAL;
this.strategy = SanitizationStrategy.REMOVE;
}
public MarkdownSanitizer(int ignored, @Nullable SanitizationStrategy strategy)
{
this.ignored = ignored;
this.strategy = strategy == null ? SanitizationStrategy.REMOVE : strategy;
}
/**
* Sanitize string with default settings.
* <br>Same as {@code sanitize(sequence, SanitizationStrategy.REMOVE)}
*
* @param sequence
* The string to sanitize
*
* @return The sanitized string
*/
@Nonnull
public static String sanitize(@Nonnull String sequence)
{
return sanitize(sequence, SanitizationStrategy.REMOVE);
}
/**
* Sanitize string without ignoring anything.
*
* @param sequence
* The string to sanitize
* @param strategy
* The {@link net.dv8tion.jda.api.utils.MarkdownSanitizer.SanitizationStrategy} to apply
*
* @throws java.lang.IllegalArgumentException
* If provided with null
*
* @return The sanitized string
*
* @see MarkdownSanitizer#MarkdownSanitizer()
* @see #withIgnored(int)
*/
@Nonnull
public static String sanitize(@Nonnull String sequence, @Nonnull SanitizationStrategy strategy)
{
Checks.notNull(sequence, "String");
Checks.notNull(strategy, "Strategy");
return new MarkdownSanitizer().withStrategy(strategy).compute(sequence);
}
/**
* Escapes every markdown formatting found in the provided string.
*
* @param sequence
* The string to sanitize
*
* @throws java.lang.IllegalArgumentException
* If provided with null
*
* @return The string with escaped markdown
*
* @see #escape(String, int)
*/
@Nonnull
public static String escape(@Nonnull String sequence)
{
return escape(sequence, NORMAL);
}
/**
* Escapes every markdown formatting found in the provided string.
* <br>Example: {@code escape("**Hello** ~~World~~!", MarkdownSanitizer.BOLD | MarkdownSanitizer.STRIKE)}
*
* @param sequence
* The string to sanitize
* @param ignored
* Formats to ignore
*
* @throws java.lang.IllegalArgumentException
* If provided with null
*
* @return The string with escaped markdown
*/
@Nonnull
public static String escape(@Nonnull String sequence, int ignored)
{
return new MarkdownSanitizer()
.withIgnored(ignored)
.withStrategy(SanitizationStrategy.ESCAPE)
.compute(sequence);
}
/**
* Escapes every single markdown formatting token found in the provided string.
* <br>Example: {@code escape("**Hello _World_", true)}
*
* @param sequence
* The string to sanitize
* @param single
* Whether it should scape single tokens or not.
*
* @throws java.lang.IllegalArgumentException
* If provided with null sequence
*
* @return The string with escaped markdown
*/
@Nonnull
public static String escape(@Nonnull String sequence, boolean single)
{
Checks.notNull(sequence, "Input");
if(!single) return escape(sequence);
StringBuilder builder = new StringBuilder();
boolean escaped = false;
boolean newline = true;
for (int i = 0; i < sequence.length(); i++)
{
char current = sequence.charAt(i);
if (newline)
{
newline = Character.isWhitespace(current); // might still be a quote if prefixed by whitespace
if (current == '>')
{
// Check for quote if line starts with angle bracket
if (i + 1 < sequence.length() && Character.isWhitespace(sequence.charAt(i+1)))
{
builder.append("\\>"); // simple quote
}
else if (i + 3 < sequence.length() && sequence.startsWith(">>>", i) && Character.isWhitespace(sequence.charAt(i+3)))
{
builder.append("\\>\\>\\>").append(sequence.charAt(i+3)); // block quote
i += 3; // since we include 3 angle brackets AND whitespace
}
else
{
builder.append(current); // just a normal angle bracket
}
continue;
}
}
if (escaped)
{
builder.append(current);
escaped = false;
continue;
}
// Handle average case
switch (current)
{
case '*': // simple markdown escapes for single characters
case '_':
case '`':
builder.append('\\').append(current);
break;
case '|': // cases that require at least 2 characters in sequence
case '~':
if (i + 1 < sequence.length() && sequence.charAt(i+1) == current)
{
builder.append('\\').append(current)
.append('\\').append(current);
i++;
}
else
builder.append(current);
break;
case '\\': // escape character
builder.append(current);
escaped = true;
break;
case '\n': // linefeed is a special case for quotes
builder.append(current);
newline = true;
break;
default:
builder.append(current);
}
}
return builder.toString();
}
/**
* Switches the used {@link net.dv8tion.jda.api.utils.MarkdownSanitizer.SanitizationStrategy}.
*
* @param strategy
* The new strategy
*
* @throws java.lang.IllegalArgumentException
* If provided with null
*
* @return The current sanitizer instance with the new strategy
*/
@Nonnull
public MarkdownSanitizer withStrategy(@Nonnull SanitizationStrategy strategy)
{
Checks.notNull(strategy, "Strategy");
this.strategy = strategy;
return this;
}
/**
* Specific regions to ignore.
* <br>Example: {@code new MarkdownSanitizer().withIgnored(MarkdownSanitizer.BOLD | MarkdownSanitizer.UNDERLINE).compute("Hello __world__!")}
*
* @param ignored
* The regions to ignore
*
* @return The current sanitizer instance with the new ignored regions
*/
@Nonnull
public MarkdownSanitizer withIgnored(int ignored)
{
this.ignored |= ignored;
return this;
}
private int getRegion(int index, @Nonnull String sequence)
{
if (sequence.length() - index >= 3)
{
String threeChars = sequence.substring(index, index + 3);
switch (threeChars)
{
case "```":
return doesEscape(index, sequence) ? ESCAPED_BLOCK : BLOCK;
case "***":
return doesEscape(index, sequence) ? ESCAPED_BOLD | ITALICS_A : BOLD | ITALICS_A;
}
}
if (sequence.length() - index >= 2)
{
String twoChars = sequence.substring(index, index + 2);
switch (twoChars)
{
case "**":
return doesEscape(index, sequence) ? ESCAPED_BOLD : BOLD;
case "__":
return doesEscape(index, sequence) ? ESCAPED_UNDERLINE : UNDERLINE;
case "~~":
return doesEscape(index, sequence) ? ESCAPED_STRIKE : STRIKE;
case "``":
return doesEscape(index, sequence) ? ESCAPED_MONO_TWO : MONO_TWO;
case "||":
return doesEscape(index, sequence) ? ESCAPED_SPOILER : SPOILER;
}
}
char current = sequence.charAt(index);
switch (current)
{
case '*':
return doesEscape(index, sequence) ? ESCAPED_ITALICS_A : ITALICS_A;
case '_':
return doesEscape(index, sequence) ? ESCAPED_ITALICS_U : ITALICS_U;
case '`':
return doesEscape(index, sequence) ? ESCAPED_MONO : MONO;
}
return NORMAL;
}
private boolean hasCollision(int index, @Nonnull String sequence, char c)
{
if (index < 0)
return false;
return index < sequence.length() - 1 && sequence.charAt(index + 1) == c;
}
private int findEndIndex(int afterIndex, int region, @Nonnull String sequence)
{
if (isEscape(region))
return -1;
int lastMatch = afterIndex + getDelta(region) + 1;
while (lastMatch != -1)
{
switch (region)
{
case BOLD | ITALICS_A:
lastMatch = sequence.indexOf("***", lastMatch);
break;
case BOLD:
lastMatch = sequence.indexOf("**", lastMatch);
if (lastMatch != -1 && hasCollision(lastMatch + 1, sequence, '*')) // did we find a bold italics tag?
{
lastMatch += 3;
continue;
}
break;
case ITALICS_A:
lastMatch = sequence.indexOf('*', lastMatch);
if (lastMatch != -1 && hasCollision(lastMatch, sequence, '*')) // did we find a bold tag?
{
if (hasCollision(lastMatch + 1, sequence, '*'))
lastMatch += 3;
else
lastMatch += 2;
continue;
}
break;
case UNDERLINE:
lastMatch = sequence.indexOf("__", lastMatch);
break;
case ITALICS_U:
lastMatch = sequence.indexOf('_', lastMatch);
if (lastMatch != -1 && hasCollision(lastMatch, sequence, '_')) // did we find an underline tag?
{
lastMatch += 2;
continue;
}
break;
case SPOILER:
lastMatch = sequence.indexOf("||", lastMatch);
break;
case BLOCK:
lastMatch = sequence.indexOf("```", lastMatch);
break;
case MONO_TWO:
lastMatch = sequence.indexOf("``", lastMatch);
if (lastMatch != -1 && hasCollision(lastMatch + 1, sequence, '`')) // did we find a codeblock?
{
lastMatch += 3;
continue;
}
break;
case MONO:
lastMatch = sequence.indexOf('`', lastMatch);
if (lastMatch != -1 && hasCollision(lastMatch, sequence, '`')) // did we find a codeblock?
{
if (hasCollision(lastMatch + 1, sequence, '`'))
lastMatch += 3;
else
lastMatch += 2;
continue;
}
break;
case STRIKE:
lastMatch = sequence.indexOf("~~", lastMatch);
break;
default:
return -1;
}
if (lastMatch == -1 || !doesEscape(lastMatch, sequence))
return lastMatch;
lastMatch++;
}
return -1;
}
@Nonnull
private String handleRegion(int start, int end, @Nonnull String sequence, int region)
{
String resolved = sequence.substring(start, end);
switch (region)
{
case BLOCK:
case MONO:
case MONO_TWO:
return resolved;
default:
return new MarkdownSanitizer(ignored, strategy).compute(resolved);
}
}
private int getDelta(int region)
{
switch (region)
{
case ESCAPED_BLOCK:
case ESCAPED_BOLD | ITALICS_A:
case BLOCK:
case BOLD | ITALICS_A:
return 3;
case ESCAPED_MONO_TWO:
case ESCAPED_BOLD:
case ESCAPED_UNDERLINE:
case ESCAPED_SPOILER:
case ESCAPED_STRIKE:
case MONO_TWO:
case BOLD:
case UNDERLINE:
case SPOILER:
case STRIKE:
return 2;
case ESCAPED_ITALICS_A:
case ESCAPED_ITALICS_U:
case ESCAPED_MONO:
case ESCAPED_QUOTE:
case ITALICS_A:
case ITALICS_U:
case MONO:
return 1;
default:
return 0;
}
}
private void applyStrategy(int region, @Nonnull String seq, @Nonnull StringBuilder builder)
{
if (strategy == SanitizationStrategy.REMOVE)
{
if (codeLanguage.matcher(seq).matches())
builder.append(seq.substring(seq.indexOf("\n") + 1));
else
builder.append(seq);
return;
}
String token = tokens.get(region);
if (token == null)
throw new IllegalStateException("Found illegal region for strategy ESCAPE '" + region + "' with no known format token!");
if (region == UNDERLINE)
token = "_\\_"; // UNDERLINE needs special handling because the client thinks its ITALICS_U if you only escape once
else if (region == BOLD)
token = "*\\*"; // BOLD needs special handling because the client thinks its ITALICS_A if you only escape once
else if (region == (BOLD | ITALICS_A))
token = "*\\*\\*"; // BOLD | ITALICS_A needs special handling because the client thinks its BOLD if you only escape once
builder.append("\\").append(token)
.append(seq)
.append("\\").append(token);
}
private boolean doesEscape(int index, @Nonnull String seq)
{
int backslashes = 0;
for (int i = index - 1; i > -1; i--)
{
if (seq.charAt(i) != '\\')
break;
backslashes++;
}
return backslashes % 2 != 0;
}
private boolean isEscape(int region)
{
return (Integer.MIN_VALUE & region) != 0;
}
private boolean isIgnored(int nextRegion)
{
return (nextRegion & ignored) == nextRegion;
}
/**
* Computes the provided input.
* <br>Uses the specified {@link net.dv8tion.jda.api.utils.MarkdownSanitizer.SanitizationStrategy} and
* ignores any regions specified with {@link #withIgnored(int)}.
*
* @param sequence
* The string to compute
*
* @throws java.lang.IllegalArgumentException
* If the provided string is null
*
* @return The resulting string after applying the computation
*/
@Nonnull
public String compute(@Nonnull String sequence)
{
Checks.notNull(sequence, "Input");
StringBuilder builder = new StringBuilder();
String end = handleQuote(sequence);
if (end != null) return end;
boolean onlySpacesSinceNewLine = true;
for (int i = 0; i < sequence.length();)
{
int nextRegion = getRegion(i, sequence);
char c = sequence.charAt(i);
boolean isNewLine = c == '\n';
boolean isSpace = c == ' ';
onlySpacesSinceNewLine = isNewLine || (onlySpacesSinceNewLine && isSpace);
if (nextRegion == NORMAL)
{
builder.append(sequence.charAt(i++));
if ((isNewLine || (isSpace && onlySpacesSinceNewLine)) && i < sequence.length())
{
String result = handleQuote(sequence.substring(i));
if (result != null)
return builder.append(result).toString();
}
continue;
}
int endRegion = findEndIndex(i, nextRegion, sequence);
if (isIgnored(nextRegion) || endRegion == -1)
{
int delta = getDelta(nextRegion);
for (int j = 0; j < delta; j++)
builder.append(sequence.charAt(i++));
continue;
}
int delta = getDelta(nextRegion);
applyStrategy(nextRegion, handleRegion(i + delta, endRegion, sequence, nextRegion), builder);
i = endRegion + delta;
}
return builder.toString();
}
private String handleQuote(@Nonnull String sequence)
{
// Special handling for quote
if (!isIgnored(QUOTE) && quote.matcher(sequence).matches())
{
int start = sequence.indexOf('>');
if (start < 0)
start = 0;
StringBuilder builder = new StringBuilder(compute(sequence.substring(start + 2)));
if (strategy == SanitizationStrategy.ESCAPE)
builder.insert(0, "\\> ");
return builder.toString();
}
else if (!isIgnored(QUOTE_BLOCK) && quoteBlock.matcher(sequence).matches())
{
if (strategy == SanitizationStrategy.ESCAPE)
return compute("\\".concat(sequence));
return compute(sequence.substring(4));
}
return null;
}
public enum SanitizationStrategy
{
/**
* Remove any format tokens that are not escaped or within a special region.
* <br>{@code "**Hello** World!" -> "Hello World!"}
*/
REMOVE,
/**
* Escape any format tokens that are not escaped or within a special region.
* <br>{@code "**Hello** World!" -> "\**Hello\** World!"}
*/
ESCAPE,
}
}