What is a secure context?
A secure context is a browsing context that meets certain minimum browser security requirements.
In modern browsers, your site can access powerful APIs only if you load it in a secure context (window.isSecureContext).
If you don’t load your site in a secure context, those APIs are unavailable or partially degraded.
When is a browser context considered secure?
A browsing context is considered secure when its document is loaded securely. A document is loaded securely in these cases:
-
The document is loaded over HTTPS.
-
The document is loaded from
localhost.
|
If your application runs in an |
Why does LuciadRIA require a secure context?
LuciadRIA uses WebGPU for hardware-accelerated rendering. Browsers restrict usage of the WebGPU API to secure contexts.
Running your application in a secure context
To run your web application in a secure context, you can choose to:
-
Serve your application over HTTPS, mostly relevant for deployment.
-
Serve your application from
localhost, typically relevant for local development only.
Secure the browsing context for local development
When you’re developing locally, the easiest way to secure the browsing context is to serve your application from localhost.
This is also the default configuration of the LuciadRIA sampleserver in the release,
as well as the Vite dev server used for development in LuciadRIA samples.
If you want to access your application from a different host name, you can set up HTTPS for local development.
Secure the browsing context for deployment
When deploying your application, you must serve it over HTTPS. You typically need to obtain a certificate, which your organization may already have, and configure your server to use it. An alternative is running your server behind a reverse proxy that handles HTTPS.
Checking if your application is running in a secure context
You can verify that your application is running in a secure context by checking the value of window.isSecureContext in your browser’s developer console.
If it returns true, your application is running in a secure context.
If it returns false, your application isn’t running in a secure context.
if (!window.isSecureContext) {
console.warn("Not a secure context. WebGPU unavailable.");
}
if (!("gpu" in navigator)) {
console.info("WebGPU not supported or not enabled.");
}
Setting up HTTPS for local development
To set up HTTPS for local development, you need to:
-
Configure the Vite dev server for HTTPS, using either an automatically generated certificate or your own certificate.
-
(Optional) Configure the LuciadRIA sample server for HTTPS, reusing that same certificate.
-
Trust the self-signed certificate in your browser, so your browser accepts the certificate and treats the connection as a secure context.
|
Your organization may already have a trusted certificate that you can use for local development instead of a self-signed one. |
Configure the Vite dev server for HTTPS
If you’re using the Vite dev server that comes with the LuciadRIA samples, you can enable HTTPS with an automatically generated certificate or with your own certificate.
Using an automatically generated certificate
Enable HTTPS and generate a self-signed certificate for localhost by setting the RIA_VITE_HTTPS environment variable before running npm run dev:
# On Mac and Linux:
RIA_VITE_HTTPS=1 npm run dev
# On Windows (cmd.exe):
set RIA_VITE_HTTPS=1&&npm run dev
This uses the @vitejs/plugin-basic-ssl plugin to generate the certificate, cached at node_modules/.vite-samples-cache/basic-ssl/_cert.pem in the LuciadRIA distribution root.
The same certificate is reused across dev server restarts, as long as it hasn’t expired.
Using your own certificate
If you already have a certificate, for example, one issued by your organization, or you want more control over the certificate
than the automatically generated one offers, you can configure Vite’s native server.https option in your sample’s own vite.config.ts file instead.
-
If you don’t have a certificate yet, generate a self-signed one for
localhost:-
Create a
localhost.cnffile with this content:[dn] CN=localhost [req] distinguished_name = dn [EXT] subjectAltName=DNS:localhost keyUsage=digitalSignature extendedKeyUsage=serverAuth -
Run this
opensslcommand to generate a private key and self-signed certificate forlocalhost:openssl req -x509 -out localhost.crt -keyout localhost.key \ -newkey rsa:2048 -nodes -sha256 \ -days 365 \ -subj "/CN=localhost" -extensions EXT -config localhost.cnfThe result is a private key in
localhost.keyand a self-signed certificate inlocalhost.crt, valid for 365 days.If you’re using a different host name, you must generate a certificate for that host name instead. For example, if you’re using
mymachine.local, replacelocalhostwithmymachine.localin theopensslcommand above.
-
-
Add
@types/nodeas a dev dependency, since reading the certificate files uses Node’sfsmodule:npm install @types/node --save-dev -
Merge the
server.httpsoption into your sample’svite.config.ts:import fs from "fs"; import {mergeConfig} from "vite"; import createSampleViteConfig from "@luciad/ria-toolbox-config/vite.config.ts"; const baseConfig = createSampleViteConfig(import.meta.url); export default (env) => mergeConfig(baseConfig(env), { server: { https: { key: fs.readFileSync("path/to/localhost.key"), cert: fs.readFileSync("path/to/localhost.crt"), }, }, });
Consult the Vite server.https documentation for more information.
Configure the LuciadRIA sample server for HTTPS
To enable HTTPS on the LuciadRIA sample server, you must create a keystore containing a certificate and private key.
If you used the automatically generated certificate, reuse it: _cert.pem at node_modules/.vite-samples-cache/basic-ssl/_cert.pem is a single file containing both the certificate and the private key, so pass it to both -in and -inkey:
openssl pkcs12 -export -in node_modules/.vite-samples-cache/basic-ssl/_cert.pem -inkey node_modules/.vite-samples-cache/basic-ssl/_cert.pem -out localhost.p12 -name localhost
If you used your own certificate instead, reuse its certificate and key files:
openssl pkcs12 -export -in localhost.crt -inkey localhost.key -out localhost.p12 -name localhost
Then, you must set these environment variables to enable HTTPS in the sample server:
-
RIA_SAMPLE_SERVER_HTTPS=trueTo enable HTTPS, defaults to false. -
RIA_SAMPLE_SERVER_KEYSTORE=path/to/localhost.p12Required for HTTPS, the path to the keystore file you created. -
RIA_SAMPLE_SERVER_KEYSTORE_PASSWORD=<password you used when creating the keystore>Required for HTTPS. The password you used when creating the keystore -
RIA_SAMPLE_SERVER_KEYSTORE_TYPE=<keystore type">Optional. The type of the keystore, defaults to"PKCS12". -
RIA_SAMPLE_SERVER_KEYSTORE_ALIAS=<alias>Optional. The name of the certificate in the keystore. The same as the-namevalue in theopensslcommand above, defaults to"localhost".
You can set these variables in the startSampleServer.sh or startSampleServer.bat script.
Alternatively, you can configure them system-wide in your environment, so you don’t have to set them every time you install
a new LuciadRIA release.
|
After enabling HTTPS, make sure to access the sampleserver using |
Trust the self-signed certificate in your browser
To prevent browser warnings about untrusted certificates, you must trust the self-signed certificate in your browser.
The browser you’re using determines the steps required for trusting self-signed certificates:
Google Chrome
-
Open
chrome://certificate-manager. -
Click
Local certificates. -
Click
Installed by youunderCustom. -
Under
Trusted Certificates, clickImportand select the certificate file you generated in Configure the Vite dev server for HTTPS (_cert.pemorlocalhost.crtif you used your own certificate).
Mozilla Firefox
-
Make sure that the web server is running with the self-signed certificate.
-
Open
about:preferences#privacyin Firefox. -
Scroll down to
Certificatesand clickView Certificates…​. -
In the
Serverstab, clickAdd Exception…​. -
Enter the URL of the web server, for example
https://localhost:3001, in theLocationfield and clickGet Certificate. -
Select the
Permanently store this exceptioncheckbox and clickConfirm Security Exception.
Safari
-
Open the certificate file you generated in Configure the Vite dev server for HTTPS (
_cert.pemorlocalhost.crtif you used your own certificate). This should open the Keychain app. -
Add the certificate to the
Systemkeychain. -
In the Keychain app, select
Systemin the left sidebar. -
Find the
localhostcertificate in the list on theCertificatestab and double-click it. -
In the certificate details, expand the
Trustsection. -
Set
When using this certificatetoAlways Trust. -
Close the certificate details and enter your password to save the changes.
After trusting the certificate, you can access your application without any warnings.