Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/backend/misc.ts
Views: 687
import { createHash } from "crypto";1import { is_valid_uuid_string } from "@cocalc/util/misc";23/*4getUid56We take the sha-512 hash of the project_id uuid just to make it harder to force7a collision. Thus even if a user could somehow generate an account id of their8choosing, this wouldn't help them get the same uid as another user. We use9this approach only a single Linux system, so are only likely to have a handful10of accounts anyways, and users are mostly trusted.1112- 2^31-1=max uid which works with FUSE and node (and Linux, which goes up to 2^32-2).13- 2^29 was the biggest that seemed to work with Docker on my crostini pixelbook,14so shrinking to that.15- it is always at least 65537 to avoid conflicts with existing users.16*/17export function getUid(project_id: string): number {18if (!is_valid_uuid_string(project_id)) {19throw Error(`project_id (=${project_id}) must be a valid v4 uuid`);20}2122const sha512sum = createHash("sha512");23let n = parseInt(sha512sum.update(project_id).digest("hex").slice(0, 8), 16); // up to 2^3224n = Math.floor(n / 8); // floor division25if (n > 65537) {26return n;27} else {28return n + 65537;29} // 65534 used by linux for user sync, etc.30}3132import { re_url, to_human_list } from "@cocalc/util/misc";33export { contains_url } from "@cocalc/util/misc";3435// returns undefined if ok, otherwise an error message.36// TODO: this should probably be called "validate_username". It's only used in @cocalc/database right now37// as a backend double check on the first and last name when creating accounts in the database.38export function is_valid_username(str: string): string | undefined {39const name = str.toLowerCase();4041const found = name.match(re_url);42if (found) {43return `URLs are not allowed. Found ${to_human_list(found)}`;44}4546if (name.indexOf("mailto:") != -1 && name.indexOf("@") != -1) {47return "email addresses are not allowed";48}4950return;51}5253// integer from process environment variable, with fallback54export function process_env_int(name: string, fallback: number): number {55const val = process.env[name];56if (val == null) return fallback;57try {58return parseInt(val);59} catch {60return fallback;61}62}6364export function envForSpawn() {65const env = { ...process.env };66for (const name of ["DEBUG", "DEBUG_CONSOLE", "NODE_ENV", "DEBUG_FILE"]) {67delete env[name];68}69return env;70}717273