Skip to content

marked 0.3.14 hangs on the attached file, worked in 0.3.9 #1058

Closed

Description

@paulkoerbitz

Expectation

example.txt
should be converted to html. This worked with marked 0.3.9.

Result

With marked 0.3.14 marked "hangs" when given the file.

What was attempted

npm install [email protected]
node_modules/marked/bin/marked

Activity

  1. Feder1co5oave commented on Feb 18, 2018

    @Feder1co5oave
    Contributor

    I'm investigating this.

    Commit a8f2d7f introduced this behavior.

    It is caused by this piece of markdown:

    ~~~
    move.rs:9:9: 9:22 error: cannot move out of borrowed content
    move.rs:9         &MyEnum::Y(y) => println!("{}", *y)  // <-- Error, y cannot be moved out of a reference
                      ^~~~~~~~~~~~~
    move.rs:9:20: 9:21 note: attempting to move value to here
    move.rs:9         &MyEnum::Y(y) => println!("{}", *y)  // <-- Error, y cannot be moved out of a reference
                                 ^
    move.rs:9:20: 9:21 help: to prevent the move, use `ref y` or `ref mut y` to capture value by reference
    move.rs:9         &MyEnum::Y(y) => println!("{}", *y)  // <-- Error, y cannot be moved out of a reference
                                 ^
    ~~~
  2. Feder1co5oave commented on Feb 19, 2018

    @Feder1co5oave
    Contributor

    This causes a hang:

    > marked.InlineLexer.rules.tag
    /^<!--[\s\S]*?-->|^<\/?[a-zA-Z0-9\-]+(?:"[^"]*"|'[^']*'|\s[^<'">\/]*)*?\/?>/
    
    > marked.InlineLexer.rules.tag.exec(`<-- Error, y cannot be moved out of a reference
    ...                              ^`)

    I don't really understand the reason underlying this.

  3. added a commit that references this issue on Feb 19, 2018
    d30c6ce
  4. Feder1co5oave commented on Feb 19, 2018

    @Feder1co5oave
    Contributor

    Thanks @paulkoerbitz, I too thought immediately to catastrophic backtracking but that group (?: ... ) should not be affected by it because its branches are mutually exclusive.
    I've worked a patch for this but I wanna be sure it doesn't break anything before posting it.
    I'll post it here in a bit and any help would be welcome, since I cannot figure out why that works and the current one doesn't.

  5. paulkoerbitz commented on Feb 19, 2018

    @paulkoerbitz
    Author

    @Feder1co5oave thank you for the quick replies. I can't really pinpoint it either (but it seems you know a lot more about this than me anyway ;) ) - I've worked around the issue by pinning my dependency to 0.3.9, so from my side the need for a fix is not super urgent.

  6. Feder1co5oave commented on Feb 19, 2018

    @Feder1co5oave
    Contributor

    @paulkoerbitz and thank you for reporting and helping. Unfortunately regexes are tricky, and there is no way to determine they run smoothly on every possible input. What I'm trying to work on is to widen our test base.

    I think I found it is in fact exponential backtracking. I wrote a script to test this:

    const regex = /^<!--[\s\S]*?-->|^<\/?[a-zA-Z0-9\-]+(?:"[^"]*"|'[^']*'|\s[^<'">\/]*)*?\/?>/;
    var src = '<-- Error, y cannot be moved out of a reference';
    
    for (let i = 0; i < 18; i++) {
    	let begin = process.hrtime();
    	regex.exec(src);
    	end = process.hrtime(begin);
    	console.log(`Matching '${src}' Took ${end[0] * 1000 + end[1] / 10**6} milliseconds`);
    	src += ' ';
    }

    this prints out:

    Matching '<-- Error, y cannot be moved out of a reference' Took 0.28264 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference ' Took 0.099293 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference  ' Took 0.094701 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference   ' Took 0.102704 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference    ' Took 0.156877 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference     ' Took 0.291632 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference      ' Took 0.516443 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference       ' Took 0.993562 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference        ' Took 2.872591 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference         ' Took 3.970916 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference          ' Took 8.493158 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference           ' Took 16.972238 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference            ' Took 35.138719 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference             ' Took 65.447452 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference              ' Took 140.613324 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference               ' Took 263.678523 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference                ' Took 504.905385 milliseconds
    Matching '<-- Error, y cannot be moved out of a reference                 ' Took 1031.746128 milliseconds
    

    with any additional space added, it takes roughly double the time and gets terribly slow pretty soon. So that group needs to be rewritten.

  7. added
    L2 - annoying
    on Feb 20, 2018
  8. added
    L0 - security
    and removed
    L2 - annoying
    on Feb 22, 2018
  9. davisjam commented on Feb 22, 2018

    @davisjam
    Contributor

    there is no way to determine they run smoothly on every possible input

    This is a good starting place: https://github.com/NicolaasWeideman/RegexStaticAnalysis
    Some setup required.

  10. Feder1co5oave commented on Feb 22, 2018

    @Feder1co5oave
    Contributor

    @davisjam Thanks! I've been looking for something like this for a while!

  11. Feder1co5oave commented on Feb 23, 2018

    @Feder1co5oave
    Contributor

    Two very different errors were revealed by this issue:

    This code fence should not end at line 3 because ^~~~~~~~~~~~~ is not a valid closing fence (must be on a new line). So the rest of the code block is parsed as a paragraph.

    ~~~
    move.rs:9:9: 9:22 error: cannot move out of borrowed content
    move.rs:9         &MyEnum::Y(y) => println!("{}", *y)  // <-- Error, y cannot be moved out of a reference
                      ^~~~~~~~~~~~~
    move.rs:9:20: 9:21 note: attempting to move value to here
    move.rs:9         &MyEnum::Y(y) => println!("{}", *y)  // <-- Error, y cannot be moved out of a reference
                                 ^
    move.rs:9:20: 9:21 help: to prevent the move, use `ref y` or `ref mut y` to capture value by reference
    move.rs:9         &MyEnum::Y(y) => println!("{}", *y)  // <-- Error, y cannot be moved out of a reference
                                 ^
    ~~~
    

    Then, <-- Error, y cannot be moved out of a reference causes the inline html rule to do catastrophic backtracking and marked hangs.

  12. joshbruce commented on Feb 24, 2018

    @joshbruce
    Member

    Created #1074 to give us a place to discuss tool recommendations and whatnot. Think this might also be a good opportunity to revisit the concept of projects to help with prioritization and tracking; otherwise we could end up with a lot of half-finished work.

    @Feder1co5oave, @UziTech, and @styfle: Can you see the projects and move cards now?

    https://github.com/markedjs/marked/projects/2?

  13. 2 remaining items

  14. davisjam commented on Feb 26, 2018

    @davisjam
    Contributor

    I believe this is fixed in #1083.

  15. davisjam commented on Feb 26, 2018

    @davisjam
    Contributor

    I tried the sample from the initial report. It hangs on master but not with the fix in #1083.

    If the root cause analysis was correct (I haven't checked) then the test case I added in #1083 should suffice to cover this case. But @Feder1co5oave perhaps you still want to add this input file as a standalone test case?

  16. removed their assignment
    on Feb 27, 2018
  17. Feder1co5oave commented on Feb 27, 2018

    @Feder1co5oave
    Contributor

    Fixed in #1083

  18. Feder1co5oave commented on Dec 7, 2018

    @Feder1co5oave
    Contributor

    This fence issue is still not fixed: demo

  19. reopened this on Dec 7, 2018
  20. added a commit that references this issue on Dec 8, 2018
    67a5941
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    L0 - security

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions