Skip to content

Markdown autolink resolution _is_mailto can fail with bad string indexing #42139

Closed
@tlienart

Description

@tlienart

This was reported by a Franklin user but ended up being a bug with the Markdown module:

julia> Markdown.parse("<一轮红日初升>")
ERROR: StringIndexError: invalid index [6], valid nearby indices [4]=>'', [7]=>''

This is because Markdown sees the < ... > and tries to form an auto link and seems to be using string indexing in doing so. CommonMark.jl handles this well

julia> import CommonMark as CM
julia> p = CM.Parser()
julia> CM.html(p("<一轮红日初升>"))
"<p>&lt;一轮红日初升&gt;</p>\n"

actually reading the full stack trace points to _is_mailto being the culprit

function _is_mailto(s::AbstractString)
length(s) < 6 && return false
# slicing strings is a bit risky, but this equality check is safe
lowercase(s[1:6]) == "mailto:" || return false
return occursin(_email_regex, s[6:end])
end

ironically there's a comment specifying that it's a bit risky to be slicing the string (also it's a bit odd to check the first 6 characters with a 7 character string)

A suggestion would be to replace

lowercase(s[1:6]) == "mailto:" || return false
return occursin(_email_regex, s[6:end])

using first and nextind, I opened a PR with this here: #42140

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions