tabris-js icon indicating copy to clipboard operation
tabris-js copied to clipboard

Trusting certificates does not work anymore

Open cpetrov opened this issue 2 years ago • 0 comments

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

  1. 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
  1. Map the domain name foo.bar to 127.0.0.1:
echo "127.0.0.1 foo.bar" | sudo tee -a /etc/hosts
  1. 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" }
    });
  }
);
  1. 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);

cpetrov avatar Sep 06 '23 09:09 cpetrov