GhostScript:错误:--file--中的/invalidfileaccess [英] GhostScript: Error: /invalidfileaccess in --file--
问题描述
如果尝试包含已知路径之外的文件,则会收到众所周知的错误:
I get the well known error if I try to include a file which is outside the known paths:
Error: /invalidfileaccess in --file--
我知道我可以使用-DNOSAFER
,但是我想避免这种情况,因为这将允许执行恶意代码.
I know that I could use -DNOSAFER
but I want to avoid this, since this would allow malicious code to be executed.
是否可以告诉gs
您可以读取任何文件,但不能删除或删除任何文件"?
Is there an option to tell gs
"you are allowed to read any file, but you must not remove or delete any files"?
推荐答案
(Answer duplicated from gGt an output file with a count of PDF pages for each file with Ghostscript; both these SO issues popped up while I was looking for a solution myself and no dice. :'-( )
请注意,自GhostScript 9.50起,默认情况下已将SAFER模式打开,从而导致类似这样的/invalidfileaccess
错误,通过确保您路径中的所有目录分隔符都为/
正斜杠,并不能消除该错误,该错误不会消失.当前路径等.
Note that since GhostScript 9.50 the SAFER mode has been turned ON by default, resulting in /invalidfileaccess
errors like these, which would not go away by making sure all directory separators in thee path are /
forward slashes, fiddling with the current path, etc.
今天这花了我很多时间和几个小时,但是到2020年之后,您需要执行以下操作:
This has cost me quite some hair and several hours today, but anno 2020 you'll need to do something like this:
gs -q --permit-file-read=d:/ -dNODISPLAY -c "(d:/test.pdf) (r) file runpdfbegin pdfpagecount = quit"
即确保在PostScript命令/脚本中正在加载的PDF的路径在批准列表(也称为权限列表)
i.e. make sure that the path to the PDF you're loading in the PostScript command/script is in the approved list a.k.a. permission list, using one of the --permit-file-xyz
commandline arguments.
gs -q -Id:/ -dNODISPLAY -c "(d:/test.pdf) (r) file runpdfbegin pdfpagecount = quit"
即确保在PostScript命令/脚本中正在加载的PDF的路径在批准列表(也称为权限列表)中.
i.e. make sure that the path to the PDF you're loading in the PostScript command/script is in the approved list a.k.a. permission list.
给定
最后,将命令行中提供的路径(例如-I,-sFONTPATH参数中的路径)添加到允许的阅读列表中.同样,初始化期间从Fontmap,cidfmap和特定于平台的字体文件枚举(例如Unix系统上的fontconfig)读取的路径会自动添加到允许读取列表中.
Finally, paths supplied on the command line (such as those in -I, -sFONTPATH parameters) are added to the permitted reading list. Similarly, paths read during initialisation from Fontmap, cidfmap, and the platform specific font file enumeration (e.g. fontconfig on Unix systems) are automatically added to the permit read lists.
另一个令我惊讶的是
Another surprise for me was that 即在 i.e. specifying the source path of the PDF in the 仅在 This problem only occurs with file loads inside 在此命令行中(请注意,缺少的 When this commandline (note the missing 为您提供无效的文件访问"错误,如下所示: gives you the "invalid file access" error as in: 比您可能有一个 than you quite probably have a 在此命令行下: 可以,那么可以肯定的是,您需要上述两种解决方案来摆脱 works okay, then that's a sure sign you need either of the solutions described above to get rid of the 我在SO和其他地方都讨论了很多讨论,包括各种bug跟踪器,但似乎所有地方都没有提到这一点,因为所有这些页面/条目都来自GhostScript 9.50和2019AD之前的版本. As I've looked at quite a few discussions, both here at SO and elsewhere, including various bugtrackers, nobody seems to mention this anywhere as all those pages/entries are from before GhostScript release 9.50 and 2019AD. Quoting from the 9.50 change notes: 文件访问控制功能(由 The file access control capability (enable with 重要的是要注意, It is important to note that 重要:默认情况下,文件访问控制已启用.为了在没有这些控件的情况下运行Ghostscript,请参见 IMPORTANT: File access controls are now enabled by default. In order to run Ghostscript without these controls, see Windows用户重要提示:请参见以下不兼容的更改 The 这篇关于GhostScript:错误:--file--中的/invalidfileaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
注意/除了像这样使用
-I
时的期望值:Note / Aside about expectations when using
-I
like that:gs -q -I d:/ -dNODISPLAY -c "(test.pdf) (r) file runpdfbegin pdfpagecount = quit"
-I
包含集中指定PDF的源路径,然后在PostScript命令中忽略它, still 给了我/invalidfileaccess
,因此请确保指定一个正确的绝对路径用于您要加载的PDF.-I
include set and then omitting it in the PostScript command, still gave me an /invalidfileaccess
so make sure to specify a proper absolute path for the PDF you're loading.-c
PostScript命令内部加载文件时出现此问题,而对于使用-f <file>
直接在命令行上指定的PDF/PS源文件则不是.-c
PostScript commands, not with PDF/PS source files specified directly on the commandline using -f <file>
.-Id:/
或--permit-file-read=d:/
是唯一的更改):-Id:/
or --permit-file-read=d:/
is the only change):gs -q -dNODISPLAY -c "(d:/test.pdf) (r) file runpdfbegin pdfpagecount = quit"
Error: /invalidfileaccess in --file--
Operand stack:
(d:/test.pdf) (r)
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval--
Dictionary stack:
--dict:737/1123(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)--
Current allocation mode is local
Last OS error: Permission denied
GPL Ghostscript 9.52: Unrecoverable error, exit code 1
-dSAFER
问题,该问题已通过以下方式解决:将上述 PDF源目录添加到可接受的路径列表中(另请参见-dSAFER
problem which is fixed by the above addition of the PDF source directory to the accepted paths list (see also the GhostScript documentation at 'NOSAFER' and onwards including the section about --permit-file-read=pathlist
et al.gs -q -dNOSAFER -dNODISPLAY -c "(d:/test.pdf) (r) file runpdfbegin pdfpagecount = quit"
/invalidfileaccess
错误./invalidfileaccess
error.
-dSAFER
) has been completely rewritten, with a ground-up rethink of the design. For more details, see: SAFER.-dSAFER
现在仅启用文件访问控制,并且不再对标准Postscript功能应用限制(特别是对setpagedevice
的限制).如果您的应用程序依赖于这些Postscript限制,请参见OLDSAFER
,并与我们取得联系,因为我们确实打算删除这些Postscript限制,除非我们有理由不这样做.-dSAFER
now only enables the file access controls, and no longer applies restrictions to standard Postscript functionality (specifically, restrictions on setpagedevice
). If your application relies on these Postscript restrictions, see OLDSAFER
, and please get in touch, as we do plan to remove those Postscript restrictions unless we have reason not to.NOSAFER
NOSAFER
SAFER
的更改从2019-09-30(版本9.50)起SAFER
change is from 2019-09-30 (release 9.50)