tabris-js
tabris-js copied to clipboard
Trusting certificates does not work anymore
Problem description
Despite using app.trustedCertificates to configure a self-signed certificate as trusted, fetch() throws an error when sending a REST request to a server using that certificate:
TypeError: The operation couldn’t be completed. (NSURLErrorDomain error -1012.)
at ./src/app.js:10:8
Environment
- Tabris.js version: 3.9.0
- Device: Simulator
- OS: iOS 16.4
Code snippet
- Create a self-signed certificate for the domain name
foo.bar:
openssl genpkey -algorithm RSA -out self-signed-key.pem
# In the next step, configure the common name to be `foo.bar`
openssl req -new -x509 -days 365 -key self-signed-key.pem -out self-signed-cert.pem
openssl x509 -outform der -in self-signed-cert.pem -out self-signed-cert.der
- Map the domain name
foo.barto127.0.0.1:
echo "127.0.0.1 foo.bar" | sudo tee -a /etc/hosts
- Create a sample server using that self-signed certificate (a Deno-based service used in the example):
Deno.serve(
{
port: 8080,
cert: Deno.readTextFileSync("./self-signed-cert.pem"),
key: Deno.readTextFileSync("./self-signed-key.pem"),
},
() => {
const body = JSON.stringify({ message: "OK" });
return new Response(body, {
status: 200,
headers: { "content-type": "application/json; charset=utf-8" }
});
}
);
- In a Tabris.js app, configure the self-signed certificate to be trusted and send a request to the server:
const { app } = require("tabris");
(async () => {
const response = await fetch("./self-signed-cert.der");
if (!response.ok) throw new Error("cannot fetch cert");
app.trustedCertificates = [await response.arrayBuffer()];
fetch("https://foo.bar:8080")
.then((r) => r.json())
.then((r) => {
console.log(r);
})
.catch((e) => {
console.error(e);
});
})().catch(console.error);