Block macros and PDF JavaScript in Moodle uploads
Moodle accepts Office files with macros and PDFs with JavaScript. Our free antivirus_activecontent refuses them at upload, by content, not file name.
Short answer: a student uploads “homework.xlsx”, a teacher opens it, and a macro runs. Moodle’s antivirus support (with ClamAV) catches known malware, but a new macro or a PDF with a script is not “known”. Our free plugin, antivirus_activecontent, refuses uploads that can run code at all: Office files with macros, and PDFs with JavaScript or actions that launch another program.
This is the nineteenth plugin in our series: one plugin for each Moodle plugin type, each one teaching DevLMS Studio, our AI plugin builder, how that type works. We checked the Moodle plugins directory first: it lists two antivirus plugins (one blocks file types, one connects to Sophos), and none looks inside documents for macros or scripts.
What it refuses
- Office files with macros, in the new formats (docm, xlsm, pptm, or the same files renamed to docx, xlsx, pptx) and the old ones (doc, xls, ppt). It reads the file, not its name.
- PDFs with JavaScript or launch actions, including scripts hidden in compressed parts of the file or names written with escape codes.
The person uploading is told why: “budget.xlsm was not uploaded because it contains macros. Save it without macros or scripts and upload it again.” Administrators get Moodle’s usual antivirus notice. Each check can be turned off. It supports Moodle 4.5 LTS to 5.3. The code is on GitHub.
Some fillable PDF forms carry calculation scripts. The US tax forms we tested (1040, W-4) do, so they are refused while the PDF check is on. A plain fillable form without scripts uploads normally.
Getting it right in both directions
A blocker that refuses good files gets turned off, and one that can be tricked is worse than none. So we wrote the hard cases as tests before fixing them:
- A lecture PDF that talks about “/JavaScript” in its text was refused at first. The check now reads PDF names only, not the words on the page.
- A trick to hide a script: an opening bracket placed at the end of some binary data could make the check skip the real script that followed. Fixed and tested.
- Image data that happens to contain the letters “/JS”: found in a real research paper from arXiv. The check no longer reads image bytes.
We then ran it over real PDFs (three arXiv papers, two IRS forms and a W3C accessibility example with a form but no script): the papers and the W3C example pass, the IRS forms with scripts are refused.
How an antivirus plugin works
From Moodle 5.3’s ClamAV plugin and antivirus manager:
- An antivirus plugin is one class with one main job: look at an uploaded file and answer “clean”, “found” or “could not scan”.
- Moodle runs every enabled plugin on every upload, before the file is saved. On “found” it refuses the upload, tells the administrators, and can keep the file in quarantine.
- The plugin can write its own message for the person uploading, so they learn what to change instead of seeing “infected”.
PHPUnit has 14 tests, including an upload refused through Moodle’s own antivirus manager. Browser tests check the settings and that clean files still upload with the blocker on.
Need a plugin like this for your own Moodle?
Describe it in DevLMS Studio and watch it built and tested in a real Moodle, or ask our team for a fixed price.
Open DevLMS Studio