This topic explains how to use the secure mode feature to safely evaluate feature flags in a web browser.
Secure mode ensures that customers’ feature flag evaluations are kept private in web browser environments, and that one end user cannot inspect the variations for another end user. On an insecure device, a malicious end user could use a context or user key to identify what flag values another end user receives by analyzing the results of multiple flag evaluations. Secure mode prevents you from doing an evaluation for a context or user key that hasn’t been signed on the backend.
Secure mode is available for communication between the following JavaScript-based SDKs and LaunchDarkly:
Secure mode is not necessary for server-side SDKs.
Secure mode works when you configure your JavaScript-based SDK to include a server-generated HMAC SHA256 hash of your context key or user key. This hash is signed with the SDK key for your environment. Enabling secure mode means that every request coming from a client-side JavaScript-based SDK requires the secure mode hash to evaluate flag variations. You can pass this to your front-end code with the mechanism of your choice, such as bootstrapping or as a template variable.
To use secure mode, you must complete the following:
You can enable secure mode at any time when you use LaunchDarkly SDKs. As a best practice, we recommend that you enable secure mode during initial SDK configuration, because late-stage changes to your SDK configuration may have negative interactions with other settings.
To learn more about the secure mode hash process, read How to verify secure mode hash.
A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: “users.” To learn more, read Contexts.
Creating contexts and evaluating flags based on them is supported in the latest major versions of most of our SDKs. For these SDKs, the code samples on this page include the two most recent versions.
Details about generating a secure mode hash are available in the SDK-specific sections below:
You can use the following server-side SDKs to generate a secure mode hash:
The SecureModeHash method computes an HMAC signature of a context signed with the client’s SDK key.
Here is the method:
The SecureModeHash method computes an HMAC signature of a context signed with the client’s SDK key.
Here is the method:
The secureModeHash method computes an HMAC signature of a context signed with the client’s SDK key.
Here is the method:
The secureModeHash method computes an HMAC signature of a context signed with the client’s SDK key.
Here is the method:
The secureModeHash method computes an HMAC signature of a context signed with the client’s SDK key.
Here is the method:
The secureModeHash method computes an HMAC signature of a context signed with the client’s SDK key.
Here is the method:
The SecureModeHash method computes an HMAC signature of a context signed with the client’s SDK key.
Here is the method:
The secure_mode_hash method computes an HMAC signature of a context signed with the client’s SDK key.
Here is the method:
The secure_mode_hash method computes an HMAC signature of a context signed with the client’s SDK key.
Here is the method:
You can use the following edge SDKs to generate a secure mode hash:
The secureModeHash method computes an HMAC signature of a context signed with the SDK key.
Here is the method:
The secureModeHash method computes an HMAC signature of a context signed with the SDK key.
Here is the method:
The secureModeHash method computes an HMAC signature of a context signed with the SDK key.
Here is the method:
To compute the hash manually, locate the server-side SDK key for your environment on the SDK keys page under Settings. Then, compute an HMAC SHA256 hash of the UTF-8 encoding of your context key, using the UTF-8 encoding of your SDK key as a secret, and convert the hash to a hexadecimal string.
Here is a .NET (server-side) example:
To use secure mode, you must generate the hash based on a unique identifier for the context. This unique identifier is called a “canonical key” and is a concatenation of the kind and key attributes, separated by a colon (:). For example, if a user context has a key of “example-user-key,” this means you must generate the hash using a canonical key of user:example-user-key. If the context is a multi-context, the canonical key must include the key and kind attribute for each context kind. For example, if a multi-context contains an organization context kind and a user context kind, and they have the keys “example-organization-key” and “example-user-key”, the canonical key is organization:example-organization-key:user:example-user-key.
Secure mode is not compatible with the SDK’s ability to automatically generate keys for anonymous contexts because the SDK needs a correctly calculated hash value. To learn more, read Anonymous contexts and users.
In JavaScript-based SDKs, send the computed secure mode hash for your context as the hash attribute in the LDOptions object during client initialization, and as the hash parameter if subsequently identifying new contexts.
Specifying the computed secure mode hash is supported in the following client-side SDKs:
Here’s how to configure or send the computed secure mode hash:
Here’s how to configure or send the computed secure mode hash:
Here’s how to configure or send the computed secure mode hash:
All context-related functionality provided by the JavaScript SDK is also available in the React Web SDK.