控制 Pandoc word 文档输出大小/测试图像大小

Control the Pandoc word document output size / test image sizes

我的客户想要将 markdown 文本转换为 word,我们将使用 Pandoc。但是,我们想要控制恶意提交(例如,带有 1000 个外部托管图像的 Markdown 文档,每个图像为 10 MB),在尝试生成输出时可以 stress/break 服务器。

选项是在 Markdown 中对图像模式进行正则表达式并测试它们的大小(甚至限制数量)或者甚至完全禁止外部图像,但我想知道如果生成的 docx 超过某个值,是否有办法中止 Pandoc尺码?

或者有没有简单的方法来获取图像并测试它们的大小?

Pandoc 通常在写入输出文件时获取图像,但您可以通过使用 Lua filter 自己获取图像来控制它。这允许在图像的组合大小变得太大时立即停止获取。

local total_size_images = 0
local max_images_size = 100000  -- in bytes

-- Process all images
function Image (img)
  -- use pandoc's default method to fetch the image contents
  local mimetype, contents = pandoc.mediabag.fetch(img.src)
  -- check that contents isn't too large
  total_size_images = total_size_images + #contents
  if total_size_images > max_images_size then
    error('images too large!')
  end
  -- replace image path with the hash of the image's contents.
  local new_filename = pandoc.utils.sha1(contents)
  -- store image in pandoc's "mediabag", so it won't be fetched again.
  pandoc.mediabag.insert(new_filename, mimetype, contents)
  img.src = new_filename
  -- return the modified image
  return img
end

请确保在发布应用程序之前阅读 pandoc 手册中的 "A note on security" 部分。