gpt-engineer-app[bot] commited on
Commit ·
2aaa293
1
Parent(s): 4e8f3b8
Changes
Browse files
src/server/branch-debug.functions.ts
CHANGED
|
@@ -53,14 +53,26 @@ const RESERVED = new Set([
|
|
| 53 |
function sanitize(diff: string) {
|
| 54 |
const tokenMap = new Map<string, string>(); // real -> token
|
| 55 |
const reverseMap = new Map<string, string>(); // token -> real
|
|
|
|
|
|
|
|
|
|
| 56 |
let counter = 1;
|
| 57 |
let commentsStripped = 0;
|
| 58 |
let secretsBlocked = 0;
|
| 59 |
|
| 60 |
// Strip secrets first
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
// Strip comments line-wise (#, //, /* */)
|
| 66 |
const lines = diff.split("\n").map((line) => {
|
|
@@ -69,12 +81,17 @@ function sanitize(diff: string) {
|
|
| 69 |
.replace(/(^|\s)#.*$/g, "$1")
|
| 70 |
.replace(/\/\/.*$/g, "")
|
| 71 |
.replace(/\/\*[\s\S]*?\*\//g, "");
|
| 72 |
-
if (stripped !== original)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return stripped;
|
| 74 |
});
|
| 75 |
|
| 76 |
const tokenize = (name: string): string => {
|
| 77 |
if (RESERVED.has(name) || /^\d+$/.test(name) || name.length < 3) return name;
|
|
|
|
| 78 |
let tok = tokenMap.get(name);
|
| 79 |
if (!tok) {
|
| 80 |
tok = `fn_${String(counter++).padStart(4, "0")}`;
|
|
@@ -87,16 +104,30 @@ function sanitize(diff: string) {
|
|
| 87 |
// Tokenize identifiers (simple heuristic: snake_case / camelCase words)
|
| 88 |
const sanitizedLines = lines.map((line) => {
|
| 89 |
if (line.startsWith("diff --git") || line.startsWith("index ") || line.startsWith("@@")) {
|
| 90 |
-
// preserve hunk headers but tokenize file paths
|
| 91 |
return line.replace(/[A-Za-z_][A-Za-z0-9_]{2,}/g, (m) => tokenize(m));
|
| 92 |
}
|
| 93 |
return line.replace(/[A-Za-z_][A-Za-z0-9_]{2,}/g, (m) => tokenize(m));
|
| 94 |
});
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
return {
|
| 97 |
-
sanitized
|
| 98 |
reverseMap,
|
| 99 |
stats: { identifiersTokenized: tokenMap.size, commentsStripped, secretsBlocked },
|
|
|
|
| 100 |
};
|
| 101 |
}
|
| 102 |
|
|
|
|
| 53 |
function sanitize(diff: string) {
|
| 54 |
const tokenMap = new Map<string, string>(); // real -> token
|
| 55 |
const reverseMap = new Map<string, string>(); // token -> real
|
| 56 |
+
const occurrences = new Map<string, number>(); // real -> count
|
| 57 |
+
const redactedComments: string[] = [];
|
| 58 |
+
const secretMatches: { pattern: string; replaced: string }[] = [];
|
| 59 |
let counter = 1;
|
| 60 |
let commentsStripped = 0;
|
| 61 |
let secretsBlocked = 0;
|
| 62 |
|
| 63 |
// Strip secrets first
|
| 64 |
+
const PATTERN_LABELS = ["api-key/secret/token assignment", "OpenAI key (sk-…)", "JWT bearer"];
|
| 65 |
+
SECRET_PATTERNS.forEach((re, idx) => {
|
| 66 |
+
diff = diff.replace(re, (match) => {
|
| 67 |
+
secretsBlocked++;
|
| 68 |
+
// Only record a safe length-summary, NEVER the secret itself
|
| 69 |
+
secretMatches.push({
|
| 70 |
+
pattern: PATTERN_LABELS[idx] ?? "secret",
|
| 71 |
+
replaced: `[REDACTED ${match.length} chars]`,
|
| 72 |
+
});
|
| 73 |
+
return "[SECRET_REDACTED]";
|
| 74 |
+
});
|
| 75 |
+
});
|
| 76 |
|
| 77 |
// Strip comments line-wise (#, //, /* */)
|
| 78 |
const lines = diff.split("\n").map((line) => {
|
|
|
|
| 81 |
.replace(/(^|\s)#.*$/g, "$1")
|
| 82 |
.replace(/\/\/.*$/g, "")
|
| 83 |
.replace(/\/\*[\s\S]*?\*\//g, "");
|
| 84 |
+
if (stripped !== original) {
|
| 85 |
+
commentsStripped++;
|
| 86 |
+
const removed = original.slice(stripped.length).trim();
|
| 87 |
+
if (removed && redactedComments.length < 20) redactedComments.push(removed);
|
| 88 |
+
}
|
| 89 |
return stripped;
|
| 90 |
});
|
| 91 |
|
| 92 |
const tokenize = (name: string): string => {
|
| 93 |
if (RESERVED.has(name) || /^\d+$/.test(name) || name.length < 3) return name;
|
| 94 |
+
occurrences.set(name, (occurrences.get(name) ?? 0) + 1);
|
| 95 |
let tok = tokenMap.get(name);
|
| 96 |
if (!tok) {
|
| 97 |
tok = `fn_${String(counter++).padStart(4, "0")}`;
|
|
|
|
| 104 |
// Tokenize identifiers (simple heuristic: snake_case / camelCase words)
|
| 105 |
const sanitizedLines = lines.map((line) => {
|
| 106 |
if (line.startsWith("diff --git") || line.startsWith("index ") || line.startsWith("@@")) {
|
|
|
|
| 107 |
return line.replace(/[A-Za-z_][A-Za-z0-9_]{2,}/g, (m) => tokenize(m));
|
| 108 |
}
|
| 109 |
return line.replace(/[A-Za-z_][A-Za-z0-9_]{2,}/g, (m) => tokenize(m));
|
| 110 |
});
|
| 111 |
|
| 112 |
+
const sanitized = sanitizedLines.join("\n");
|
| 113 |
+
|
| 114 |
+
// Build audit token map sorted by occurrence (most-used first)
|
| 115 |
+
const auditMap: AuditEntry[] = Array.from(tokenMap.entries())
|
| 116 |
+
.map(([real, token]) => ({ token, real, occurrences: occurrences.get(real) ?? 0 }))
|
| 117 |
+
.sort((a, b) => b.occurrences - a.occurrences);
|
| 118 |
+
|
| 119 |
+
// Sample: first 30 non-empty lines, original (post-secret-redaction) vs sanitized
|
| 120 |
+
const SAMPLE_LINES = 30;
|
| 121 |
+
const sample: AuditSample = {
|
| 122 |
+
original: diff.split("\n").slice(0, SAMPLE_LINES).join("\n"),
|
| 123 |
+
sanitized: sanitized.split("\n").slice(0, SAMPLE_LINES).join("\n"),
|
| 124 |
+
};
|
| 125 |
+
|
| 126 |
return {
|
| 127 |
+
sanitized,
|
| 128 |
reverseMap,
|
| 129 |
stats: { identifiersTokenized: tokenMap.size, commentsStripped, secretsBlocked },
|
| 130 |
+
audit: { tokenMap: auditMap, redactedComments, secretMatches, sample },
|
| 131 |
};
|
| 132 |
}
|
| 133 |
|