@cocalc/backend
Version:
CoCalc backend functionality: functionality used by either the hub, the next.js server or the project.
74 lines (72 loc) • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.envForSpawn = exports.process_env_int = exports.is_valid_username = exports.contains_url = exports.getUid = void 0;
const crypto_1 = require("crypto");
const misc_1 = require("@cocalc/util/misc");
/*
getUid
We take the sha-512 hash of the project_id uuid just to make it harder to force
a collision. Thus even if a user could somehow generate an account id of their
choosing, this wouldn't help them get the same uid as another user. We use
this approach only a single Linux system, so are only likely to have a handful
of accounts anyways, and users are mostly trusted.
- 2^31-1=max uid which works with FUSE and node (and Linux, which goes up to 2^32-2).
- 2^29 was the biggest that seemed to work with Docker on my crostini pixelbook,
so shrinking to that.
- it is always at least 65537 to avoid conflicts with existing users.
*/
function getUid(project_id) {
if (!(0, misc_1.is_valid_uuid_string)(project_id)) {
throw Error(`project_id (=${project_id}) must be a valid v4 uuid`);
}
const sha512sum = (0, crypto_1.createHash)("sha512");
let n = parseInt(sha512sum.update(project_id).digest("hex").slice(0, 8), 16); // up to 2^32
n = Math.floor(n / 8); // floor division
if (n > 65537) {
return n;
}
else {
return n + 65537;
} // 65534 used by linux for user sync, etc.
}
exports.getUid = getUid;
const misc_2 = require("@cocalc/util/misc");
var misc_3 = require("@cocalc/util/misc");
Object.defineProperty(exports, "contains_url", { enumerable: true, get: function () { return misc_3.contains_url; } });
// returns undefined if ok, otherwise an error message.
// TODO: this should probably be called "validate_username". It's only used in @cocalc/database right now
// as a backend double check on the first and last name when creating accounts in the database.
function is_valid_username(str) {
const name = str.toLowerCase();
const found = name.match(misc_2.re_url);
if (found) {
return `URLs are not allowed. Found ${(0, misc_2.to_human_list)(found)}`;
}
if (name.indexOf("mailto:") != -1 && name.indexOf("@") != -1) {
return "email addresses are not allowed";
}
return;
}
exports.is_valid_username = is_valid_username;
// integer from process environment variable, with fallback
function process_env_int(name, fallback) {
const val = process.env[name];
if (val == null)
return fallback;
try {
return parseInt(val);
}
catch {
return fallback;
}
}
exports.process_env_int = process_env_int;
function envForSpawn() {
const env = { ...process.env };
for (const name of ["DEBUG", "DEBUG_CONSOLE", "NODE_ENV", "DEBUG_FILE"]) {
delete env[name];
}
return env;
}
exports.envForSpawn = envForSpawn;
//# sourceMappingURL=misc.js.map