refactor: switch to loading kernel as a Blob with script injection, and do not use in-memory variables to pass data. Additionally, fix reading of code with readableStreamToBlob

This commit is contained in:
Derrick Hammer 2023-07-18 17:11:42 -04:00
parent 6a66f5db58
commit 825db849e2
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 33 additions and 20 deletions

View File

@ -34,6 +34,7 @@
"@noble/ciphers": "^0.1.4", "@noble/ciphers": "^0.1.4",
"@peculiar/webcrypto": "^1.4.3", "@peculiar/webcrypto": "^1.4.3",
"@scure/bip39": "^1.2.1", "@scure/bip39": "^1.2.1",
"binconv": "^0.2.0",
"ed25519-keygen": "^0.4.1", "ed25519-keygen": "^0.4.1",
"ejs": "^3.1.9", "ejs": "^3.1.9",
"file-type": "^18.5.0", "file-type": "^18.5.0",

View File

@ -12,9 +12,9 @@ import {
setBootloaderPortals, setBootloaderPortals,
setKernelLoaded, setKernelLoaded,
setLoginComplete, setLoginComplete,
setUserKey,
} from "./vars.js"; } from "./vars.js";
import { getStoredUserKey } from "./storage.js"; import { getStoredUserKey } from "./storage.js";
import { readableStreamToBlob } from "binconv";
import { handleIncomingMessage } from "./messages.js"; import { handleIncomingMessage } from "./messages.js";
export function boot() { export function boot() {
@ -29,7 +29,6 @@ export function boot() {
} }
log("user is already logged in, attempting to load kernel"); log("user is already logged in, attempting to load kernel");
setUserKey(userKey);
setActivePortalMasterKey(userKey); setActivePortalMasterKey(userKey);
setLoginComplete(true); setLoginComplete(true);
sendAuthUpdate(); sendAuthUpdate();
@ -59,7 +58,31 @@ export async function loadKernel() {
setBootloaderPortals(getActivePortals()); setBootloaderPortals(getActivePortals());
try { try {
eval(kernelCode); await new Promise(async (resolve, reject) => {
const url = URL.createObjectURL(await readableStreamToBlob(kernelCode));
const el = document.createElement("script");
el.src = url;
el.onload = () => {
URL.revokeObjectURL(url);
resolve(null);
};
el.onerror = (
event: Event | string,
source?: string,
lineno?: number,
colno?: number,
error?: Error,
) => {
URL.revokeObjectURL(url);
reject(error);
};
document.head.appendChild(el);
});
window.removeEventListener("message", handleIncomingMessage);
setKernelLoaded("success"); setKernelLoaded("success");
sendAuthUpdate(); sendAuthUpdate();
log("kernel successfully loaded"); log("kernel successfully loaded");
@ -78,16 +101,18 @@ export async function loadKernel() {
async function downloadKernel( async function downloadKernel(
kernelCid: string, kernelCid: string,
): Promise<[kernelCode: string, err: Err]> { ): Promise<[kernelCode: ReadableStream, err: Err]> {
const [code, err] = await downloadObject(kernelCid); const [code, err] = await downloadObject(kernelCid);
if (err != null) { if (err != null) {
return ["", err]; return [null as any, err];
} }
return [code, null]; return [code, null];
} }
function downloadDefaultKernel(): Promise<[kernelCode: string, err: Err]> { function downloadDefaultKernel(): Promise<
[kernelCode: ReadableStream, err: Err]
> {
return downloadKernel(defaultKernelLink); return downloadKernel(defaultKernelLink);
} }

View File

@ -4,7 +4,6 @@ import {
getLogoutComplete, getLogoutComplete,
setLoginComplete, setLoginComplete,
setLogoutComplete, setLogoutComplete,
setUserKey,
} from "./vars.js"; } from "./vars.js";
import { log, logErr, reloadKernel, sendAuthUpdate } from "./util.js"; import { log, logErr, reloadKernel, sendAuthUpdate } from "./util.js";
import { loadKernel } from "./kernel.js"; import { loadKernel } from "./kernel.js";
@ -38,8 +37,6 @@ function handleStorage(event: StorageEvent) {
return; return;
} }
setUserKey(userKey);
log("user is now logged in, attempting to load kernel"); log("user is now logged in, attempting to load kernel");
setLoginComplete(true); setLoginComplete(true);
loadKernel(); loadKernel();

View File

@ -4,12 +4,9 @@ import { x25519 } from "@noble/curves/ed25519";
let loginComplete = false; let loginComplete = false;
let logoutComplete = false; let logoutComplete = false;
let kernelLoaded = "not yet"; let kernelLoaded = "not yet";
let bootloaderPortals: Client[] = [];
let communicationKey: Uint8Array; let communicationKey: Uint8Array;
let frontendCommunicationPubKey: Uint8Array; let frontendCommunicationPubKey: Uint8Array;
var userKey: Uint8Array;
export const defaultKernelLink = export const defaultKernelLink =
"zduFSV7fLr7GeucUQFgxzq877rmMAbALz19nDHMtBEwz1eoGcKeQt3Lzfh"; "zduFSV7fLr7GeucUQFgxzq877rmMAbALz19nDHMtBEwz1eoGcKeQt3Lzfh";
@ -32,15 +29,8 @@ export function setKernelLoaded(status: string) {
export function getKernelLoaded() { export function getKernelLoaded() {
return kernelLoaded; return kernelLoaded;
} }
export function setUserKey(key: Uint8Array) {
userKey = key;
}
export function getUserKey() {
return userKey;
}
export function setBootloaderPortals(portals: Client[]) { export function setBootloaderPortals(portals: Client[]) {
bootloaderPortals = portals; window.bootloaderPortals = portals;
} }
export function getCommunicationKey() { export function getCommunicationKey() {