audit.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Audit logging utilities for SaaS VOC Cloud Functions
  3. */
  4. /**
  5. * Safely serialize a value, removing sensitive fields
  6. */
  7. function safeValue(value, depth) {
  8. if (depth > 5 || value === null || value === undefined) return value;
  9. if (value instanceof Date) return value;
  10. if (Array.isArray(value)) return value.slice(0, 100).map((item) => safeValue(item, depth + 1));
  11. if (typeof value !== 'object') return value;
  12. const output = {};
  13. Object.keys(value).slice(0, 100).forEach((key) => {
  14. if (/token|secret|password|credential|authorization|master/i.test(key)) return;
  15. output[key] = safeValue(value[key], depth + 1);
  16. });
  17. return output;
  18. }
  19. /**
  20. * Generate a unique public ID
  21. */
  22. function publicId() {
  23. return 'cf-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 10);
  24. }
  25. /**
  26. * Get user ID from active request
  27. */
  28. function getUserId() {
  29. return Parse?.request?.user?.id || null;
  30. }
  31. /**
  32. * Append an audit log entry
  33. */
  34. async function appendAudit(workspaceId, action, entityType, entityId, metadata) {
  35. const audit = new Parse.Object('VocAuditLog');
  36. audit.set('publicId', publicId());
  37. audit.set('workspaceId', workspaceId);
  38. audit.set('actorUserId', getUserId());
  39. audit.set('action', action);
  40. audit.set('entityType', entityType);
  41. audit.set('entityId', entityId || null);
  42. audit.set('metadata', safeValue(metadata || {}, 0));
  43. await audit.save(null, { useMasterKey: true });
  44. }
  45. /*
  46. // module.exports = {
  47. // appendAudit,
  48. // };
  49. */