Skip to main content
Policies are evaluated at three stages: session (connection time), pre-request (before query execution), and post-request (after data retrieval). Each stage provides different input data for making policy decisions.
For available enforcement actions at each stage, see Enforcement.

Policy Scope

By default, policies apply universally to all users, resources, and data locations. You can narrow the scope within each policy using conditions.

User Context Quick Reference

Access user information via input.user and input.end_user:
FieldTypeDescription
usernameStringUnique username
emailStringEmail address (human users only)
groups[]StringGroups the user belongs to
typeStringhuman or machine

Resource Context Quick Reference

Filter by resource via input.resource:
FieldTypeDescription
idStringResource ID
nameStringResource name
technologyStringpostgres, mongodb, snowflake, etc.
hostnameStringResource hostname
environmentStringEnvironment tag

Policy stages

Session stage

Available inputs

NameTypeDescription
policy_idSTRINGID of the policy evaluated.
applicationSTRINGName of the application.
client_ip_addressSTRINGIP address of the client.
tlsBOOLEANTells if the connection between Proxy and client is encrypted with TLS.
db_nameSTRINGName of the database.
native_userSTRINGNative user making a connection to the database.
userUser ObjectThe user making a connection to the database. User object includes name, email and groups.
end_userUser ObjectIf the user is a machine user, the end user is the user behind the application, otherwise the end user has the same value as the user.
deviceDevice ObjectDevice information.
resourceResource ObjectThe Resource being queried.
connectorConnector ObjectThe current Connector.
awsAWS ObjectAWS resources for SSH.
snowflakeSnowflake ObjectSnowflake specific information.
spaceSpace ObjectSpace specific information.

Available actions

For this policy stage, two types of actions are possible: allow and block.

Pre-request stage

Pre-request rules are evaluated by the proxy when it intercepts a request (query) just before sending it to the resource. This evaluation stage is especially useful in blocking write requests.

Available inputs

NameTypeDescription
policy_idSTRINGID of the policy evaluated.
application.nameSTRINGName of the application.
client_ip_addressSTRINGIP address of the client.
sql_querySQL Query ObjectSQL query.
querySQL Query ObjectSQL query. (alias)
db_nameSTRINGName of the database.
user_typeENUMUnderlying type used by the user to connect to the database, can be formal or native.
schema_paths[]STRINGList of the schema paths that are accessed by a query.
table_paths[]STRINGList of the table paths that are accessed by a query.
pathSTRINGPath of the resource being queried.
userUser ObjectThe user making a connection to the database. User object includes name, email and groups.
end_userUser ObjectIf the user is a machine user, the end user is the user behind the application, otherwise the end user has the same value as the user.
deviceDevice ObjectDevice information.
spaceSpace ObjectSpace information.
row[]Column ObjectRows of data being queried.
resourceResource ObjectThe Resource being queried.
columns[]Column ObjectList of columns.
connectorConnector ObjectThe current Connector.
snowflakeSnowflake ObjectSnowflake specific information.

Available actions

For this policy stage, three types of actions are possible: allow, block, and rewrite.

Post-request stage

Post-request rules are evaluated by the proxy when it intercepts data received from the resource. This evaluation stage is particularly useful in the context of masking or filtering read requests.

Available inputs

NameTypeDescription
policy_idSTRINGID of the policy evaluated.
applicationSTRINGName of the application.
client_ip_addressSTRINGIP address of the client.
sql_querySQL Query ObjectSQL query.
querySQL Query ObjectSQL query. (alias)
db_nameSTRINGName of the database.
schema_paths[]STRINGList of the schema paths that are accessed by a query.
table_paths[]STRINGList of the table paths that are accessed by a query.
table_names[]STRINGList of the table names that are accessed by a query.
columns[]Column ObjectList of columns.
userUser ObjectThe user making a connection to the database. User object includes name, email and groups.
end_userUser ObjectIf the user is a machine user, the end user is the user behind the application, otherwise the end user has the same value as the user.
deviceDevice ObjectDevice information.
resourceResource ObjectThe Resource being queried.
columns[]Column ObjectList of columns.
connectorConnector ObjectThe current Connector.
snowflakeSnowflake ObjectSnowflake specific information.
spaceSpace ObjectSpace specific information.

Available actions

For this policy stage, four types of actions are possible: allow, filter, mask, decrypt.

Standard input data

Policies can access various data types, including user information, resource information, and SQL query information. The following sections describe the available data types.

User object

ValueTypeDescription
usernameSTRINGUsername of the user
emailSTRINGIf it’s a human user, their email address
groups[]STRINGGroups in which the user is included
typeSTRINGType of the user, can be either human or machine

Connector object

ValueTypeDescription
idSTRINGID of the Connector
nameSTRINGName of the Connector

Resource object

ValueTypeDescription
idSTRINGID of the Resource
nameSTRINGName of the Resource
technologySTRINGTechnology of the Resource
hostnameSTRINGHostname of the Resource
hostname_nameSTRINGName of the Hostname of the Resource
environmentSTRINGEnvironment of the Resource
portSTRINGPort of the Resource

Column object

ValueTypeDescription
nameSTRINGName of the column
data_labelSTRINGLabel assigned to the column
json_pathSTRINGJSON path of the column
data_typeSTRINGData type of the column
valueSTRINGValue of the column
in_functions[]Function ObjectList of functions that the column is used in

Function object

ValueTypeDescription
nameSTRINGName of the function (e.g. MAX)
categories[]STRINGCategories of the function (e.g. ["aggregate", "window"])
Function categories are used to group functions into different categories. The following categories are available: aggregate, bitwise, conditional, context, conversion, data_generation, date_time, differential_privacy, encryption, file, geospatial, hash, metadata, notification, numeric, scalar, semi_structured, string_and_binary, system, table, vector_similarity, window. These categories are inspired by the Snowflake documentation.

Space object

ValueTypeDescription
nameSTRINGName of the space
idSTRINGID of the space

Native users

You can enforce policies on native users, the user used by the proxy to connect to the underlying resource. For example, here is a policy that allows the connection only if the native user is reader:
package formal.v2

import future.keywords.if
import future.keywords.in

default session := { "action": "block", "type": "block_with_formal_message" }

session := { "action": "allow", "reason": "reader native user" } if {
	input.native_user == "reader"
}

Snowflake resources

You can enforce policies on Snowflake resources based on the role used to connect to the resource.

Snowflake object

ValueTypeDescription
roleSTRINGRole used to connect to the Snowflake resource

SQL resources

Formal can also enforce policies specific to SQL queries. SQL Query Object
ValueTypeDescription
querySTRINGSQL query
statement_typeSTRINGType of the SQL statement
command_typeSTRINGType of the SQL command
limitINTLimit of the SQL query

AWS resources

Formal can also enforce policies on specific AWS Resources. These policies can be actively used for the SSH or Kubernetes.

AWS object

ValueTypeDescription
accountAccount ObjectAWS account
ecsECS ObjectConfiguration details for ECS resources
ec2EC2 ObjectConfiguration details for EC2 instances
eksEKS ObjectConfiguration details for EKS resources

ECS object

ValueTypeDescription
cluster_nameSTRINGName of the ECS cluster
service_nameSTRINGName of the ECS service
task_idSTRINGID of the ECS task
container_nameSTRINGName of the ECS container

EC2 object

ValueTypeDescription
tags[]STRINGTags of the EC2 instance
instance_idSTRINGIdentifier of the EC2 instance

EKS object

ValueTypeDescription
cluster_ arnSTRINGAmazon Resource Name (ARN) of the EKS cluster
cluster_nameSTRINGName of the EKS cluster

Device

Formal can also enforce policies on specific device attributes.
This feature is only available for devices using the Formal Desktop App.

Device info object

ValueTypeDescription
hardwareHardware Info ObjectHardware information of the device
softwareSoftware Info ObjectSoftware information of the device

Hardware info object

ValueTypeDescription
model_nameSTRINGModel name of the device
model_identifierSTRINGModel identifier of the device
model_numberSTRINGModel number of the device
system_firmware_versionSTRINGSystem firmware version of the device
os_loader_versionSTRINGOS loader version of the device
serial_numberSTRINGSerial number of the device
hardware_uuidSTRINGHardware UUID of the device
provisioning_udidSTRINGProvisioning UDID of the device
activation_lock_statusSTRINGActivation lock status of the device

Software info object

ValueTypeDescription
system_versionSTRINGSystem version of the device
kernel_versionSTRINGKernel version of the device
boot_volumeSTRINGBoot volume of the device
boot_modeSTRINGBoot mode of the device
computer_nameSTRINGComputer name of the device
user_nameSTRINGUser name of the device
secure_virtual_memorySTRINGSecure virtual memory of the device
system_integrity_protectionSTRINGSystem integrity protection of the device
time_since_bootSTRINGTime since boot of the device

Policy Input Retention

Formal can optionally retain these inputs for policy backtesting where you can test policies on historical data.

Extending Policies with External Data

You can extend policies with custom data fetched by Policy Data Loaders. The data is accessible via the data object using the loader’s configured key. Example:
# If your Policy Data Loader has key "approved_users"
data.approved_users.user_ids
For more information, see Policy Data Loaders.