forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuditLog.ts
More file actions
141 lines (129 loc) · 3.46 KB
/
Copy pathAuditLog.ts
File metadata and controls
141 lines (129 loc) · 3.46 KB
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
import mongoose from "mongoose";
import crypto from "crypto";
export type AuditAction =
| "challenge_issued"
| "challenge_rate_limited"
| "unlock_success"
| "unlock_invalid_signature"
| "unlock_expired_challenge"
| "unlock_no_access"
| "unlock_integrity_failure"
| "unlock_error"
| "unlock_rate_limited"
| "kms_key_rotated"
| "kms_break_glass_triggered"
| "kms_key_revoked"
| "kms_key_suspended";
export type AuditResult = "success" | "failure" | "blocked";
const auditLogSchema = new mongoose.Schema(
{
action: {
type: String,
required: true,
enum: [
"challenge_issued",
"challenge_rate_limited",
"unlock_success",
"unlock_invalid_signature",
"unlock_expired_challenge",
"unlock_no_access",
"unlock_integrity_failure",
"unlock_error",
"unlock_rate_limited",
"kms_key_rotated",
"kms_break_glass_triggered",
"kms_key_revoked",
"kms_key_suspended",
] as AuditAction[],
index: true,
},
result: {
type: String,
required: true,
enum: ["success", "failure", "blocked"] as AuditResult[],
index: true,
},
promptId: {
type: String,
default: null,
index: true,
},
walletAddress: {
type: String,
default: null,
lowercase: true,
index: true,
},
requestId: {
type: String,
default: null,
index: true,
},
clientIp: {
type: String,
default: null,
},
reason: {
type: String,
default: null,
},
hash: {
type: String,
default: "",
},
previousHash: {
type: String,
default: "",
},
// Sensitive fields are NEVER stored — only stable reason codes above.
// No plaintext, no keys, no raw signatures, no challenge secrets.
},
{
timestamps: true,
// Append-only: disable update operations at the schema level via middleware.
},
);
// Hash chaining middleware for tamper-evident audit logs
auditLogSchema.pre("save", async function (next) {
try {
const latestDoc = await (this.constructor as any)
.findOne()
.sort({ createdAt: -1 });
const prevHash = latestDoc ? latestDoc.hash || "" : "0".repeat(64);
this.set("previousHash", prevHash);
const fieldsToHash = [
this.get("action") || "",
this.get("result") || "",
this.get("promptId") || "",
this.get("walletAddress") || "",
this.get("requestId") || "",
this.get("clientIp") || "",
this.get("reason") || "",
prevHash,
];
const currentHash = crypto
.createHash("sha256")
.update(fieldsToHash.join("|"))
.digest("hex");
this.set("hash", currentHash);
next();
} catch (err: any) {
next(err);
}
});
// Compound indexes for common incident-review queries.
auditLogSchema.index({ walletAddress: 1, createdAt: -1 });
auditLogSchema.index({ promptId: 1, createdAt: -1 });
auditLogSchema.index({ action: 1, result: 1, createdAt: -1 });
// Prevent updates — audit records are immutable.
auditLogSchema.pre("findOneAndUpdate", function () {
throw new Error("AuditLog records are immutable.");
});
auditLogSchema.pre("updateOne", function () {
throw new Error("AuditLog records are immutable.");
});
auditLogSchema.pre("updateMany", function () {
throw new Error("AuditLog records are immutable.");
});
export const AuditLog =
mongoose.models.AuditLog || mongoose.model("AuditLog", auditLogSchema);