使用 RMagick 从 https 读取 pdf 会出现未授权错误

Reading pdfs with RMagick from https gives a not authorized error

我正在尝试阅读 pdf 并将第一页另存为图像。此方法对http有效,但对https无效。

require 'RMagick'

url = "http://www.ke.tu-darmstadt.de/publications/reports/tud-ke-2008-07.pdf"
image = Magick::Image.read(url + "[0]")
=> [http://www.ke.tu-darmstadt.de/publications/reports/tud-ke-2008-07.pdf[0]=>tud-ke-2008-07.pdf PDF 595x842 595x842+0+0 DirectClass 16-bit 27kb]

url = "https://www.cs.purdue.edu/homes/dgleich/publications/Gleich%202003%20-%20Machine%20Learning%20in%20Computer%20Chess.pdf"
image = Magick::Image.read(url + "[0]")
Magick::ImageMagickError: not authorized `//www.cs.purdue.edu/homes/dgleich/publications/Gleich%202003%20-%20Machine%20Learning%20in%20Computer%20Chess.pdf' @ error/constitute.c/ReadImage/454

policy.xml 文件未经编辑看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policymap [
<!ELEMENT policymap (policy)+>
<!ELEMENT policy (#PCDATA)>
<!ATTLIST policy domain (delegate|coder|filter|path|resource) #IMPLIED>
<!ATTLIST policy name CDATA #IMPLIED>
<!ATTLIST policy rights CDATA #IMPLIED>
<!ATTLIST policy pattern CDATA #IMPLIED>
<!ATTLIST policy value CDATA #IMPLIED>
]>
<!--
  Configure ImageMagick policies.

  Domains include system, delegate, coder, filter, path, or resource.

  Rights include none, read, write, and execute.  Use | to combine them,
  for example: "read | write" to permit read from, or write to, a path.

  Use a glob expression as a pattern.

  Suppose we do not want users to process MPEG video images:

    <policy domain="delegate" rights="none" pattern="mpeg:decode" />

  Here we do not want users reading images from HTTP:

    <policy domain="coder" rights="none" pattern="HTTP" />

  Lets prevent users from executing any image filters:

    <policy domain="filter" rights="none" pattern="*" />

  The /repository file system is restricted to read only.  We use a glob
  expression to match all paths that start with /repository:

    <policy domain="path" rights="read" pattern="/repository/*" />

  Any large image is cached to disk rather than memory:

  Define arguments for the memory, map, area, and disk resources with
  SI prefixes (.e.g 100MB).  In addition, resource policies are maximums for
  each instance of ImageMagick (e.g. policy memory limit 1GB, -limit 2GB
  exceeds policy maximum so memory limit is 1GB).
-->
<policymap>
  <!-- <policy domain="system" name="precision" value="6"/> -->
  <!-- <policy domain="resource" name="temporary-path" value="/tmp"/> -->
  <!-- <policy domain="resource" name="memory" value="2GiB"/> -->
  <!-- <policy domain="resource" name="map" value="4GiB"/> -->
  <!-- <policy domain="resource" name="area" value="1GB"/> -->
  <!-- <policy domain="resource" name="disk" value="16EB"/> -->
  <!-- <policy domain="resource" name="file" value="768"/> -->
  <!-- <policy domain="resource" name="thread" value="4"/> -->
  <!-- <policy domain="resource" name="throttle" value="0"/> -->
  <!-- <policy domain="resource" name="time" value="3600"/> -->
  <policy domain="coder" rights="none" pattern="EPHEMERAL" />
  <policy domain="coder" rights="none" pattern="URL" />
  <policy domain="coder" rights="none" pattern="HTTPS" />
  <policy domain="coder" rights="none" pattern="MVG" />
  <policy domain="coder" rights="none" pattern="MSL" />
  <policy domain="coder" rights="none" pattern="TEXT" />
  <policy domain="coder" rights="none" pattern="SHOW" />
  <policy domain="coder" rights="none" pattern="WIN" />
  <policy domain="coder" rights="none" pattern="PLT" />
  <policy domain="path" rights="none" pattern="@*" />
</policymap>

听起来您的 imagemagick 策略文件不允许访问 https。这是通过看起来像

的指令完成的
<policy domain="coder" rights="none" pattern="HTTPS" />

这是继最近一轮 imagemagick security vulnerabilities 之后推荐 policy.xml 的一部分。

您当然可以编辑 policy.xml 来删除它(我不知道如果文件完全丢失,imagemagick 是否会抱怨)但是这可能会让您面临这些漏洞如果您的托管服务提供商依赖于这些动机

另一种选择是下载文件,然后让 Rmagick 读取该本地文件 - 该策略仅限制 ImageMagick 自己进行 https 访问。