firebase-tools
Version:
Command-Line Interface for Firebase
192 lines (191 loc) • 6.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLocalConfigPath = getLocalConfigPath;
exports.getAllLocalTokens = getAllLocalTokens;
exports.getAllLocalTokenNames = getAllLocalTokenNames;
exports.getLocalToken = getLocalToken;
exports.getLocalTokenName = getLocalTokenName;
exports.saveLocalToken = saveLocalToken;
exports.removeLocalTokens = removeLocalTokens;
const Configstore = require("configstore");
const fs = require("fs");
const os = require("os");
const path = require("path");
const error_1 = require("../error");
const CONFIGSTORE_ID = "firebase-appcheck";
const WARNING_COMMENT = "This file contains secrets and should not be committed to source repositories";
const FORBIDDEN_KEYS = new Set(["__proto__", "constructor", "prototype"]);
const MANAGED_TOP_LEVEL_KEYS = new Set(["_comment", "tokens", "tokenNames"]);
function getCanonicalConfigDir() {
return path.join(os.homedir(), ".config");
}
function areSameDirectory(dirA, dirB) {
const resolvedA = path.resolve(dirA);
const resolvedB = path.resolve(dirB);
const caseInsensitive = process.platform === "win32" || process.platform === "darwin";
if (caseInsensitive ? resolvedA.toLowerCase() === resolvedB.toLowerCase() : resolvedA === resolvedB) {
return true;
}
try {
if (fs.existsSync(resolvedA) && fs.existsSync(resolvedB)) {
return fs.realpathSync(resolvedA) === fs.realpathSync(resolvedB);
}
}
catch {
}
return false;
}
function getConfigStorePaths() {
const canonicalDir = getCanonicalConfigDir();
const canonicalPath = path.join(canonicalDir, "configstore", `${CONFIGSTORE_ID}.json`);
const xdgConfigHome = process.env.XDG_CONFIG_HOME?.trim();
if (xdgConfigHome &&
path.isAbsolute(xdgConfigHome) &&
!areSameDirectory(xdgConfigHome, canonicalDir)) {
const xdgPath = path.join(xdgConfigHome, "configstore", `${CONFIGSTORE_ID}.json`);
return [canonicalPath, xdgPath];
}
return [canonicalPath];
}
function extractStringRecord(raw) {
if (!(0, error_1.isObject)(raw) || Array.isArray(raw)) {
return {};
}
const result = {};
for (const [key, value] of Object.entries(raw)) {
if (FORBIDDEN_KEYS.has(key)) {
continue;
}
if (typeof value === "string" && value.length > 0) {
result[key] = value;
}
}
return result;
}
function readConfigFile(configPath) {
let content;
try {
content = fs.readFileSync(configPath, "utf8");
}
catch (err) {
if (err?.code === "ENOENT") {
return undefined;
}
throw new error_1.FirebaseError(`Failed to read local App Check config at ${configPath}: ${(0, error_1.getErrMsg)(err)}`, { exit: 1, original: err instanceof Error ? err : undefined });
}
if (!content.trim()) {
return undefined;
}
let parsed;
try {
parsed = JSON.parse(content);
}
catch (err) {
throw new error_1.FirebaseError(`Failed to parse local App Check config at ${configPath}: ${(0, error_1.getErrMsg)(err)}. Fix or delete the file and try again.`, { exit: 1, original: err instanceof Error ? err : undefined });
}
if (!(0, error_1.isObject)(parsed) || Array.isArray(parsed)) {
throw new error_1.FirebaseError(`Invalid local App Check config at ${configPath}: expected a top-level JSON object.`, { exit: 1 });
}
return parsed;
}
function readMergedConfig() {
const tokens = {};
const tokenNames = {};
const extraFields = {};
const paths = getConfigStorePaths();
for (let i = paths.length - 1; i >= 0; i--) {
const raw = readConfigFile(paths[i]);
if (!raw) {
continue;
}
for (const [key, value] of Object.entries(raw)) {
if (!MANAGED_TOP_LEVEL_KEYS.has(key) && !FORBIDDEN_KEYS.has(key)) {
extraFields[key] = value;
}
}
Object.assign(tokens, extractStringRecord(raw.tokens));
Object.assign(tokenNames, extractStringRecord(raw.tokenNames));
}
for (const appId of Object.keys(tokenNames)) {
if (!Object.prototype.hasOwnProperty.call(tokens, appId)) {
delete tokenNames[appId];
}
}
return { tokens, tokenNames, extraFields };
}
function writeLocalConfig(config) {
const payload = {
...config.extraFields,
_comment: WARNING_COMMENT,
tokens: config.tokens,
};
if (Object.keys(config.tokenNames).length > 0) {
payload.tokenNames = config.tokenNames;
}
for (const configPath of getConfigStorePaths()) {
const store = new Configstore(CONFIGSTORE_ID, undefined, { configPath });
store.all = payload;
try {
fs.chmodSync(configPath, 0o600);
}
catch {
}
}
}
function getLocalConfigPath() {
return path.join(getCanonicalConfigDir(), "configstore", `${CONFIGSTORE_ID}.json`);
}
function getAllLocalTokens() {
return readMergedConfig().tokens;
}
function getAllLocalTokenNames() {
return readMergedConfig().tokenNames;
}
function getLocalToken(appId) {
if (FORBIDDEN_KEYS.has(appId)) {
return undefined;
}
const tokens = getAllLocalTokens();
return Object.prototype.hasOwnProperty.call(tokens, appId) ? tokens[appId] : undefined;
}
function getLocalTokenName(appId) {
if (FORBIDDEN_KEYS.has(appId)) {
return undefined;
}
const tokenNames = getAllLocalTokenNames();
return Object.prototype.hasOwnProperty.call(tokenNames, appId) ? tokenNames[appId] : undefined;
}
function saveLocalToken(appId, token, resourceName) {
if (!appId || FORBIDDEN_KEYS.has(appId)) {
throw new error_1.FirebaseError(`Invalid Firebase App ID "${appId}".`, { exit: 1 });
}
const config = readMergedConfig();
config.tokens[appId] = token;
if (resourceName) {
config.tokenNames[appId] = resourceName;
}
else {
delete config.tokenNames[appId];
}
writeLocalConfig(config);
}
function removeLocalTokens(appIds) {
if (appIds.length === 0) {
return;
}
const config = readMergedConfig();
let modified = false;
for (const appId of appIds) {
if (FORBIDDEN_KEYS.has(appId)) {
continue;
}
if (Object.prototype.hasOwnProperty.call(config.tokens, appId)) {
delete config.tokens[appId];
delete config.tokenNames[appId];
modified = true;
}
}
if (modified) {
writeLocalConfig(config);
}
}