Skip to content

Commit f63c768

Browse files
committed
Ruby: parse \G, \b, and \B anchors as special characters, not escapes
1 parent 1f3f7e9 commit f63c768

File tree

6 files changed

+133
-94
lines changed

6 files changed

+133
-94
lines changed

ruby/ql/lib/codeql/ruby/security/performance/ParseRegExp.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ class RegExp extends AST::RegExpLiteral {
397397
end = start + 2 and
398398
this.escapingChar(start) and
399399
char = this.getText().substring(start, end) and
400-
char = ["\\A", "\\Z", "\\z"]
400+
char = ["\\A", "\\Z", "\\z", "\\G", "\\b", "\\B"]
401401
)
402402
}
403403

ruby/ql/lib/codeql/ruby/security/performance/RegExpTreeView.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ private int toHex(string hex) {
441441
/**
442442
* A word boundary, that is, a regular expression term of the form `\b`.
443443
*/
444-
class RegExpWordBoundary extends RegExpEscape {
445-
RegExpWordBoundary() { this.getUnescaped() = "b" }
444+
class RegExpWordBoundary extends RegExpSpecialChar {
445+
RegExpWordBoundary() { this.getChar() = "\\b" }
446446
}
447447

448448
/**

ruby/ql/test/library-tests/regexp/parse.expected

Lines changed: 117 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -308,249 +308,277 @@ regexp.rb:
308308

309309
# 38| [RegExpConstant, RegExpEscape] \t
310310

311-
# 41| [RegExpStar] (foo)*
311+
# 41| [RegExpSpecialChar] \G
312+
313+
# 41| [RegExpSequence] \Gabc
314+
#-----| 0 -> [RegExpSpecialChar] \G
315+
#-----| 1 -> [RegExpConstant, RegExpNormalChar] a
316+
#-----| 2 -> [RegExpConstant, RegExpNormalChar] b
317+
#-----| 3 -> [RegExpConstant, RegExpNormalChar] c
318+
319+
# 41| [RegExpConstant, RegExpNormalChar] a
320+
321+
# 41| [RegExpConstant, RegExpNormalChar] b
322+
323+
# 41| [RegExpConstant, RegExpNormalChar] c
324+
325+
# 42| [RegExpSpecialChar] \b
326+
327+
# 42| [RegExpSequence] \b!a\B
328+
#-----| 0 -> [RegExpSpecialChar] \b
329+
#-----| 1 -> [RegExpConstant, RegExpNormalChar] !
330+
#-----| 2 -> [RegExpConstant, RegExpNormalChar] a
331+
#-----| 3 -> [RegExpSpecialChar] \B
332+
333+
# 42| [RegExpConstant, RegExpNormalChar] !
334+
335+
# 42| [RegExpConstant, RegExpNormalChar] a
336+
337+
# 42| [RegExpSpecialChar] \B
338+
339+
# 45| [RegExpStar] (foo)*
312340
#-----| 0 -> [RegExpGroup] (foo)
313341

314-
# 41| [RegExpGroup] (foo)
342+
# 45| [RegExpGroup] (foo)
315343
#-----| 0 -> [RegExpSequence] foo
316344

317-
# 41| [RegExpSequence] (foo)*bar
345+
# 45| [RegExpSequence] (foo)*bar
318346
#-----| 0 -> [RegExpStar] (foo)*
319347
#-----| 1 -> [RegExpConstant, RegExpNormalChar] b
320348
#-----| 2 -> [RegExpConstant, RegExpNormalChar] a
321349
#-----| 3 -> [RegExpConstant, RegExpNormalChar] r
322350

323-
# 41| [RegExpConstant, RegExpNormalChar] f
351+
# 45| [RegExpConstant, RegExpNormalChar] f
324352

325-
# 41| [RegExpSequence] foo
353+
# 45| [RegExpSequence] foo
326354
#-----| 0 -> [RegExpConstant, RegExpNormalChar] f
327355
#-----| 1 -> [RegExpConstant, RegExpNormalChar] o
328356
#-----| 2 -> [RegExpConstant, RegExpNormalChar] o
329357

330-
# 41| [RegExpConstant, RegExpNormalChar] o
358+
# 45| [RegExpConstant, RegExpNormalChar] o
331359

332-
# 41| [RegExpConstant, RegExpNormalChar] o
360+
# 45| [RegExpConstant, RegExpNormalChar] o
333361

334-
# 41| [RegExpConstant, RegExpNormalChar] b
362+
# 45| [RegExpConstant, RegExpNormalChar] b
335363

336-
# 41| [RegExpConstant, RegExpNormalChar] a
364+
# 45| [RegExpConstant, RegExpNormalChar] a
337365

338-
# 41| [RegExpConstant, RegExpNormalChar] r
366+
# 45| [RegExpConstant, RegExpNormalChar] r
339367

340-
# 42| [RegExpConstant, RegExpNormalChar] f
368+
# 46| [RegExpConstant, RegExpNormalChar] f
341369

342-
# 42| [RegExpSequence] fo(o|b)ar
370+
# 46| [RegExpSequence] fo(o|b)ar
343371
#-----| 0 -> [RegExpConstant, RegExpNormalChar] f
344372
#-----| 1 -> [RegExpConstant, RegExpNormalChar] o
345373
#-----| 2 -> [RegExpGroup] (o|b)
346374
#-----| 3 -> [RegExpConstant, RegExpNormalChar] a
347375
#-----| 4 -> [RegExpConstant, RegExpNormalChar] r
348376

349-
# 42| [RegExpConstant, RegExpNormalChar] o
377+
# 46| [RegExpConstant, RegExpNormalChar] o
350378

351-
# 42| [RegExpGroup] (o|b)
379+
# 46| [RegExpGroup] (o|b)
352380
#-----| 0 -> [RegExpAlt] o|b
353381

354-
# 42| [RegExpAlt] o|b
382+
# 46| [RegExpAlt] o|b
355383
#-----| 0 -> [RegExpConstant, RegExpNormalChar] o
356384
#-----| 1 -> [RegExpConstant, RegExpNormalChar] b
357385

358-
# 42| [RegExpConstant, RegExpNormalChar] o
386+
# 46| [RegExpConstant, RegExpNormalChar] o
359387

360-
# 42| [RegExpConstant, RegExpNormalChar] b
388+
# 46| [RegExpConstant, RegExpNormalChar] b
361389

362-
# 42| [RegExpConstant, RegExpNormalChar] a
390+
# 46| [RegExpConstant, RegExpNormalChar] a
363391

364-
# 42| [RegExpConstant, RegExpNormalChar] r
392+
# 46| [RegExpConstant, RegExpNormalChar] r
365393

366-
# 43| [RegExpGroup] (a|b|cd)
394+
# 47| [RegExpGroup] (a|b|cd)
367395
#-----| 0 -> [RegExpAlt] a|b|cd
368396

369-
# 43| [RegExpSequence] (a|b|cd)e
397+
# 47| [RegExpSequence] (a|b|cd)e
370398
#-----| 0 -> [RegExpGroup] (a|b|cd)
371399
#-----| 1 -> [RegExpConstant, RegExpNormalChar] e
372400

373-
# 43| [RegExpAlt] a|b|cd
401+
# 47| [RegExpAlt] a|b|cd
374402
#-----| 0 -> [RegExpConstant, RegExpNormalChar] a
375403
#-----| 1 -> [RegExpConstant, RegExpNormalChar] b
376404
#-----| 2 -> [RegExpSequence] cd
377405

378-
# 43| [RegExpConstant, RegExpNormalChar] a
406+
# 47| [RegExpConstant, RegExpNormalChar] a
379407

380-
# 43| [RegExpConstant, RegExpNormalChar] b
408+
# 47| [RegExpConstant, RegExpNormalChar] b
381409

382-
# 43| [RegExpConstant, RegExpNormalChar] c
410+
# 47| [RegExpConstant, RegExpNormalChar] c
383411

384-
# 43| [RegExpSequence] cd
412+
# 47| [RegExpSequence] cd
385413
#-----| 0 -> [RegExpConstant, RegExpNormalChar] c
386414
#-----| 1 -> [RegExpConstant, RegExpNormalChar] d
387415

388-
# 43| [RegExpConstant, RegExpNormalChar] d
416+
# 47| [RegExpConstant, RegExpNormalChar] d
389417

390-
# 43| [RegExpConstant, RegExpNormalChar] e
418+
# 47| [RegExpConstant, RegExpNormalChar] e
391419

392-
# 44| [RegExpGroup] (?::+)
420+
# 48| [RegExpGroup] (?::+)
393421
#-----| 0 -> [RegExpPlus] :+
394422

395-
# 44| [RegExpSequence] (?::+)\w
423+
# 48| [RegExpSequence] (?::+)\w
396424
#-----| 0 -> [RegExpGroup] (?::+)
397425
#-----| 1 -> [RegExpCharacterClassEscape] \w
398426

399-
# 44| [RegExpPlus] :+
427+
# 48| [RegExpPlus] :+
400428
#-----| 0 -> [RegExpConstant, RegExpNormalChar] :
401429

402-
# 44| [RegExpConstant, RegExpNormalChar] :
430+
# 48| [RegExpConstant, RegExpNormalChar] :
403431

404-
# 44| [RegExpCharacterClassEscape] \w
432+
# 48| [RegExpCharacterClassEscape] \w
405433

406-
# 47| [RegExpGroup] (?<id>\w+)
434+
# 51| [RegExpGroup] (?<id>\w+)
407435
#-----| 0 -> [RegExpPlus] \w+
408436

409-
# 47| [RegExpPlus] \w+
437+
# 51| [RegExpPlus] \w+
410438
#-----| 0 -> [RegExpCharacterClassEscape] \w
411439

412-
# 47| [RegExpCharacterClassEscape] \w
440+
# 51| [RegExpCharacterClassEscape] \w
413441

414-
# 48| [RegExpGroup] (?'foo'fo+)
442+
# 52| [RegExpGroup] (?'foo'fo+)
415443
#-----| 0 -> [RegExpSequence] fo+
416444

417-
# 48| [RegExpConstant, RegExpNormalChar] f
445+
# 52| [RegExpConstant, RegExpNormalChar] f
418446

419-
# 48| [RegExpSequence] fo+
447+
# 52| [RegExpSequence] fo+
420448
#-----| 0 -> [RegExpConstant, RegExpNormalChar] f
421449
#-----| 1 -> [RegExpPlus] o+
422450

423-
# 48| [RegExpPlus] o+
451+
# 52| [RegExpPlus] o+
424452
#-----| 0 -> [RegExpConstant, RegExpNormalChar] o
425453

426-
# 48| [RegExpConstant, RegExpNormalChar] o
454+
# 52| [RegExpConstant, RegExpNormalChar] o
427455

428-
# 51| [RegExpGroup] (a+)
456+
# 55| [RegExpGroup] (a+)
429457
#-----| 0 -> [RegExpPlus] a+
430458

431-
# 51| [RegExpSequence] (a+)b+\1
459+
# 55| [RegExpSequence] (a+)b+\1
432460
#-----| 0 -> [RegExpGroup] (a+)
433461
#-----| 1 -> [RegExpPlus] b+
434462
#-----| 2 -> [RegExpBackRef] \1
435463

436-
# 51| [RegExpPlus] a+
464+
# 55| [RegExpPlus] a+
437465
#-----| 0 -> [RegExpConstant, RegExpNormalChar] a
438466

439-
# 51| [RegExpConstant, RegExpNormalChar] a
467+
# 55| [RegExpConstant, RegExpNormalChar] a
440468

441-
# 51| [RegExpPlus] b+
469+
# 55| [RegExpPlus] b+
442470
#-----| 0 -> [RegExpConstant, RegExpNormalChar] b
443471

444-
# 51| [RegExpConstant, RegExpNormalChar] b
472+
# 55| [RegExpConstant, RegExpNormalChar] b
445473

446-
# 51| [RegExpBackRef] \1
474+
# 55| [RegExpBackRef] \1
447475

448-
# 52| [RegExpGroup] (?<qux>q+)
476+
# 56| [RegExpGroup] (?<qux>q+)
449477
#-----| 0 -> [RegExpPlus] q+
450478

451-
# 52| [RegExpSequence] (?<qux>q+)\s+\k<qux>+
479+
# 56| [RegExpSequence] (?<qux>q+)\s+\k<qux>+
452480
#-----| 0 -> [RegExpGroup] (?<qux>q+)
453481
#-----| 1 -> [RegExpPlus] \s+
454482
#-----| 2 -> [RegExpPlus] \k<qux>+
455483

456-
# 52| [RegExpPlus] q+
484+
# 56| [RegExpPlus] q+
457485
#-----| 0 -> [RegExpConstant, RegExpNormalChar] q
458486

459-
# 52| [RegExpConstant, RegExpNormalChar] q
487+
# 56| [RegExpConstant, RegExpNormalChar] q
460488

461-
# 52| [RegExpPlus] \s+
489+
# 56| [RegExpPlus] \s+
462490
#-----| 0 -> [RegExpCharacterClassEscape] \s
463491

464-
# 52| [RegExpCharacterClassEscape] \s
492+
# 56| [RegExpCharacterClassEscape] \s
465493

466-
# 52| [RegExpBackRef] \k<qux>
494+
# 56| [RegExpBackRef] \k<qux>
467495

468-
# 52| [RegExpPlus] \k<qux>+
496+
# 56| [RegExpPlus] \k<qux>+
469497
#-----| 0 -> [RegExpBackRef] \k<qux>
470498

471-
# 55| [RegExpNamedCharacterProperty] \p{Word}
499+
# 59| [RegExpNamedCharacterProperty] \p{Word}
472500

473-
# 55| [RegExpStar] \p{Word}*
501+
# 59| [RegExpStar] \p{Word}*
474502
#-----| 0 -> [RegExpNamedCharacterProperty] \p{Word}
475503

476-
# 56| [RegExpNamedCharacterProperty] \P{Digit}
504+
# 60| [RegExpNamedCharacterProperty] \P{Digit}
477505

478-
# 56| [RegExpPlus] \P{Digit}+
506+
# 60| [RegExpPlus] \P{Digit}+
479507
#-----| 0 -> [RegExpNamedCharacterProperty] \P{Digit}
480508

481-
# 57| [RegExpNamedCharacterProperty] \p{^Alnum}
509+
# 61| [RegExpNamedCharacterProperty] \p{^Alnum}
482510

483-
# 57| [RegExpRange] \p{^Alnum}{2,3}
511+
# 61| [RegExpRange] \p{^Alnum}{2,3}
484512
#-----| 0 -> [RegExpNamedCharacterProperty] \p{^Alnum}
485513

486-
# 57| [RegExpNormalChar] 2
514+
# 61| [RegExpNormalChar] 2
487515

488-
# 57| [RegExpNormalChar] ,
516+
# 61| [RegExpNormalChar] ,
489517

490-
# 57| [RegExpNormalChar] 3
518+
# 61| [RegExpNormalChar] 3
491519

492-
# 57| [RegExpNormalChar] }
520+
# 61| [RegExpNormalChar] }
493521

494-
# 58| [RegExpCharacterClass] [a-f\p{Digit}]
522+
# 62| [RegExpCharacterClass] [a-f\p{Digit}]
495523
#-----| 0 -> [RegExpCharacterRange] a-f
496524
#-----| 1 -> [RegExpNamedCharacterProperty] \p{Digit}
497525

498-
# 58| [RegExpPlus] [a-f\p{Digit}]+
526+
# 62| [RegExpPlus] [a-f\p{Digit}]+
499527
#-----| 0 -> [RegExpCharacterClass] [a-f\p{Digit}]
500528

501-
# 58| [RegExpCharacterRange] a-f
529+
# 62| [RegExpCharacterRange] a-f
502530
#-----| 0 -> [RegExpConstant, RegExpNormalChar] a
503531
#-----| 1 -> [RegExpConstant, RegExpNormalChar] f
504532

505-
# 58| [RegExpConstant, RegExpNormalChar] a
533+
# 62| [RegExpConstant, RegExpNormalChar] a
506534

507-
# 58| [RegExpConstant, RegExpNormalChar] f
535+
# 62| [RegExpConstant, RegExpNormalChar] f
508536

509-
# 58| [RegExpNamedCharacterProperty] \p{Digit}
537+
# 62| [RegExpNamedCharacterProperty] \p{Digit}
510538

511-
# 61| [RegExpCharacterClass] [[:alpha:]]
539+
# 65| [RegExpCharacterClass] [[:alpha:]]
512540
#-----| 0 -> [RegExpNamedCharacterProperty] [:alpha:]
513541

514-
# 61| [RegExpSequence] [[:alpha:]][[:digit:]]
542+
# 65| [RegExpSequence] [[:alpha:]][[:digit:]]
515543
#-----| 0 -> [RegExpCharacterClass] [[:alpha:]]
516544
#-----| 1 -> [RegExpCharacterClass] [[:digit:]]
517545

518-
# 61| [RegExpNamedCharacterProperty] [:alpha:]
546+
# 65| [RegExpNamedCharacterProperty] [:alpha:]
519547

520-
# 61| [RegExpCharacterClass] [[:digit:]]
548+
# 65| [RegExpCharacterClass] [[:digit:]]
521549
#-----| 0 -> [RegExpNamedCharacterProperty] [:digit:]
522550

523-
# 61| [RegExpNamedCharacterProperty] [:digit:]
551+
# 65| [RegExpNamedCharacterProperty] [:digit:]
524552

525-
# 64| [RegExpCharacterClass] [[:alpha:][:digit:]]
553+
# 68| [RegExpCharacterClass] [[:alpha:][:digit:]]
526554
#-----| 0 -> [RegExpNamedCharacterProperty] [:alpha:]
527555
#-----| 1 -> [RegExpNamedCharacterProperty] [:digit:]
528556

529-
# 64| [RegExpNamedCharacterProperty] [:alpha:]
557+
# 68| [RegExpNamedCharacterProperty] [:alpha:]
530558

531-
# 64| [RegExpNamedCharacterProperty] [:digit:]
559+
# 68| [RegExpNamedCharacterProperty] [:digit:]
532560

533-
# 67| [RegExpCharacterClass] [A-F[:digit:]a-f]
561+
# 71| [RegExpCharacterClass] [A-F[:digit:]a-f]
534562
#-----| 0 -> [RegExpCharacterRange] A-F
535563
#-----| 1 -> [RegExpNamedCharacterProperty] [:digit:]
536564
#-----| 2 -> [RegExpCharacterRange] a-f
537565

538-
# 67| [RegExpCharacterRange] A-F
566+
# 71| [RegExpCharacterRange] A-F
539567
#-----| 0 -> [RegExpConstant, RegExpNormalChar] A
540568
#-----| 1 -> [RegExpConstant, RegExpNormalChar] F
541569

542-
# 67| [RegExpConstant, RegExpNormalChar] A
570+
# 71| [RegExpConstant, RegExpNormalChar] A
543571

544-
# 67| [RegExpConstant, RegExpNormalChar] F
572+
# 71| [RegExpConstant, RegExpNormalChar] F
545573

546-
# 67| [RegExpNamedCharacterProperty] [:digit:]
574+
# 71| [RegExpNamedCharacterProperty] [:digit:]
547575

548-
# 67| [RegExpCharacterRange] a-f
576+
# 71| [RegExpCharacterRange] a-f
549577
#-----| 0 -> [RegExpConstant, RegExpNormalChar] a
550578
#-----| 1 -> [RegExpConstant, RegExpNormalChar] f
551579

552-
# 67| [RegExpConstant, RegExpNormalChar] a
580+
# 71| [RegExpConstant, RegExpNormalChar] a
553581

554-
# 67| [RegExpConstant, RegExpNormalChar] f
582+
# 71| [RegExpConstant, RegExpNormalChar] f
555583

556-
# 70| [RegExpNamedCharacterProperty] [:digit:]
584+
# 74| [RegExpNamedCharacterProperty] [:digit:]

0 commit comments

Comments
 (0)