示例工程 46-csp-navigation-integrity

46-csp-navigation-integrity/app/shared/ipc.ts

typescript · 3.5 KB

export const CH = {
  snapshot: "integrity:snapshot",
  runCsp: "integrity:run-csp",
  navigate: "integrity:navigate",
  tamper: "integrity:tamper",
  refresh: "integrity:refresh",
} as const;

export type EventRow = {
  at: number;
  kind: string;
  detail: string;
};

export type Check = {
  id: string;
  label: string;
  pass: boolean;
  detail: string;
};

export type CspViolation = {
  directive: string;
  blockedURI: string;
  disposition: string;
};

export type CspResult = {
  id: string;
  label: string;
  blocked: boolean;
  evidence: string;
};

export type CspState = {
  policy: string;
  violations: CspViolation[];
  results: CspResult[];
};

export type NavAttempt = {
  label: string;
  url: string;
  allowed: boolean;
  reason: string;
  observed: string;
};

export type IntegrityFile = {
  fileName: string;
  expected: string;
  actual: string;
  ok: boolean;
};

export type IntegrityState = {
  manifestVersion: number;
  good: IntegrityFile;
  tampered: IntegrityFile | null;
};

export type PackagingState = {
  isPackaged: boolean;
  appPath: string;
  asar: boolean;
  fusesVerified: false;
  note: string;
};

export type Snapshot = {
  platform: string;
  electronVersion: string;
  nodeVersion: string;
  checks: Check[];
  score: number;
  csp: CspState;
  navigation: {
    policy: NavAttempt[];
    attempts: NavAttempt[];
  };
  integrity: IntegrityState;
  packaging: PackagingState;
  events: EventRow[];
};

const NAV_HOSTS = new Set(["docs.xwrt.dev", "www.electronjs.org"]);

export function isAllowedNavigation(
  raw: string,
  appUrl: string,
): { allowed: boolean; reason: string } {
  let url: URL;
  try {
    url = new URL(raw);
  } catch {
    return { allowed: false, reason: "BAD_URL" };
  }
  if (url.protocol === "file:") {
    if (appUrl.startsWith("file:")) {
      const dir = appUrl.slice(0, appUrl.lastIndexOf("/") + 1);
      if (url.href.startsWith(dir)) {
        return { allowed: true, reason: "APP_FILE" };
      }
    }
    return { allowed: false, reason: "BAD_SCHEME:file:" };
  }
  if (appUrl) {
    try {
      const app = new URL(appUrl);
      if (url.origin === app.origin && url.protocol === app.protocol) {
        return { allowed: true, reason: "APP_ORIGIN" };
      }
    } catch {
      // appUrl 在测试里可能是空串或非法值,按不可比较处理。
    }
  }
  if (url.protocol !== "https:") {
    return { allowed: false, reason: `BAD_SCHEME:${url.protocol}` };
  }
  if (!NAV_HOSTS.has(url.hostname)) {
    return { allowed: false, reason: `HOST_NOT_ALLOWED:${url.hostname}` };
  }
  return { allowed: true, reason: "ALLOWED_HOST" };
}

export function parseCsp(policy: string): Record<string, string[]> {
  const out: Record<string, string[]> = {};
  for (const part of policy.split(";")) {
    const tokens = part.trim().split(/\s+/).filter(Boolean);
    const name = tokens.shift();
    if (name) out[name] = tokens;
  }
  return out;
}

export function assertCspRequest(value: unknown): asserts value is {
  id: string;
} {
  if (typeof value !== "object" || value === null) {
    throw new Error("BAD_CSP_REQUEST");
  }
  if (typeof (value as { id?: unknown }).id !== "string") {
    throw new Error("BAD_CSP_REQUEST");
  }
}

export function assertNavigateRequest(value: unknown): asserts value is {
  url: string;
} {
  if (typeof value !== "object" || value === null) {
    throw new Error("BAD_NAV_REQUEST");
  }
  const url = (value as { url?: unknown }).url;
  if (typeof url !== "string" || url.length === 0 || url.length > 4096) {
    throw new Error("BAD_NAV_URL");
  }
}