-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonlSecretFirewall.cpp
More file actions
462 lines (415 loc) · 14.8 KB
/
JsonlSecretFirewall.cpp
File metadata and controls
462 lines (415 loc) · 14.8 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include <algorithm>
#include <array>
#include <charconv>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
namespace {
struct Config {
std::size_t max_line_bytes = 512 * 1024;
std::size_t max_value_chars = 2048;
bool hash_redacted = true;
bool pretty_stats = false;
};
struct Stats {
std::uint64_t lines_seen = 0;
std::uint64_t lines_emitted = 0;
std::uint64_t line_too_large = 0;
std::uint64_t json_keys_redacted = 0;
std::uint64_t inline_tokens_redacted = 0;
std::uint64_t high_entropy_redacted = 0;
std::uint64_t values_truncated = 0;
};
constexpr std::array<std::string_view, 20> kSensitiveKeyFragments = {
"api_key", "apikey", "authorization", "auth", "bearer", "token", "secret",
"client_secret", "password", "passwd", "session", "cookie", "set-cookie",
"private_key", "access_key", "refresh_token", "id_token", "webhook_secret",
"connection_string", "dsn"
};
constexpr std::array<std::string_view, 8> kSuspiciousPrefixes = {
"sk-", "ghp_", "github_pat_", "xoxb-", "xoxp-", "AIza", "AKIA", "eyJ"
};
std::string ToLower(std::string_view input) {
std::string out;
out.reserve(input.size());
for (unsigned char ch : input) {
out.push_back(static_cast<char>(std::tolower(ch)));
}
return out;
}
bool ContainsSensitiveKeyFragment(std::string_view key) {
std::string lowered = ToLower(key);
for (const auto fragment : kSensitiveKeyFragments) {
if (lowered.find(fragment) != std::string::npos) {
return true;
}
}
return false;
}
bool IsLikelyBase64ish(char ch) {
return std::isalnum(static_cast<unsigned char>(ch)) || ch == '+' || ch == '/' || ch == '_' || ch == '-' || ch == '=';
}
std::uint64_t Fnv1a64(std::string_view value) {
std::uint64_t hash = 1469598103934665603ull;
for (unsigned char ch : value) {
hash ^= static_cast<std::uint64_t>(ch);
hash *= 1099511628211ull;
}
return hash;
}
std::string Hex64(std::uint64_t value) {
std::ostringstream out;
out << std::hex << std::setfill('0') << std::setw(16) << value;
return out.str();
}
std::string BuildRedaction(std::string_view reason, std::string_view original, const Config& config) {
std::string out = "[REDACTED:";
out += reason;
if (config.hash_redacted) {
out += ":fnv1a64=";
out += Hex64(Fnv1a64(original));
}
out += "]";
return out;
}
bool HasMixedClasses(std::string_view token) {
bool has_lower = false;
bool has_upper = false;
bool has_digit = false;
for (unsigned char ch : token) {
has_lower |= std::islower(ch) != 0;
has_upper |= std::isupper(ch) != 0;
has_digit |= std::isdigit(ch) != 0;
}
return (has_lower && has_upper) || (has_lower && has_digit) || (has_upper && has_digit);
}
bool LooksHighEntropyToken(std::string_view token) {
if (token.size() < 24) {
return false;
}
for (const auto prefix : kSuspiciousPrefixes) {
if (token.rfind(prefix, 0) == 0) {
return true;
}
}
std::size_t valid_chars = 0;
std::array<bool, 256> seen{};
for (unsigned char ch : token) {
if (!IsLikelyBase64ish(static_cast<char>(ch))) {
return false;
}
++valid_chars;
seen[ch] = true;
}
std::size_t distinct = 0;
for (bool bit : seen) {
distinct += bit ? 1u : 0u;
}
return valid_chars >= 24 && distinct >= 10 && HasMixedClasses(token);
}
std::string MaybeTruncateValue(std::string_view value, Stats& stats, const Config& config) {
if (value.size() <= config.max_value_chars) {
return std::string(value);
}
++stats.values_truncated;
std::string out(value.substr(0, config.max_value_chars));
out += "...[TRUNCATED len=";
out += std::to_string(value.size());
out += "]";
return out;
}
bool ParseJsonString(const std::string& input, std::size_t start, std::size_t& end, std::string& decoded) {
if (start >= input.size() || input[start] != '"') {
return false;
}
decoded.clear();
bool escape = false;
for (std::size_t i = start + 1; i < input.size(); ++i) {
char ch = input[i];
if (escape) {
switch (ch) {
case '"': decoded.push_back('"'); break;
case '\\': decoded.push_back('\\'); break;
case '/': decoded.push_back('/'); break;
case 'b': decoded.push_back('\b'); break;
case 'f': decoded.push_back('\f'); break;
case 'n': decoded.push_back('\n'); break;
case 'r': decoded.push_back('\r'); break;
case 't': decoded.push_back('\t'); break;
case 'u':
decoded.push_back('?');
break;
default:
decoded.push_back(ch);
break;
}
escape = false;
continue;
}
if (ch == '\\') {
escape = true;
continue;
}
if (ch == '"') {
end = i + 1;
return true;
}
decoded.push_back(ch);
}
return false;
}
std::string JsonEscape(std::string_view input) {
std::string out;
out.reserve(input.size() + 8);
for (unsigned char ch : input) {
switch (ch) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\b': out += "\\b"; break;
case '\f': out += "\\f"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (ch < 0x20) {
std::ostringstream oss;
oss << "\\u" << std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(ch);
out += oss.str();
} else {
out.push_back(static_cast<char>(ch));
}
break;
}
}
return out;
}
std::string RedactInlineText(const std::string& input, Stats& stats, const Config& config) {
std::string out;
out.reserve(input.size());
std::size_t i = 0;
while (i < input.size()) {
bool matched = false;
for (const auto prefix : kSuspiciousPrefixes) {
if (i + prefix.size() <= input.size() && input.compare(i, prefix.size(), prefix.data(), prefix.size()) == 0) {
std::size_t j = i + prefix.size();
while (j < input.size() && IsLikelyBase64ish(input[j])) {
++j;
}
if (j - i >= 12) {
out += BuildRedaction("token", std::string_view(input).substr(i, j - i), config);
++stats.inline_tokens_redacted;
i = j;
matched = true;
break;
}
}
}
if (matched) {
continue;
}
if (IsLikelyBase64ish(input[i])) {
std::size_t j = i;
while (j < input.size() && IsLikelyBase64ish(input[j])) {
++j;
}
std::string_view token(input.data() + i, j - i);
if (LooksHighEntropyToken(token)) {
out += BuildRedaction("entropy", token, config);
++stats.high_entropy_redacted;
} else {
out.append(token);
}
i = j;
continue;
}
out.push_back(input[i]);
++i;
}
return out;
}
std::string ProcessJsonLikeLine(const std::string& line, Stats& stats, const Config& config) {
std::string out;
out.reserve(line.size() + 32);
std::vector<std::string> key_stack;
std::size_t i = 0;
while (i < line.size()) {
if (line[i] == '"') {
std::size_t end = i;
std::string decoded;
if (!ParseJsonString(line, i, end, decoded)) {
return RedactInlineText(line, stats, config);
}
std::size_t lookahead = end;
while (lookahead < line.size() && std::isspace(static_cast<unsigned char>(line[lookahead]))) {
++lookahead;
}
bool is_key = lookahead < line.size() && line[lookahead] == ':';
if (is_key) {
key_stack.push_back(decoded);
out.append(line, i, end - i);
i = end;
continue;
}
bool sensitive_context = !key_stack.empty() && ContainsSensitiveKeyFragment(key_stack.back());
if (sensitive_context) {
std::string replacement = BuildRedaction(key_stack.back(), decoded, config);
out.push_back('"');
out += JsonEscape(replacement);
out.push_back('"');
++stats.json_keys_redacted;
} else {
std::string candidate = MaybeTruncateValue(decoded, stats, config);
candidate = RedactInlineText(candidate, stats, config);
out.push_back('"');
out += JsonEscape(candidate);
out.push_back('"');
}
i = end;
continue;
}
if (!key_stack.empty()) {
if (line[i] == ',') {
key_stack.pop_back();
} else if (line[i] == '}' || line[i] == ']') {
key_stack.pop_back();
}
}
out.push_back(line[i]);
++i;
}
return out;
}
bool LooksJsonLike(std::string_view line) {
for (char ch : line) {
if (std::isspace(static_cast<unsigned char>(ch))) {
continue;
}
return ch == '{' || ch == '[' || ch == '"';
}
return false;
}
void PrintUsage(const char* argv0) {
std::cerr
<< "Usage: " << argv0 << " [--input PATH] [--output PATH] [--max-line-bytes N]"
<< " [--max-value-chars N] [--no-hash] [--stats]\n";
}
bool ParseSizeArg(std::string_view value, std::size_t& out) {
unsigned long long parsed = 0;
auto result = std::from_chars(value.data(), value.data() + value.size(), parsed);
if (result.ec != std::errc{} || result.ptr != value.data() + value.size()) {
return false;
}
if (parsed > std::numeric_limits<std::size_t>::max()) {
return false;
}
out = static_cast<std::size_t>(parsed);
return true;
}
int Run(int argc, char** argv) {
Config config;
std::optional<std::string> input_path;
std::optional<std::string> output_path;
for (int i = 1; i < argc; ++i) {
std::string_view arg = argv[i];
auto require_value = [&](std::string_view name) -> std::string_view {
if (i + 1 >= argc) {
throw std::runtime_error("missing value for " + std::string(name));
}
return argv[++i];
};
if (arg == "--input") {
input_path = std::string(require_value(arg));
} else if (arg == "--output") {
output_path = std::string(require_value(arg));
} else if (arg == "--max-line-bytes") {
if (!ParseSizeArg(require_value(arg), config.max_line_bytes)) {
throw std::runtime_error("invalid --max-line-bytes");
}
} else if (arg == "--max-value-chars") {
if (!ParseSizeArg(require_value(arg), config.max_value_chars)) {
throw std::runtime_error("invalid --max-value-chars");
}
} else if (arg == "--no-hash") {
config.hash_redacted = false;
} else if (arg == "--stats") {
config.pretty_stats = true;
} else if (arg == "--help" || arg == "-h") {
PrintUsage(argv[0]);
return 0;
} else {
throw std::runtime_error("unknown argument: " + std::string(arg));
}
}
std::ifstream file_in;
std::ofstream file_out;
std::istream* in = &std::cin;
std::ostream* out = &std::cout;
if (input_path) {
file_in.open(*input_path, std::ios::binary);
if (!file_in) {
throw std::runtime_error("failed to open input: " + *input_path);
}
in = &file_in;
}
if (output_path) {
file_out.open(*output_path, std::ios::binary | std::ios::trunc);
if (!file_out) {
throw std::runtime_error("failed to open output: " + *output_path);
}
out = &file_out;
}
Stats stats;
std::string line;
while (std::getline(*in, line)) {
++stats.lines_seen;
if (line.size() > config.max_line_bytes) {
++stats.line_too_large;
*out << "{\"redaction_error\":\"line_too_large\",\"bytes\":" << line.size() << "}\n";
++stats.lines_emitted;
continue;
}
std::string sanitized = LooksJsonLike(line)
? ProcessJsonLikeLine(line, stats, config)
: RedactInlineText(line, stats, config);
*out << sanitized << '\n';
++stats.lines_emitted;
}
if (!*out) {
throw std::runtime_error("write failed");
}
if (config.pretty_stats) {
std::cerr
<< "lines_seen=" << stats.lines_seen
<< " lines_emitted=" << stats.lines_emitted
<< " line_too_large=" << stats.line_too_large
<< " json_keys_redacted=" << stats.json_keys_redacted
<< " inline_tokens_redacted=" << stats.inline_tokens_redacted
<< " high_entropy_redacted=" << stats.high_entropy_redacted
<< " values_truncated=" << stats.values_truncated
<< '\n';
}
return 0;
}
} // namespace
int main(int argc, char** argv) {
try {
return Run(argc, argv);
} catch (const std::exception& ex) {
std::cerr << "JsonlSecretFirewall error: " << ex.what() << '\n';
return 1;
}
}
/*
This solves secret leaks and oversized payload leaks in JSONL logs, trace exports, and AI gateway stream archives. Built because modern LLM apps, MCP servers, CI jobs, and edge workers still dump bearer tokens, webhook secrets, cookies, and giant prompt blobs into logs when something fails at 2 AM. Use it when you need a fast single-file C++ log scrubber before shipping data to S3, ClickHouse, BigQuery, Datadog, Loki, or any internal data pipeline. The trick: it handles JSON-looking lines with key-aware redaction, also catches inline token patterns and high-entropy blobs in plain text, and keeps output line-oriented so downstream tools do not break. Drop this into a security sidecar, observability agent, build pipeline, ingest worker, or incident cleanup path when you want practical secret redaction for JSONL, NDJSON, AI traces, SSE archives, and developer logs without pulling in a large dependency stack.
*/