Skip to content

Security in Scripts

Scripts can read and change data that the users of a process cannot see. This page explains which scripts can be trusted, where permission checks belong, and how to handle credentials and user input.

Where Scripts Run

Script Runs on Can be changed by the user
Form scripts and form rules The user's browser Yes
Prework, postwork and other workflow scripts The server No
Module functions called from a form The server The arguments, yes

A user can change or skip code that runs in the browser, and can send any value to the server. Form rules improve the experience of filling a form, but they do not protect data. Repeat every check that matters in a server script; see Checking Data.

Server Scripts Are Not Limited by User Permissions

Server scripts are not limited by the permissions of the user who completed the task. Work item scripts run in a background job of the domain, and services such as $Database and $Documents do not check the user's access rights. A script can read documents and data rows that the user cannot open.

Therefore, a script that returns or copies data on behalf of a user must decide itself whether the user may see that data. Return only what the user needs, not whole records.

Checking the Caller of a Module Function

A script module function can be called from a form. The caller chooses the arguments, so treat them as untrusted input. Check the user with $ActiveUser and $Domain.HasPermission before returning or changing data:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
exports.GetSalaryHistory = function (employeeId) {
    var READ = 1;

    if (employeeId !== $ActiveUser.Id && !$Domain.HasPermission($ActiveUser, 'HumanResources', READ)) {
        throw new Error('You are not allowed to view this salary history.');
    }

    return $Database.Get({
        TargetSchema : 'HR',
        TargetTable : 'SalaryHistory',
        Where : {
            Criteria : [
                { Expression : 'Employee', Value : employeeId }
            ]
        }
    }).Map(function () {
        return { date : this.ChangeDate, salary : this.Salary };
    });
};

$ActiveUser is available when the module function is called on behalf of a user. It is not guaranteed in workflow scripts; use the work item and instance objects there.

Keeping Credentials out of Scripts

Scripts are part of the process definition. Everyone who can edit or export the process can read them. Do not write passwords, API keys, tokens or connection strings in scripts.

Do not write credentials or personal data to console, error messages or process data. Diagnostic output and errors can be read by administrators; see Debugging Scripts.

Using Input Safely

Pass values from users or external systems as parameters, not as part of a query or a URL:

1
2
3
var orders = $Database.ExecuteQuery('ErpDatabase', 'select Number, Total from Orders where CustomerCode = @Customer', {
    Customer : $Xml.Evaluate('Customer/Code')
});