Security
JavaScript in PDF documents operates under a security model that protects users from potentially harmful operations. Certain functions are marked as "secure" and can only be executed in trusted contexts.
Secure vs. non-secure functions
Non-secure functions can be called from any context:
-
Reading form field values
-
Basic calculations
-
Displaying alerts
-
Navigating pages
-
Form manipulations
Secure functions require elevated privileges:
-
File system operations (util.readFileIntoStream, util.writeToFile)
-
Launching URLs (app.launchURL)
-
Path operations (app.getPath)
-
Data import/export operations
Event and document scripts
Both run in an untrusted context and cannot execute secure functions directly:
// Button event or document script - WILL FAIL
var stream = util.readFileIntoStream("/C/data.txt") // Security error!
Folder-level scripts
Can define trusted functions that execute secure operations:
// Folder-level script - CAN define trusted functions
var trustedReadFile = app.trustedFunction(function(path) {
app.beginPriv() // Elevate privileges
var stream = util.readFileIntoStream(path)
app.endPriv() // Return to normal
return stream
})
This pattern uses app.trustedFunction, app.beginPriv, and app.endPriv to handle secure operations.
The pattern for trusted functions:
var myTrustedFunction = app.trustedFunction(function(parameters) {
app.beginPriv() // Start privileged block
// Secure operations here
var result = someSecureOperation()
app.endPriv() // End privileged block
return result
})
Key points:
-
Must be defined in folder-level scripts
-
app.beginPriv() and app.endPriv() wrap secure operations
-
Can be called from untrusted contexts
Helper functions that need privileges use app.trustPropagatorFunction():
// Helper that needs privileges
var trustedHelper = app.trustPropagatorFunction(function() {
app.beginPriv()
var path = app.getPath("user", "documents")
app.endPriv()
return path
})
// Main trusted function
var trustedMain = app.trustedFunction(function() {
app.beginPriv()
var path = trustedHelper() // Helper inherits trust
// Do something with path
app.endPriv()
})
Besides folder-level scripts, trust can be established through:
-
Trusted Locations: Mark folders as trusted in Revu preferences
-
Certified PDFs: Digitally signed PDFs with trusted certificates
These are configured through the Revu user interface, not JavaScript.
Quick reference
Establishing trust:
-
Only possible in folder-level scripts
Common secure operations:
-
File I/O
-
System paths
-
URL launching
-
Data import/export
Moving forward
Understanding trusted functions and the security model enables powerful automation while maintaining safety. Folder-level scripts are the key to bridging the gap between untrusted PDF contexts and secure system operations. Next up we will look at a few more PDF-specific concepts you will need.
Resources
Revu 21
Revu JavaScript
JavaScript