Arx / mobile /mobile-core.js
umer-wasim's picture
v0.3: phone check (iPhone, Android) as the Space front page, catalog 0.3 (74 checks), shared vulnerability rules, real-CVE training data
6ccf48e verified
Raw History Blame Contribute Delete
14.4 kB
// Phone self-check: turns what the browser detected and what the user answered into evidence in the
// same format as the desktop collectors (schema/evidence.schema.json, source.kind = "web").
// Used by the mobile web page and, through mobile/build-evidence.mjs, by the synthetic generator, so the
// training data uses exactly the wording the page produces. Pure functions only (no DOM, no network).
import { matchApps, knownVulnResult } from '../app/renderer/vuln-core.js';
export const MOBILE_VERSION = '0.3.0';
const MOBILE = new Set(['ios', 'android']);
const ORG_PENDING = 'Answered once by the organisation (organisational.conf); not part of the phone self-check.';
const major = version => parseInt(String(version).split('.')[0], 10) || 0;
const plural = (n, word) => `${n} ${word}${n === 1 ? '' : 's'}`;
// Text and status for an answer whose result depends on the baseline.
function computed(spec, answer, B, os) {
if (spec.expected_from === 'seconds') {
const limit = parseInt(B.SCREEN_LOCK_MAX_SECONDS, 10);
const label = os === 'ios' ? 'Auto-Lock' : 'Screen timeout';
const ok = answer.seconds != null && answer.seconds > 0 && answer.seconds <= limit;
return { status: ok ? 'Compliant' : 'Non-Compliant', actual: `${label} ${answer.label}`, expected: `<= ${limit} seconds`,
detail: ok ? '' : answer.seconds == null ? 'The phone never locks by itself.' : `The phone stays unlocked for ${answer.label} when not in use.` };
}
if (spec.expected_from === 'passcode') {
const min = parseInt(B.MOBILE_PASSCODE_MIN_DIGITS, 10);
const expected = `>= ${min} digits or alphanumeric`;
if (answer.alphanumeric) return { status: 'Compliant', actual: 'alphanumeric', expected, detail: '' };
if (answer.pattern) return { status: 'Non-Compliant', actual: 'pattern', expected, detail: 'Pattern locks are easy to guess from screen smudges or by watching.' };
if (answer.none) return { status: 'Non-Compliant', actual: 'swipe or none', expected, detail: 'There is no secret needed to unlock the phone.' };
const ok = answer.digits >= min;
const shown = answer.digits >= 7 ? '7+ digit numeric code' : `${answer.digits}-digit ${os === 'android' ? 'PIN' : 'code'}`;
return { status: ok ? 'Compliant' : 'Non-Compliant', actual: shown, expected, detail: ok ? '' : `A ${answer.digits}-digit code has only ${10 ** answer.digits} combinations.` };
}
throw new Error(`unknown computed answer type ${spec.expected_from}`);
}
// Android security patch level ("YYYY-MM") against MOBILE_PATCH_MAX_AGE_DAYS.
function patchResult(value, B, now) {
const maxDays = parseInt(B.MOBILE_PATCH_MAX_AGE_DAYS, 10);
const expected = `<= ${maxDays} days old`;
const m = /^(\d{4})-(\d{2})/.exec(value || '');
if (!m) return null;
const days = Math.floor((now - Date.UTC(+m[1], +m[2] - 1, 1)) / 86400000);
const months = Math.max(0, Math.round(days / 30.4));
const ok = days <= maxDays;
return { status: ok ? 'Compliant' : 'Non-Compliant', actual: `security patch ${m[1]}-${m[2]} (${plural(months, 'month')} old)`, expected,
detail: ok ? '' : `Security fixes published since ${m[1]}-${m[2]} are missing; Google publishes Android fixes every month.` };
}
// Newest fixed iOS version for the same major line, from the NVD ranges in the bundle.
function latestIosFix(bundle, product, version) {
const app = bundle.apps.find(a => a.id === product);
if (!app) return null;
let best = null;
for (const row of app.cves) {
const fixed = row[5];
if (fixed && major(fixed) === major(version) && (!best || cmp(fixed, best) > 0)) best = fixed;
}
return best;
}
// Apple fixes some vulnerabilities only in the newest major version.
function newerMajor(bundle, product, version) {
const app = bundle.apps.find(a => a.id === product);
let top = null;
for (const row of app ? app.cves : []) if (row[5] && major(row[5]) > major(version) && (!top || cmp(row[5], top) > 0)) top = row[5];
return top ? ` ${major(top)}.x (newest: ${top}) fixes vulnerabilities that are not fixed in ${major(version)}.x.` : '';
}
function cmp(a, b) {
const x = String(a).split('.').map(Number), y = String(b).split('.').map(Number);
for (let i = 0; i < Math.max(x.length, y.length); i++) { const d = (x[i] || 0) - (y[i] || 0); if (d) return d; }
return 0;
}
function osSupported(input, B) {
const ios = input.os_family === 'ios';
const min = parseInt(ios ? B.IOS_MIN_MAJOR : B.ANDROID_MIN_MAJOR, 10);
const ok = major(input.os_version) >= min;
return { status: ok ? 'Compliant' : 'Non-Compliant', actual: `${input.os_name} ${input.os_version}`, expected: `>= ${ios ? 'iOS' : 'Android'} ${min}`,
detail: (input.version_source === 'user' ? `Version entered by the user from ${ios ? 'Settings › General › About' : 'Settings › About phone'}.` : `Version reported by the browser (${input.browser}).`)
+ (ok ? '' : ' This version no longer receives all security updates.') };
}
function aiResults(id, apps, B) {
const approved = (B.APPROVED_AI_TOOLS || '').split(',').map(a => a.trim().toLowerCase()).filter(Boolean);
if (id === 'aiact.inventory') return { status: 'Compliant', actual: apps.join(', ') || 'no AI tools found', expected: 'inventory taken', detail: 'AI apps listed by the user.' };
if (id === 'aiact.unapproved') {
const bad = apps.filter(a => !approved.includes(a.toLowerCase()));
return { status: bad.length ? 'Non-Compliant' : 'Compliant', actual: bad.join(', ') || 'none', expected: `only: ${B.APPROVED_AI_TOOLS || 'none'}`, detail: '' };
}
if (id === 'aiact.risk_classification') {
return apps.length ? { status: 'Pending', actual: apps.join(', '), expected: 'every AI tool classified', detail: 'Classified by the auditor model (Caveat classifier) in a later step.' }
: { status: 'NotApplicable', actual: 'no AI tools found', expected: 'every AI tool classified', detail: '' };
}
return apps.length ? { status: 'Pending', actual: 'depends on classification', expected: '>= 6 months for high-risk AI', detail: 'Required only if a tool is classified high-risk.' }
: { status: 'NotApplicable', actual: 'no AI tools found', expected: '>= 6 months for high-risk AI', detail: '' };
}
function orgResult(check, answer) {
if (answer === 'yes') return { status: 'Compliant', actual: 'yes', expected: 'yes', detail: '' };
if (answer === 'no' || answer === 'partial') return { status: 'Non-Compliant', actual: answer, expected: 'yes', detail: '' };
if (answer === 'n/a') return { status: 'NotApplicable', actual: 'n/a', expected: 'yes', detail: '' };
return { status: 'Pending', actual: 'not answered', expected: 'yes', detail: answer === undefined ? ORG_PENDING : `Answer ${check.answer_key} in organisational.conf.` };
}
// The questions the page shows for this OS, in catalog order (detected checks are skipped).
export function questionsFor(osFamily, catalog, questions, detected = {}) {
const list = [];
for (const check of catalog.checks) {
if (!check.os.includes(osFamily)) continue;
const spec = (questions.checks[check.id] || {})[osFamily];
if (!spec) continue;
if (check.id === 'screen_lock.enabled' && detected.platform_authenticator === true) continue;
list.push({ id: check.id, name: (check.names && check.names[osFamily]) || check.name, ...spec });
}
return list;
}
/**
* input: { os_family, os_name, os_version, version_source: 'browser'|'user', browser, device_model, hostname, started_at,
* platform_authenticator: true|false|null, answers: { checkId: answerIndex | 'not_sure' | value }, ai_apps: [...], org: {key: answer}|null }
* ctx: { catalog, questions, baseline, vuln: {bundle, manifest}|null|{error}, now }
*/
export function buildMobileEvidence(input, ctx) {
const { catalog, questions, baseline: B } = ctx;
const now = ctx.now ?? Date.now();
const os = input.os_family;
if (!MOBILE.has(os)) throw new Error(`not a phone OS: ${os}`);
const answers = input.answers || {};
const apps = input.ai_apps || [];
const results = [];
const lockEvidence = input.platform_authenticator === true ? 'detected' : 'self-reported';
let lockSet = null;
for (const check of catalog.checks) {
if (!check.os.includes(os)) continue;
const name = (check.names && check.names[os]) || check.name;
const spec = (questions.checks[check.id] || {})[os];
let r, evidence = 'self-reported';
if (check.section === 'org') {
r = orgResult(check, input.org ? input.org[check.answer_key] : undefined);
evidence = null;
} else if (check.id === 'screen_lock.enabled' && input.platform_authenticator === true) {
r = { status: 'Compliant', actual: os === 'ios' ? 'passcode set' : 'screen lock set', expected: spec.expected,
detail: 'The browser reports that a device unlock method (passcode, PIN, Face ID or fingerprint) is set up.' };
evidence = 'detected';
} else if (check.id === 'gdpr.os_supported') {
r = osSupported(input, B);
evidence = input.version_source === 'user' ? 'self-reported' : 'detected';
} else if (check.id === 'encryption.enabled') {
if (os === 'android') {
const ok = major(input.os_version) >= 10;
r = { status: ok ? 'Compliant' : 'Non-Compliant', actual: ok ? `on (Android ${major(input.os_version)} encrypts by default)` : 'not guaranteed (Android older than 10)',
expected: 'on', detail: ok ? '' : 'Android encrypts by default only from version 10.' };
evidence = input.version_source === 'user' ? 'self-reported' : 'detected';
} else {
r = lockSet === null ? { status: 'Pending', actual: 'unknown', expected: 'on', detail: 'Depends on whether a passcode is set.' }
: { status: lockSet ? 'Compliant' : 'Non-Compliant', actual: lockSet ? 'on (passcode set)' : 'off (no passcode)', expected: 'on',
detail: lockSet ? 'iOS encrypts the data on the phone with hardware keys once a passcode is set.' : 'Without a passcode, iOS data protection is not active.' };
evidence = lockEvidence;
}
} else if (os === 'ios' && (check.id === 'vuln.pending_updates' || check.id === 'vuln.known_vulnerabilities')) {
evidence = input.version_source === 'user' ? 'self-reported' : 'detected';
const product = input.os_name === 'iPadOS' ? 'ipados' : 'ios';
if (!ctx.vuln || ctx.vuln.error) {
r = { status: 'Pending', actual: 'not checked', expected: check.id === 'vuln.pending_updates' ? 'latest iOS security release' : `<= ${B.MAX_CRITICAL_HIGH_VULNERABILITIES} critical/high`,
detail: `No vulnerability data available${ctx.vuln && ctx.vuln.error ? `: ${ctx.vuln.error}` : ''}.` };
} else if (check.id === 'vuln.pending_updates') {
const latest = latestIosFix(ctx.vuln.bundle, product, input.os_version);
const dataDate = ctx.vuln.manifest.built_at.slice(0, 10);
r = !latest ? { status: 'Pending', actual: `${input.os_name} ${input.os_version}`, expected: 'latest iOS security release', detail: `No security releases for ${input.os_name} ${major(input.os_version)} in the vulnerability data from ${dataDate}.` }
: cmp(input.os_version, latest) >= 0 ? { status: 'Compliant', actual: `${input.os_name} ${input.os_version} (up to date)`, expected: 'latest iOS security release', detail: `Newest fixed ${input.os_name} ${major(input.os_version)} release in the vulnerability data from ${dataDate}: ${latest}.${newerMajor(ctx.vuln.bundle, product, input.os_version)}` }
: { status: 'Non-Compliant', actual: `${input.os_name} ${input.os_version} (update to ${latest} available)`, expected: 'latest iOS security release', detail: `Install ${input.os_name} ${latest} or later in Settings › General › Software Update.` };
} else {
const inventory = { apps: [{ name: input.os_name, version: input.os_version }] };
const matched = matchApps(inventory, ctx.vuln.bundle, 'ios');
r = { expected: `<= ${B.MAX_CRITICAL_HIGH_VULNERABILITIES} critical/high`, ...knownVulnResult(matched, inventory, ctx.vuln, 'ios', parseInt(B.MAX_CRITICAL_HIGH_VULNERABILITIES, 10) || 0, now) };
if (input.version_source !== 'user') r.detail += ' Patch-level versions (x.y.z) are not always visible to the browser; confirm the version in Settings › General › About.';
}
} else if (check.section === 'aiact' && check.id !== 'aiact.os_ai_features') {
r = aiResults(check.id, apps, B);
} else if (spec) {
const a = answers[check.id];
if (a === undefined || a === 'not_sure') {
r = { ...questions.not_sure, expected: spec.expected || (spec.expected_from === 'seconds' ? `<= ${B.SCREEN_LOCK_MAX_SECONDS} seconds` : spec.expected_from === 'passcode' ? `>= ${B.MOBILE_PASSCODE_MIN_DIGITS} digits or alphanumeric` : `<= ${B.MOBILE_PATCH_MAX_AGE_DAYS} days old`) };
} else if (spec.input === 'month') {
r = patchResult(a, B, now) || { ...questions.not_sure, expected: `<= ${B.MOBILE_PATCH_MAX_AGE_DAYS} days old` };
} else {
const answer = spec.answers[a];
r = spec.expected_from ? computed(spec, answer, B, os)
: { status: answer.status, actual: answer.actual, expected: spec.expected, detail: answer.detail || '' };
}
} else {
throw new Error(`no rule for ${check.id} on ${os}`);
}
if (check.id === 'screen_lock.enabled') lockSet = r.status === 'Compliant' ? true : r.status === 'Non-Compliant' ? false : null;
const item = { id: check.id, section: check.section, name, status: r.status, actual: String(r.actual), expected: r.expected };
if (r.detail) item.detail = r.detail;
if (evidence) item.evidence = evidence;
results.push(item);
}
const summary = Object.fromEntries(catalog.statuses.map(s => [s, results.filter(r => r.status === s).length]));
const host = { hostname: input.hostname, os_family: os, os_name: input.os_name, os_version: String(input.os_version) };
if (input.device_model) host.device_model = input.device_model;
if (input.browser) host.browser = input.browser;
return {
schema_version: '1.0',
catalog_version: catalog.catalog_version,
source: { kind: 'web', collector: os, collector_version: MOBILE_VERSION },
audit: { mode: 'READ-ONLY', started_at: input.started_at, execution_user: 'phone self-check (browser)', is_admin: false },
host,
baseline: { ...B },
results,
summary,
};
}