Closer/functions/dist/security/checkDeviceIntegrity.js

72 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkDeviceIntegrity = void 0;
refactor(functions): B3 migrate 12 callables to v2 + harden Migrate all callables off functions.https.onCall to firebase-functions/v2/https onCall: createInvite, acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou, checkDeviceIntegrity, syncEntitlement, assignDailyQuestionCallable, wrapReleaseKey. context.auth/app → request.auth/app, data arg → request.data. The 8 client-hardcoded callable names are preserved verbatim (verified via emulator discovery). The manual `if (!request.app)` App Check check is a 1:1 port (no enforceAppCheck switch). Hardening folded in: - acceptInviteCallable: await the previously fire-and-forget partner_joined push (gen 2 freezes the instance after the response) — still swallows push errors so a failed push never fails the accept. - checkDeviceIntegrity: 10s timeout on the Play Integrity client.request so a hung upstream can't pin the instance (fail-closed catch already handles the throw); memory 512MiB. - wrapReleaseKey: memory 512MiB (tink); HttpsError swapped to v2; lazy tink require + graceful failure preserved. - Error mapping with `if (e instanceof HttpsError) throw e` re-throw guard around the risky DB sections in acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou — raw errors map to a clean 'internal' without masking intentional codes (resource-exhausted rate limits, permission-denied, etc.). leaveCouple's best-effort recursiveDelete sweep now swallows errors (the transactional leave already succeeded). - Adopt shared sendPushToUser()/logger; remove copied token readers + plaintext token logging. Delete dead placeholder callables notifications/reminders.ts (sendDailyQuestionReminder, sendPartnerAnsweredNotification) — no client caller; wrote sent:false rows nothing consumed. Build clean; 70 tests green; discovery loads all callables as v2 in us-central1. dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:58:32 -05:00
const https_1 = require("firebase-functions/v2/https");
const google_auth_library_1 = require("google-auth-library");
refactor(functions): B3 migrate 12 callables to v2 + harden Migrate all callables off functions.https.onCall to firebase-functions/v2/https onCall: createInvite, acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou, checkDeviceIntegrity, syncEntitlement, assignDailyQuestionCallable, wrapReleaseKey. context.auth/app → request.auth/app, data arg → request.data. The 8 client-hardcoded callable names are preserved verbatim (verified via emulator discovery). The manual `if (!request.app)` App Check check is a 1:1 port (no enforceAppCheck switch). Hardening folded in: - acceptInviteCallable: await the previously fire-and-forget partner_joined push (gen 2 freezes the instance after the response) — still swallows push errors so a failed push never fails the accept. - checkDeviceIntegrity: 10s timeout on the Play Integrity client.request so a hung upstream can't pin the instance (fail-closed catch already handles the throw); memory 512MiB. - wrapReleaseKey: memory 512MiB (tink); HttpsError swapped to v2; lazy tink require + graceful failure preserved. - Error mapping with `if (e instanceof HttpsError) throw e` re-throw guard around the risky DB sections in acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou — raw errors map to a clean 'internal' without masking intentional codes (resource-exhausted rate limits, permission-denied, etc.). leaveCouple's best-effort recursiveDelete sweep now swallows errors (the transactional leave already succeeded). - Adopt shared sendPushToUser()/logger; remove copied token readers + plaintext token logging. Delete dead placeholder callables notifications/reminders.ts (sendDailyQuestionReminder, sendPartnerAnsweredNotification) — no client caller; wrote sent:false rows nothing consumed. Build clean; 70 tests green; discovery loads all callables as v2 in us-central1. dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:58:32 -05:00
const log_1 = require("../log");
const PACKAGE_NAME = 'app.closer';
const PLAY_INTEGRITY_URL = `https://playintegrity.googleapis.com/v1/${PACKAGE_NAME}:decodeIntegrityToken`;
refactor(functions): B3 migrate 12 callables to v2 + harden Migrate all callables off functions.https.onCall to firebase-functions/v2/https onCall: createInvite, acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou, checkDeviceIntegrity, syncEntitlement, assignDailyQuestionCallable, wrapReleaseKey. context.auth/app → request.auth/app, data arg → request.data. The 8 client-hardcoded callable names are preserved verbatim (verified via emulator discovery). The manual `if (!request.app)` App Check check is a 1:1 port (no enforceAppCheck switch). Hardening folded in: - acceptInviteCallable: await the previously fire-and-forget partner_joined push (gen 2 freezes the instance after the response) — still swallows push errors so a failed push never fails the accept. - checkDeviceIntegrity: 10s timeout on the Play Integrity client.request so a hung upstream can't pin the instance (fail-closed catch already handles the throw); memory 512MiB. - wrapReleaseKey: memory 512MiB (tink); HttpsError swapped to v2; lazy tink require + graceful failure preserved. - Error mapping with `if (e instanceof HttpsError) throw e` re-throw guard around the risky DB sections in acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou — raw errors map to a clean 'internal' without masking intentional codes (resource-exhausted rate limits, permission-denied, etc.). leaveCouple's best-effort recursiveDelete sweep now swallows errors (the transactional leave already succeeded). - Adopt shared sendPushToUser()/logger; remove copied token readers + plaintext token logging. Delete dead placeholder callables notifications/reminders.ts (sendDailyQuestionReminder, sendPartnerAnsweredNotification) — no client caller; wrote sent:false rows nothing consumed. Build clean; 70 tests green; discovery loads all callables as v2 in us-central1. dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:58:32 -05:00
// Cap the upstream call so a hung Play Integrity API can't pin the instance for the whole
// function timeout; the fail-closed catch below turns a timeout into passed:false.
const PLAY_INTEGRITY_TIMEOUT_MS = 10000;
/**
* Verifies a Play Integrity API token server-side and returns whether the
* device meets basic integrity requirements.
*
* Called by [PlayIntegrityChecker] on app startup. Requires the caller to be
* authenticated the Firebase Auth token is verified automatically by the
* Functions runtime.
*
* Setup required before this function works in production:
* 1. Enable the Play Integrity API in your Google Cloud project.
* 2. Link the Cloud project to your Play Console app (Play Console
* Setup API access Link to Cloud project).
* 3. Grant the Cloud Functions service account the "Play Integrity API User"
* role in IAM, or use a dedicated service account key via
* GOOGLE_APPLICATION_CREDENTIALS.
*
* If the Play Integrity API is not configured the function fails-closed
* (returns passed: false). Configure the API and grant the service account
* the "Play Integrity API User" IAM role before deploying to production.
*/
refactor(functions): B3 migrate 12 callables to v2 + harden Migrate all callables off functions.https.onCall to firebase-functions/v2/https onCall: createInvite, acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou, checkDeviceIntegrity, syncEntitlement, assignDailyQuestionCallable, wrapReleaseKey. context.auth/app → request.auth/app, data arg → request.data. The 8 client-hardcoded callable names are preserved verbatim (verified via emulator discovery). The manual `if (!request.app)` App Check check is a 1:1 port (no enforceAppCheck switch). Hardening folded in: - acceptInviteCallable: await the previously fire-and-forget partner_joined push (gen 2 freezes the instance after the response) — still swallows push errors so a failed push never fails the accept. - checkDeviceIntegrity: 10s timeout on the Play Integrity client.request so a hung upstream can't pin the instance (fail-closed catch already handles the throw); memory 512MiB. - wrapReleaseKey: memory 512MiB (tink); HttpsError swapped to v2; lazy tink require + graceful failure preserved. - Error mapping with `if (e instanceof HttpsError) throw e` re-throw guard around the risky DB sections in acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou — raw errors map to a clean 'internal' without masking intentional codes (resource-exhausted rate limits, permission-denied, etc.). leaveCouple's best-effort recursiveDelete sweep now swallows errors (the transactional leave already succeeded). - Adopt shared sendPushToUser()/logger; remove copied token readers + plaintext token logging. Delete dead placeholder callables notifications/reminders.ts (sendDailyQuestionReminder, sendPartnerAnsweredNotification) — no client caller; wrote sent:false rows nothing consumed. Build clean; 70 tests green; discovery loads all callables as v2 in us-central1. dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:58:32 -05:00
exports.checkDeviceIntegrity = (0, https_1.onCall)({ memory: '512MiB' }, async (request) => {
var _a;
if (!request.auth) {
throw new https_1.HttpsError('unauthenticated', 'Caller must be authenticated.');
}
refactor(functions): B3 migrate 12 callables to v2 + harden Migrate all callables off functions.https.onCall to firebase-functions/v2/https onCall: createInvite, acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou, checkDeviceIntegrity, syncEntitlement, assignDailyQuestionCallable, wrapReleaseKey. context.auth/app → request.auth/app, data arg → request.data. The 8 client-hardcoded callable names are preserved verbatim (verified via emulator discovery). The manual `if (!request.app)` App Check check is a 1:1 port (no enforceAppCheck switch). Hardening folded in: - acceptInviteCallable: await the previously fire-and-forget partner_joined push (gen 2 freezes the instance after the response) — still swallows push errors so a failed push never fails the accept. - checkDeviceIntegrity: 10s timeout on the Play Integrity client.request so a hung upstream can't pin the instance (fail-closed catch already handles the throw); memory 512MiB. - wrapReleaseKey: memory 512MiB (tink); HttpsError swapped to v2; lazy tink require + graceful failure preserved. - Error mapping with `if (e instanceof HttpsError) throw e` re-throw guard around the risky DB sections in acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou — raw errors map to a clean 'internal' without masking intentional codes (resource-exhausted rate limits, permission-denied, etc.). leaveCouple's best-effort recursiveDelete sweep now swallows errors (the transactional leave already succeeded). - Adopt shared sendPushToUser()/logger; remove copied token readers + plaintext token logging. Delete dead placeholder callables notifications/reminders.ts (sendDailyQuestionReminder, sendPartnerAnsweredNotification) — no client caller; wrote sent:false rows nothing consumed. Build clean; 70 tests green; discovery loads all callables as v2 in us-central1. dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:58:32 -05:00
if (!request.app) {
throw new https_1.HttpsError('failed-precondition', 'App Check verification required.');
}
refactor(functions): B3 migrate 12 callables to v2 + harden Migrate all callables off functions.https.onCall to firebase-functions/v2/https onCall: createInvite, acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou, checkDeviceIntegrity, syncEntitlement, assignDailyQuestionCallable, wrapReleaseKey. context.auth/app → request.auth/app, data arg → request.data. The 8 client-hardcoded callable names are preserved verbatim (verified via emulator discovery). The manual `if (!request.app)` App Check check is a 1:1 port (no enforceAppCheck switch). Hardening folded in: - acceptInviteCallable: await the previously fire-and-forget partner_joined push (gen 2 freezes the instance after the response) — still swallows push errors so a failed push never fails the accept. - checkDeviceIntegrity: 10s timeout on the Play Integrity client.request so a hung upstream can't pin the instance (fail-closed catch already handles the throw); memory 512MiB. - wrapReleaseKey: memory 512MiB (tink); HttpsError swapped to v2; lazy tink require + graceful failure preserved. - Error mapping with `if (e instanceof HttpsError) throw e` re-throw guard around the risky DB sections in acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou — raw errors map to a clean 'internal' without masking intentional codes (resource-exhausted rate limits, permission-denied, etc.). leaveCouple's best-effort recursiveDelete sweep now swallows errors (the transactional leave already succeeded). - Adopt shared sendPushToUser()/logger; remove copied token readers + plaintext token logging. Delete dead placeholder callables notifications/reminders.ts (sendDailyQuestionReminder, sendPartnerAnsweredNotification) — no client caller; wrote sent:false rows nothing consumed. Build clean; 70 tests green; discovery loads all callables as v2 in us-central1. dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:58:32 -05:00
const token = (_a = request.data) === null || _a === void 0 ? void 0 : _a.token;
if (!token || typeof token !== 'string') {
refactor(functions): B3 migrate 12 callables to v2 + harden Migrate all callables off functions.https.onCall to firebase-functions/v2/https onCall: createInvite, acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou, checkDeviceIntegrity, syncEntitlement, assignDailyQuestionCallable, wrapReleaseKey. context.auth/app → request.auth/app, data arg → request.data. The 8 client-hardcoded callable names are preserved verbatim (verified via emulator discovery). The manual `if (!request.app)` App Check check is a 1:1 port (no enforceAppCheck switch). Hardening folded in: - acceptInviteCallable: await the previously fire-and-forget partner_joined push (gen 2 freezes the instance after the response) — still swallows push errors so a failed push never fails the accept. - checkDeviceIntegrity: 10s timeout on the Play Integrity client.request so a hung upstream can't pin the instance (fail-closed catch already handles the throw); memory 512MiB. - wrapReleaseKey: memory 512MiB (tink); HttpsError swapped to v2; lazy tink require + graceful failure preserved. - Error mapping with `if (e instanceof HttpsError) throw e` re-throw guard around the risky DB sections in acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou — raw errors map to a clean 'internal' without masking intentional codes (resource-exhausted rate limits, permission-denied, etc.). leaveCouple's best-effort recursiveDelete sweep now swallows errors (the transactional leave already succeeded). - Adopt shared sendPushToUser()/logger; remove copied token readers + plaintext token logging. Delete dead placeholder callables notifications/reminders.ts (sendDailyQuestionReminder, sendPartnerAnsweredNotification) — no client caller; wrote sent:false rows nothing consumed. Build clean; 70 tests green; discovery loads all callables as v2 in us-central1. dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:58:32 -05:00
throw new https_1.HttpsError('invalid-argument', 'token is required.');
}
try {
const verdicts = await decodeIntegrityToken(token);
const passed = verdicts.includes('MEETS_DEVICE_INTEGRITY') ||
verdicts.includes('MEETS_STRONG_INTEGRITY');
return { passed, verdicts };
}
catch (err) {
refactor(functions): B3 migrate 12 callables to v2 + harden Migrate all callables off functions.https.onCall to firebase-functions/v2/https onCall: createInvite, acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou, checkDeviceIntegrity, syncEntitlement, assignDailyQuestionCallable, wrapReleaseKey. context.auth/app → request.auth/app, data arg → request.data. The 8 client-hardcoded callable names are preserved verbatim (verified via emulator discovery). The manual `if (!request.app)` App Check check is a 1:1 port (no enforceAppCheck switch). Hardening folded in: - acceptInviteCallable: await the previously fire-and-forget partner_joined push (gen 2 freezes the instance after the response) — still swallows push errors so a failed push never fails the accept. - checkDeviceIntegrity: 10s timeout on the Play Integrity client.request so a hung upstream can't pin the instance (fail-closed catch already handles the throw); memory 512MiB. - wrapReleaseKey: memory 512MiB (tink); HttpsError swapped to v2; lazy tink require + graceful failure preserved. - Error mapping with `if (e instanceof HttpsError) throw e` re-throw guard around the risky DB sections in acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou — raw errors map to a clean 'internal' without masking intentional codes (resource-exhausted rate limits, permission-denied, etc.). leaveCouple's best-effort recursiveDelete sweep now swallows errors (the transactional leave already succeeded). - Adopt shared sendPushToUser()/logger; remove copied token readers + plaintext token logging. Delete dead placeholder callables notifications/reminders.ts (sendDailyQuestionReminder, sendPartnerAnsweredNotification) — no client caller; wrote sent:false rows nothing consumed. Build clean; 70 tests green; discovery loads all callables as v2 in us-central1. dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:58:32 -05:00
log_1.logger.error('[checkDeviceIntegrity] verification failed', { error: String(err) });
// Fail-closed: an unverifiable request is treated as failed, not passed.
// Ensure the Play Integrity API is enabled and the service account has
// "Play Integrity API User" role before deploying to production.
return { passed: false, verdicts: [], error: 'verification_unavailable' };
}
});
async function decodeIntegrityToken(token) {
var _a, _b, _c;
const auth = new google_auth_library_1.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/playintegrity'],
});
const client = await auth.getClient();
const response = await client.request({
url: PLAY_INTEGRITY_URL,
method: 'POST',
data: { integrity_token: token },
refactor(functions): B3 migrate 12 callables to v2 + harden Migrate all callables off functions.https.onCall to firebase-functions/v2/https onCall: createInvite, acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou, checkDeviceIntegrity, syncEntitlement, assignDailyQuestionCallable, wrapReleaseKey. context.auth/app → request.auth/app, data arg → request.data. The 8 client-hardcoded callable names are preserved verbatim (verified via emulator discovery). The manual `if (!request.app)` App Check check is a 1:1 port (no enforceAppCheck switch). Hardening folded in: - acceptInviteCallable: await the previously fire-and-forget partner_joined push (gen 2 freezes the instance after the response) — still swallows push errors so a failed push never fails the accept. - checkDeviceIntegrity: 10s timeout on the Play Integrity client.request so a hung upstream can't pin the instance (fail-closed catch already handles the throw); memory 512MiB. - wrapReleaseKey: memory 512MiB (tink); HttpsError swapped to v2; lazy tink require + graceful failure preserved. - Error mapping with `if (e instanceof HttpsError) throw e` re-throw guard around the risky DB sections in acceptInvite, leaveCouple, submitOutcome, sendGentleReminder, sendThinkingOfYou — raw errors map to a clean 'internal' without masking intentional codes (resource-exhausted rate limits, permission-denied, etc.). leaveCouple's best-effort recursiveDelete sweep now swallows errors (the transactional leave already succeeded). - Adopt shared sendPushToUser()/logger; remove copied token readers + plaintext token logging. Delete dead placeholder callables notifications/reminders.ts (sendDailyQuestionReminder, sendPartnerAnsweredNotification) — no client caller; wrote sent:false rows nothing consumed. Build clean; 70 tests green; discovery loads all callables as v2 in us-central1. dist rebuilt. Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:58:32 -05:00
timeout: PLAY_INTEGRITY_TIMEOUT_MS,
});
return ((_c = (_b = (_a = response.data.tokenPayloadExternal) === null || _a === void 0 ? void 0 : _a.deviceIntegrity) === null || _b === void 0 ? void 0 : _b.deviceRecognitionVerdict) !== null && _c !== void 0 ? _c : []);
}
//# sourceMappingURL=checkDeviceIntegrity.js.map