1# [[plugins/contrib/bibtex2html]] and [[plugins/contrib/compile]] plugins 2 3*Answer to [[anarcat]] mentionning [[plugins/contrib/compile]] in the [[plugins/contrib/bibtex2html]] documentation.* 4 5I do not think that the [[plugins/contrib/compile]] plugin can replace the [[plugins/contrib/bibtex2html]] plugin right now: the [[plugins/contrib/compile]] plugin compiles a document, and renders *an HTML link to* the compiled document, whereas (if I am not wrong), the [[plugins/contrib/bibtex2html]] plugin compiles a document, and renders *the content of* the compiled document (which happens to be some HTML code). 6 7Right now, it is not possible for the [[plugins/contrib/compile]] plugin to render the *content* of the compiled document. This could be done by providing a `DESTCONTENT` template variable, containing the content of the compiled document. This should not be hard to implement. 8 9-- [[Louis|spalax]] (author of [[plugins/contrib/compile]]) 10 11> Interesting! I am thinking of writing a simpler plugin (maybe called "exec") that would be a merge of the two approaches. There would be an `htmlize` hook to render arbitrary page extensions (based on the configuration) into anything with predefined pipelines. Then a `preprocess` hook would allo directives to inject links to documents or simply inline them like bibtex2html does. The plugin could be secure insofar as the pipelines configured are secure as well. Should that be merged in compile or be a separate plugin? --[[anarcat]] 12 13>> This "arbitrary executable" stuff scares me, and I'm not going to merge anything 14>> like that without a relatively paranoid review. As a result, it could take a while. 15>> 16>> At some point when I have more time and energy I should try to write down what 17>> ikiwiki's security model is. The short version is that the plugins shipped 18>> with ikiwiki should never let wiki editors execute arbitrary code, even if they 19>> have direct VCS access or can alter "safe"-flagged setup options. The ability to 20>> alter non-"safe" setup options is equivalent to access to the uid running the 21>> wiki, and so is the ability to alter the plugins that the wiki uses. 22>> 23>> Defining pipelines or compilation commands in the setup file does not 24>> *directly* contradict that, because the setup option would not be flagged 25>> as safe, but it does provide an easy way to cause a huge 26>> increase in attack surface, particularly when shell scripts are known to 27>> be a difficult thing to write securely. If people want arbitrary compilation, 28>> Perl or XML-RPC (e.g. Python) plugins are probably safer (even if they call 29>> external commands with `IPC::Run` or `subprocess`!), and they would mean that 30>> the authors of the arbitrary-compilation code can't have any illusions about 31>> "oh, this isn't all that security-sensitive, I'm just writing an 32>> ad-hoc command". 33>> 34>> I hope that ImageTragick is still fresh in everyone's minds - many of the 35>> individual vulnerabilities there involved ImageMagick and GraphicsMagick 36>> running arbitrary shell pipelines from delegates.xml that turned out not 37>> to be hardened against invocation by a hostile user. --[[smcv]] 38 39>>> The `exec` plugin would definitely not be marked as "safe": it 40>>> allows wiki administrators to execute arbitrary code as the wiki 41>>> user. 42>>> 43>>> That said, I believe it is possible to craft a configuration that 44>>> *would* be safe to use for *users* in general. Of course, we can't 45>>> exclude foot-shooting here: if an admin misconfigures the plugin, 46>>> they can introduce new attack vectors. But default and sample 47>>> configurations should be secure. 48>>> 49>>> It is with that model in mind that I wrote the bibtex2html plugin: it doesn't 50>>> use the shell to execute bibtex2html: 51>>> 52>>> [[!format perl """my @bibtex_cmd = (qw[bibtex2html -noheader -nofooter -nobibsource -nodoc -q -o -], $near); open(PIPE, "-|", @bibtex_cmd) || error "can't open pipe to @bibtex_cmd: $!";"""]] 53>>> 54>>> I specifically tried to address that case, to make sure users 55>>> can't execute arbitrary code even if the plugin is enabled. 56>>> 57>>> Still: it is tricky! The above pipeline could *still* be 58>>> vulnerable to certain attacks if bibtex2html does some dangerous 59>>> stuff on its own. For example, it could call other executables 60>>> with the shell without checking arguments, and then the filename 61>>> would be expanded into hostile shell commands. Even worse and 62>>> trickier, the filename could be something like `-oclobberfile` and 63>>> one file would be destroyed! 64>>> 65>>> bibtex2html is probably vulnerable to such an attack right now. We 66>>> should check attachments for weird filenames and restrict what is 67>>> allowed to upload and pass to the plugin. 68>>> 69>>> In case you haven't reviewed the [[compile]] plugin in detail, 70>>> what struck me as an interesting model is the way it allows admin 71>>> to configure extensions to pipeline mappings. What I had in mind 72>>> was something like this: 73>>> 74>>> exec_pipelines: 75>>> - bib: 'bibtex2html -o- %s' 76>>> - svg: 'inkscape -o- %s' 77>>> - tex: 78>>> - 'pdflatex %s' 79>>> - 'bibtex %s' 80>>> - 'pdflatex %s' 81>>> - 'pdflatex %s' 82>>> 83>>> (forgive my YAML cluelessness, the idea above is to define a hash 84>>> of extension -> (command) mapping.) The command would be broken up 85>>> on spaces into an array and the `%s` element would be replaced by 86>>> the source file, which would be forbidden to use shell 87>>> metacharacters, including prefixed dashes. I believe such a plugin 88>>> could be crafted to be secure with proper configuration 89>>> 90>>> Of course, it's better if there's a native plugin for 91>>> everything. For bibtex, we need to use Text::Bibtex, for 92>>> example. But that basically means rewriting bibtex2html in Perl, 93>>> which not something any user can do easily. And it's an even worse 94>>> problems for documents like Word spreadsheets or Latex 95>>> documents. Only the native commands can do the right thing. 96>>> 97>>> A clever admin can certainly find out about such a command and 98>>> having a way for that admin to easily hook that into ikiwiki would 99>>> be a powerful tool, with all that implies. --[[anarcat]] 100 101>>>> Concerning the ability to run arbitrary commands, a [[discussion was 102>>>> started|https://ikiwiki.info/plugins/contrib/compile/discussion/]] by someone 103>>>> who wanted a secure version of this plugin. The idea I had (which has some 104>>>> similarities with what is being discussed here) was to provide a 105>>>> `compile_secure` boolean option to restrict what the user can do (if 106>>>> false, users can run arbitrary commands; if true, users can only run a set of 107>>>> predefined commands). However, since [[fr33domlover]], who started the 108>>>> discussion, did not answer, nothing was implemented. 109>>>> 110>>>> Concerning arbitrary commands, I do not know Perl, but I think it can run 111>>>> commands using something similar to [exec](http://linux.die.net/man/3/exec), 112>>>> which prevents (?) shell injections. This adds the burden of manipulating 113>>>> arrays instead of strings, but security should be improved. 114>>>> 115>>>> But none of those ideas solve the problems you mentionned, being that 116>>>> external commands can do nasty things (the `-oclobberfile` option of 117>>>> `bibtex2html`) or contain bugs (like ImageMagick). 118>>>> 119>>>> If we want to merge this plugin and compile, I think a better idea than the one 120>>>> I proposed at the beginning of the discussion would be to provide two different 121>>>> directives: a `\[[!compile "foo.bar"]]` would compile the file and render it as a 122>>>> link to the compiled file (what the compile plugin does right now), while 123>>>> `\[[!render "foo.bar"]]` would compile the file, 124>>>> and render its content in the current page (whath the bibtex2html plugin 125>>>> does). In fact, providing this 126>>>> `\[[!render ...]]` directive (without the security considerations) seems 127>>>> easy enough to implement, and I might implement it some day (soon, if it 128>>>> solves [[anarcat]] problem and closes the discussion). 129>>>> 130>>>> While I am really happy to see that my plugin sparks some interest, I fear I 131>>>> won't be able to implement what is discussed here, apart from the quick 132>>>> feature I mentionned in the previous paragraph (I have a baby at home, I am 133>>>> moving to another city in a few weeks, and the only code I ever wrote in Perl 134>>>> was to contribute to IkiWiki). However, you have my blessing for making 135>>>> whatever you want with my code: contribute, write a version 2 of it, write a 136>>>> new plugin that makes it obsolete, copy the good ideas and dump the rest, etc. 137>>>> 138>>>> --[[Louis|spalax]] 139