Client Parameters (Beta)
Pass parameters from the client directly for use in Sync Rules.
This feature is currently in a beta release. It is suitable for production use if you have tested your use cases extensively.
Use client parameters with caution. Please make sure to read the Security consideration section below.
Client parameters are parameters that are passed to the PowerSync Service instance from the client SDK, and can be used in Sync Rules’ parameter queries to further filter data.
PowerSync already supports using token parameters in parameter queries. An example of a token parameter is a user ID, and this is commonly used to filter synced data by the user. These parameters are embedded in the JWT authentication token, and therefore can be considered trusted and can be used for access control purposes.
Client parameters are specified directly by the client (i.e. not through the JWT authentication token). The advantage of client parameters is that they give client-side control over what data to sync, and can therefore be used to further filter or limit synced data. A common use case is lazy-loading, where data is split into pages and a client parameter can be used to specify which page(s) to sync to a user, and this can update dynamically as the user paginates (or reaches the end of an infinite-scrolling feed).
Usage
Client parameters are defined when instantiating the PowerSync database, within the options of PowerSync’s connect()
method:
The parameter is then available in Sync Rules under request.parameters
(alongside the already supported request.user_id
).
In this example, only ‘posts’ from the user’s current page are synced:
Security consideration
An important consideration with client parameters is that a client can pass any value, and sync data accordingly. Hence, client parameters should always be treated with care, and should not be used for access control purposes. Where permissions are required, use token parameters (request.jwt()
) instead, or use token parameters in combination with client parameters.
The following examples show secure vs. insecure ways of using client and token parameters:
Secure (using a token parameter only):
Insecure (using a client parameter only):
Secure (using a token parameter combined with a client parameter):
Warning on potentially dangerous queries
Based on the above security consideration, the PowerSync Dashboard will warn developers when client parameters are being used in sync rules in an insecure way (i.e. where the query does not also include a parameter from request.jwt()
).
The below sync rules will display the warning:
Potentially dangerous query based on parameters set by the client. The client can send any value for these parameters so it’s not a good place to do authorization.
This warning can be disabled by specifying accept_potentially_dangerous_queries: true
in the bucket definition:
Was this page helpful?