storebound/cli/lib/scan/declare.ts
CRT 00a38cd305 storebound scan: offline rejection-risk scan of the project itself
Detects the stack (native iOS / Android, React Native, Expo, Capacitor, Cordova,
Flutter, Unity, Godot) and the SDKs from a maintained table with versions,
file:line and each vendor's disclosure page, then reports App Store and Google
Play rejection risks with guideline, evidence, fix, severity and skill: 2.1(a)
demo account and placeholder text, 5.1.1(v) account deletion, 4.8 login
services, 3.1.1 restore and outside payments, 3.1.2 subscription info, 5.1.2(i)
ATT, purpose strings, privacy link, privacy manifest, 2.3.10, 2.3.7, 2.3.8, 4.2
web wrappers, 2.5.1, 2.5.5, ATS, export compliance, 1.3 kids ads, review
prompts, privacy labels vs SDKs; Play target SDK 36, delete-account URL, App
access, foreground services, exact alarms, QUERY_ALL_PACKAGES, SMS / call log,
com.example package name.

Proposes Data safety / App Privacy / ATT / privacy manifest answers (detected
file:line or ask owner) as a diff against the config; --write adds only the
detected ones. scan.ignore records owner-accepted findings. MCP tool scan
(read-only), validate includes open findings, desktop Checks tab shows them.
Fixtures for iOS, Android, Capacitor, Unity, Flutter, clean and teaser.
2026-10-11 15:20:55 +00:00

155 lines
12 KiB
TypeScript

// Declarations proposal for `storebound scan`: from the detected SDKs, permissions and code, the Play Data safety and
// App Store App Privacy answers, App Tracking Transparency and the privacy manifest entries the project needs, each
// marked "detected (file:line)" or "ask owner", and a diff against the config's dataSafety / appPrivacy blocks.
// Only detected items are ever written (`scan --write`); "ask owner" items stay questions (AGENTS.md rule 5). Pure.
import type { AppPrivacyConfig, DataSafety, DataUsage, PlayPurpose, ShipConfig } from '../config';
import { APPLE_PRIVACY, applePurpose } from '../declarations';
import { REQUIRED_REASON_APIS } from '../privacy-manifest';
import { IOS_PERMISSIONS } from './permissions';
import { grep, type Hit, lineOf, type ProjectFacts } from './project';
import { SIGNALS } from './rules';
export type DeclArea = 'dataSafety' | 'appPrivacy' | 'att' | 'privacyManifest';
export interface DeclItem { area: DeclArea; what: string; value: string; status: 'detected' | 'ask'; evidence: Hit[]; note?: string }
export interface DataDiff {
type: string;
/** App Privacy category / type it maps to. */
apple: string;
change: 'add' | 'merge' | 'same' | 'kept';
before: DataUsage | null;
after: DataUsage;
evidence: Hit[];
}
export interface Proposal {
items: DeclItem[];
diff: DataDiff[];
/** The dataSafety block --write would write (null when nothing was detected and the config has none). */
dataSafety: Partial<DataSafety> & { data: Record<string, DataUsage> } | null;
appPrivacy: AppPrivacyConfig | null;
appPrivacyChange: { from: boolean | null; to: boolean } | null;
/** Types an SDK collects that the config's dataSafety does not declare (the privacy-labels rule). */
labelGaps: { type: string; apple: string; evidence: Hit[] }[];
/** --write would change the config. */
changed: boolean;
}
const PERMISSION_DATA: Record<string, { type: string; note: string }> = {
NSCameraUsageDescription: { type: 'PSL_PHOTOS', note: 'only if photos / videos leave the device (uploaded, sent to an API)' },
NSMicrophoneUsageDescription: { type: 'PSL_AUDIO', note: 'only if recordings leave the device' },
NSPhotoLibraryUsageDescription: { type: 'PSL_PHOTOS', note: 'only if picked photos leave the device' },
NSLocationWhenInUseUsageDescription: { type: 'PSL_APPROX_LOCATION', note: 'approximate or precise (PSL_PRECISE_LOCATION), only if it leaves the device' },
NSContactsUsageDescription: { type: 'PSL_CONTACTS', note: 'only if contacts leave the device' },
NSHealthShareUsageDescription: { type: 'PSL_HEALTH', note: 'only if health data leaves the device' },
NSCalendarsFullAccessUsageDescription: { type: 'PSL_CALENDAR', note: 'only if events leave the device' },
};
const uniq = <T>(a: T[]): T[] => [...new Set(a)];
const sorted = (a: readonly string[] | undefined) => [...(a ?? [])].sort().join();
const usageEq = (a: DataUsage, b: DataUsage) => a.shared === b.shared && sorted(a.collect) === sorted(b.collect) && sorted(a.share) === sorted(b.share);
export function proposeDeclarations(p: ProjectFacts, c: ShipConfig | null, configFile?: { path: string; text: string } | null): Proposal {
const items: DeclItem[] = [];
const detected = new Map<string, { collect: Set<PlayPurpose>; share: Set<PlayPurpose>; evidence: Hit[]; from: string[] }>();
for (const s of p.sdks) {
const d0 = s.deps[0]!;
const ev: Hit = { file: d0.file, line: d0.line, text: `${s.sdk.name}: ${d0.name}${d0.version ? ` ${d0.version}` : ''}` };
for (const d of s.sdk.data) {
const e = detected.get(d.type) ?? { collect: new Set<PlayPurpose>(), share: new Set<PlayPurpose>(), evidence: [], from: [] };
for (const x of d.collect) e.collect.add(x);
for (const x of d.share ?? []) e.share.add(x);
if (!e.from.includes(s.sdk.name)) { e.from.push(s.sdk.name); e.evidence.push(ev); }
detected.set(d.type, e);
}
}
// accounts proven by the config or sign-up code
const signUp = grep(p.sources, SIGNALS.signUp, 1);
if (c?.accounts?.enabled || signUp.length) {
const ev: Hit[] = signUp.length ? signUp : [{ file: configFile?.path ?? 'storebound.config.ts', line: configFile ? lineOf(configFile.text, /accounts\s*:/) : 1, text: 'accounts.enabled' }];
for (const t of ['PSL_EMAIL', 'PSL_USER_ACCOUNT']) {
const e = detected.get(t) ?? { collect: new Set<PlayPurpose>(), share: new Set<PlayPurpose>(), evidence: [], from: [] };
e.collect.add('account'); e.collect.add('functionality'); e.evidence.push(...ev); e.from.push('accounts');
detected.set(t, e);
}
}
const have = c?.dataSafety?.data ?? {};
const diff: DataDiff[] = [];
const out: Record<string, DataUsage> = {};
for (const [type, e] of detected) {
const share = [...e.share].sort();
const det: DataUsage = { shared: share.length > 0, collect: [...e.collect].sort(), ...(share.length ? { share } : {}) };
const before = have[type] ?? null;
const after: DataUsage = before ? {
...before, shared: before.shared || det.shared,
collect: uniq([...before.collect, ...det.collect]).sort(),
...((before.share?.length || share.length) ? { share: uniq([...(before.share ?? []), ...share]).sort() } : {}),
} : det;
out[type] = after;
const [cat, name] = APPLE_PRIVACY[type] ?? ['Other Data', 'Other Data Types'];
diff.push({ type, apple: `${cat} / ${name}`, change: !before ? 'add' : usageEq(before, after) ? 'same' : 'merge', before, after, evidence: e.evidence.slice(0, 3) });
items.push({ area: 'dataSafety', what: type, value: `collect: ${after.collect.join(', ')}${after.shared ? `; shared: ${(after.share ?? []).join(', ')}` : ''}`, status: 'detected', evidence: e.evidence.slice(0, 2),
note: `from ${e.from.join(', ')}; ephemeral / optional: ask owner` });
}
for (const [type, u] of Object.entries(have)) if (!detected.has(type)) {
out[type] = u;
const [cat, name] = APPLE_PRIVACY[type] ?? ['Other Data', 'Other Data Types'];
diff.push({ type, apple: `${cat} / ${name}`, change: 'kept', before: u, after: u, evidence: [] });
}
// permissions that may mean collected data: questions, never written
for (const perm of IOS_PERMISSIONS) for (const k of perm.keys) {
const e = p.plist.get(k), pd = PERMISSION_DATA[k];
if (!e || !pd || out[pd.type]) continue;
items.push({ area: 'dataSafety', what: pd.type, value: `collected? ${pd.note}`, status: 'ask', evidence: [{ ...e.at, text: k }] });
}
const labelGaps = c?.dataSafety ? diff.filter(d => d.change === 'add').map(d => ({ type: d.type, apple: d.apple, evidence: d.evidence })) : [];
// top-level Data safety answers
const anyData = Object.keys(out).length > 0;
if (anyData) items.push({ area: 'dataSafety', what: 'collectsData', value: 'true', status: 'detected', evidence: diff.flatMap(d => d.evidence).slice(0, 1) });
for (const k of ['encryptedInTransit', 'deletionRequest'] as const)
items.push({ area: 'dataSafety', what: k, value: c?.dataSafety ? String(c.dataSafety[k]) : '?', status: 'ask', evidence: [], note: k === 'encryptedInTransit' ? 'all traffic over HTTPS / TLS? (ATS and the SDKs\' own endpoints)' : 'can users ask for their data to be deleted?' });
if ((c?.accounts?.enabled || signUp.length) && (c?.dataSafety?.accountCreation ?? ['PSL_ACM_NONE']).includes('PSL_ACM_NONE'))
items.push({ area: 'dataSafety', what: 'accountCreation', value: 'which sign-up methods (PSL_ACM_USER_ID_PASSWORD, PSL_ACM_OAUTH, ...)?', status: 'ask', evidence: signUp.slice(0, 1) });
// App Privacy per type (linked / tracking are owner answers)
for (const d of diff) {
const shared = d.after.shared ? d.after.share ?? [] : [];
// a purpose the data is shared for counts once, as the third party's (AdMob: Third-Party Advertising, not the developer's)
const purposes = uniq([...d.after.collect.filter(x => !shared.includes(x)).map(x => applePurpose(x, false)), ...shared.map(x => applePurpose(x, true))]).sort();
items.push({ area: 'appPrivacy', what: d.apple, value: purposes.join(', '), status: d.change === 'kept' ? 'ask' : 'detected', evidence: d.evidence.slice(0, 1), note: 'linked to identity? used for tracking? ask owner' });
}
// tracking + ATT
const attReq = grep(p.sources, SIGNALS.attRequest, 1);
const attStr = p.plist.get('NSUserTrackingUsageDescription');
const trackers = p.sdks.filter(s => s.sdk.tracking);
let tracking: boolean | null = null;
if (attReq.length) { tracking = true; items.push({ area: 'appPrivacy', what: 'tracking', value: 'true (the app asks for App Tracking Transparency)', status: 'detected', evidence: attReq }); }
else if (trackers.length) items.push({ area: 'appPrivacy', what: 'tracking', value: `${trackers.map(s => s.sdk.name).join(', ')} can track: personalized ads / attribution on?`, status: 'ask', evidence: trackers.map(s => ({ file: s.deps[0]!.file, line: s.deps[0]!.line, text: s.sdk.name })).slice(0, 2) });
if (attStr) items.push({ area: 'att', what: 'NSUserTrackingUsageDescription', value: String(attStr.value).slice(0, 80), status: 'detected', evidence: [{ ...attStr.at, text: 'NSUserTrackingUsageDescription' }] });
else if (trackers.length || attReq.length) items.push({ area: 'att', what: 'NSUserTrackingUsageDescription', value: 'needed if anything tracks, e.g. "Your data is used to show ads that fit your interests."', status: 'ask', evidence: [] });
if (attReq.length) items.push({ area: 'att', what: 'ATT request', value: 'found', status: 'detected', evidence: attReq });
if (trackers.length || attReq.length) items.push({ area: 'appPrivacy', what: 'trackingDomains', value: 'the SDKs\' tracking domains (from each SDK\'s privacy manifest)', status: 'ask', evidence: [] });
// privacy manifest
if (p.ios) {
const declared = new Map<string, string[]>();
for (const m of p.privacyManifests) for (const [k, v] of Object.entries(m.apis)) declared.set(k, v);
const nativeSrc = p.sources.filter(s => /\.(swift|m|mm|h)$/.test(s.path));
for (const [cat, def] of Object.entries(REQUIRED_REASON_APIS)) {
const h = grep(nativeSrc, def.symbols, 1);
if (!h.length) continue;
const have = declared.get(cat);
items.push({ area: 'privacyManifest', what: cat, value: have?.length ? `declared: ${have.join(', ')}` : `add with a reason: ${def.note}`, status: have?.length ? 'detected' : 'ask', evidence: h });
}
const m = p.privacyManifests[0];
if (tracking !== null || m) items.push({ area: 'privacyManifest', what: 'NSPrivacyTracking', value: tracking ? 'true' : m ? String(m.tracking) : '?', status: tracking ? 'detected' : 'ask', evidence: m ? [{ file: m.path, line: 1, text: 'PrivacyInfo.xcprivacy' }] : [] });
if (diff.length) items.push({ area: 'privacyManifest', what: 'NSPrivacyCollectedDataTypes', value: uniq(diff.map(d => d.apple.split(' / ')[1]!)).join(', '), status: 'detected', evidence: diff.flatMap(d => d.evidence).slice(0, 1), note: 'the app\'s own collection; SDKs declare theirs in their manifests' });
}
const dsChanged = diff.some(d => d.change === 'add' || d.change === 'merge') || (anyData && c?.dataSafety?.collectsData === false);
const dataSafety = anyData || c?.dataSafety ? { ...(c?.dataSafety ?? {}), collectsData: anyData || !!c?.dataSafety?.collectsData, data: out } : null;
const from = c?.appPrivacy?.tracking ?? null;
const appPrivacyChange = tracking !== null && from !== tracking ? { from, to: tracking } : null;
const appPrivacy = appPrivacyChange ? { ...(c?.appPrivacy ?? {}), tracking: appPrivacyChange.to } : c?.appPrivacy ?? null;
return { items, diff, dataSafety, appPrivacy, appPrivacyChange, labelGaps, changed: dsChanged || !!appPrivacyChange };
}