Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | 157x 157x 157x 157x 157x 48x 48x 48x 48x 36x 36x 48x 48x 48x 48x 11x 11x 11x 10x 48x 9x 48x 48x 48x 48x 48x 48x 39x 39x 39x 39x 39x 39x 156x 18x 16x 16x 39x 16x 11x 11x 10x 10x 10x 157x | /**
* The device and network bans a suspension applies, and lifts (SHY-0605).
*
* Moved from `routes/admin-users.js` so every suspension ending can lift
* them, not only the admin unsuspend route. A home of their own rather than
* `bans.js`: they call `syncBannedClaim`, and `banned-claim.js` already
* requires `bans.js`, so living there would make the two require each other.
*/
const { db } = require('./firebase');
const { clearBanCache } = require('./bans');
const { syncBannedClaim } = require('./banned-claim');
const { generateId, now } = require('./helpers');
const log = require('./log');
// ══════════════════════════════════════════════════════════════
// HELPER: Auto-apply device + network bans on suspension
// ══════════════════════════════════════════════════════════════
/**
* When a user is suspended, automatically ban all their known devices
* and their last known IP address. Uses the same duration as the
* suspension if an endDate is provided, otherwise permanent.
*/
async function autoApplyBans(uniqueId, endDate) {
const timestamp = now();
// Calculate expiry — match suspension duration or permanent
let expiresAt = null;
let duration = 'permanent';
if (endDate) {
expiresAt = new Date(endDate).toISOString();
duration = 'suspension-match';
}
// Find all devices bound to this user
const bindingsSnap = await db
.collection('deviceBindings')
.where('uniqueId', '==', uniqueId)
.get();
let lastIp = null;
const batch = db.batch();
for (const doc of bindingsSnap.docs) {
const binding = doc.data();
// Create a device ban for each bound device
batch.set(db.doc(`deviceBans/${doc.id}`), {
deviceId: doc.id,
reason: 'Auto-applied: user suspended',
duration,
expiresAt,
linkedUniqueId: uniqueId,
autoApplied: true,
createdAt: timestamp,
createdBy: 'system',
});
// Track the last known IP from the most recent binding
if (binding.lastIp || binding.ip) {
lastIp = binding.lastIp || binding.ip;
}
}
// Create a network ban for the last known IP
if (lastIp) {
batch.set(db.doc(`networkBans/${generateId()}`), {
type: 'ip',
value: lastIp,
reason: 'Auto-applied: user suspended',
duration,
expiresAt,
linkedUniqueId: uniqueId,
autoApplied: true,
createdAt: timestamp,
createdBy: 'system',
});
}
await batch.commit();
// Auto-applied bans must bite on the target's next request, exactly
// like admin-issued ones (SHY-0149 mid-session AC).
clearBanCache();
// SHY-0150: the auto-bans change the target's claim standing — mint
// `banned: true` (+ revoke refresh tokens) through the same chokepoint
// the admin ban routes use. AFTER clearBanCache so the recompute reads
// the standing this batch just created. This helper runs fire-and-forget
// from the suspend route, whose .catch logs any failure here.
await syncBannedClaim(uniqueId);
const deviceCount = bindingsSnap.docs.length;
const networkCount = lastIp ? 1 : 0;
log.info('admin-users', 'Auto-bans applied', {
uniqueId,
deviceBans: deviceCount,
networkBans: networkCount,
});
}
// ══════════════════════════════════════════════════════════════
// HELPER: Lift auto-applied bans on unsuspension
// ══════════════════════════════════════════════════════════════
/**
* Remove device and network bans that were auto-applied during suspension.
* Only removes bans with autoApplied === true and linkedUserId === uid.
* Manually-applied bans are left untouched.
*
* @returns {Promise<number>} how many bans were lifted (0 when none)
*/
async function liftAutoAppliedBans(uniqueId, adminUid) {
const numericId = Number(uniqueId);
const stringId = String(uniqueId);
const [deviceSnapStr, deviceSnapNum, networkSnapStr, networkSnapNum] = await Promise.all([
db
.collection('deviceBans')
.where('linkedUniqueId', '==', stringId)
.where('autoApplied', '==', true)
.get(),
db
.collection('deviceBans')
.where('linkedUniqueId', '==', numericId)
.where('autoApplied', '==', true)
.get(),
db
.collection('networkBans')
.where('linkedUniqueId', '==', stringId)
.where('autoApplied', '==', true)
.get(),
db
.collection('networkBans')
.where('linkedUniqueId', '==', numericId)
.where('autoApplied', '==', true)
.get(),
]);
// Deduplicate by path in case both id forms match the same doc. Not by the
// bare id: a device ban and a network ban may share one.
const seen = new Set();
const allDocs = [];
for (const snap of [deviceSnapStr, deviceSnapNum, networkSnapStr, networkSnapNum]) {
for (const d of snap.docs) {
if (!seen.has(d.ref.path)) {
seen.add(d.ref.path);
allDocs.push(d);
}
}
}
if (allDocs.length === 0) return 0;
await Promise.all(allDocs.map((d) => d.ref.delete()));
// Lifting bans must unblock the gate as promptly as banning engages it.
clearBanCache();
// SHY-0150: RECOMPUTE the claim (never blind-clear) — a manually-issued
// ban that survives the auto-ban lift keeps the user's claim standing.
await syncBannedClaim(uniqueId);
// Audit log
await db.doc(`adminAuditLog/${generateId()}`).set({
adminId: adminUid,
action: 'LIFT_AUTO_BANS',
targetUserId: uniqueId,
details: `Removed ${allDocs.length} auto-applied ban(s) on unsuspension`,
createdAt: now(),
});
log.info('admin-users', 'Auto-applied bans lifted', { uniqueId, removed: allDocs.length });
return allDocs.length;
}
module.exports = { autoApplyBans, liftAutoAppliedBans };
|