【问题标题】:Ruby, How to prevent Redcarpet to render HTML code in the output?Ruby,如何防止 Redcarpet 在输出中呈现 HTML 代码?
【发布时间】:2021-06-23 11:31:57
【问题描述】:

我正在使用 Redcarpet 渲染用户介绍的网页数据。

我发现用户很容易引入恶意 HTML 代码。

我正在尝试不同的 Redcarpet 初始化程序选项,以防止在输出中呈现任何可能的恶意代码,但没有任何效果:

尝试filter_html:

markdown =
  Redcarpet::Markdown.new(
    Redcarpet::Render::HTML,
    filter_html: true
  )

markdown.render("<style>style</style> <script>alert()</script>")

# => "<p><style>style</style> <script>alert()</script></p>\n"

尝试scape_html:

markdown =
  Redcarpet::Markdown.new(
    Redcarpet::Render::HTML,
    escape_html: true
  )

markdown.render("<style>style</style> <script>alert()</script>")

# => "<p><style>style</style> <script>alert()</script></p>\n"

【问题讨论】:

    标签: ruby markdown redcarpet


    【解决方案1】:

    这些是 renderer 的选项,而不是解析器,所以你需要将它们传递给渲染器,然后将配置的渲染器传递给解析器,例如:

    markdown =
      Redcarpet::Markdown.new(
        Redcarpet::Render::HTML.new(escape_html: true),
        # other parser options here, e.g.
        autolink: true
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多