@zereight/mcp-gitlab 2.3.29-next.248 → 2.3.29-next.249

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -242,6 +242,21 @@ function readJsonStringToken(token) {
242
242
  }
243
243
  return parsed;
244
244
  }
245
+ /**
246
+ * GitLab marks CI/CD variables as `masked` or `hidden` (masked and hidden) so
247
+ * their values stay out of job logs and the UI. The variables API still returns
248
+ * the value, so with masking enabled the whole `value` of such a variable
249
+ * object is replaced, whatever it looks like.
250
+ */
251
+ export const FLAGGED_CI_VARIABLE_REPLACEMENT = "[Masked CI variable value]";
252
+ function isFlaggedGitlabCiVariable(value) {
253
+ const record = value;
254
+ return (typeof record.key === "string" &&
255
+ typeof record.value === "string" &&
256
+ (record.masked === true || record.hidden === true));
257
+ }
258
+ // The literal after a JSON property name, up to the next `,` or `}`.
259
+ const JSON_TRUE_VALUE_GAP = /^\s*:\s*true\s*$/;
245
260
  function readGitlabCiAwsSecretKey(value) {
246
261
  if (!("key" in value))
247
262
  return undefined;
@@ -265,8 +280,13 @@ function maskStrings(value, mask, parentKey) {
265
280
  return value.map(item => maskStrings(item, mask, parentKey));
266
281
  if (value && typeof value === "object") {
267
282
  const ciKey = readGitlabCiAwsSecretKey(value);
283
+ const flaggedCiVariable = isFlaggedGitlabCiVariable(value);
268
284
  const output = {};
269
285
  for (const [key, item] of Object.entries(value)) {
286
+ if (flaggedCiVariable && key === "value") {
287
+ output[key] = FLAGGED_CI_VARIABLE_REPLACEMENT;
288
+ continue;
289
+ }
270
290
  const fieldKey = key === "value" && ciKey !== undefined ? ciKey : key;
271
291
  output[key] = maskStrings(item, mask, fieldKey);
272
292
  }
@@ -295,13 +315,27 @@ function maskJsonText(text, mask) {
295
315
  const scopes = [];
296
316
  const stringValues = [];
297
317
  const objectCiKeys = new Map();
318
+ // Objects shaped like a GitLab CI variable: a string `key`, and `masked` or
319
+ // `hidden` set to the literal true. Both are collected before any rewrite.
320
+ const objectsWithStringKey = new Set();
321
+ const objectsFlaggedMasked = new Set();
298
322
  let nextObjectId = 0;
323
+ let previousTokenEnd = 0;
299
324
  for (const match of text.matchAll(JSON_STRING_OR_STRUCTURE_PATTERN)) {
300
325
  const token = match[0];
301
326
  const index = match.index;
302
327
  if (index === undefined)
303
328
  continue;
329
+ const gap = text.slice(previousTokenEnd, index);
330
+ previousTokenEnd = index + token.length;
304
331
  const scope = scopes[scopes.length - 1];
332
+ if ((token === "," || token === "}") &&
333
+ scope?.isObject &&
334
+ !scope.expectingKey &&
335
+ (scope.fieldName === "masked" || scope.fieldName === "hidden") &&
336
+ JSON_TRUE_VALUE_GAP.test(gap)) {
337
+ objectsFlaggedMasked.add(scope.objectId);
338
+ }
305
339
  if (token === "{") {
306
340
  scopes.push({
307
341
  isObject: true,
@@ -336,8 +370,10 @@ function maskJsonText(text, mask) {
336
370
  scope.expectingKey = false;
337
371
  continue;
338
372
  }
339
- if (scope?.isObject && scope.fieldName === "key" && AWS_SECRET_FIELD_NAMES.has(decoded)) {
340
- objectCiKeys.set(scope.objectId, decoded);
373
+ if (scope?.isObject && scope.fieldName === "key") {
374
+ objectsWithStringKey.add(scope.objectId);
375
+ if (AWS_SECRET_FIELD_NAMES.has(decoded))
376
+ objectCiKeys.set(scope.objectId, decoded);
341
377
  }
342
378
  stringValues.push({
343
379
  index,
@@ -345,12 +381,21 @@ function maskJsonText(text, mask) {
345
381
  decoded,
346
382
  fieldName: scope?.fieldName,
347
383
  objectId: scope?.objectId ?? -1,
384
+ isObjectProperty: scope?.isObject === true,
348
385
  });
349
386
  }
350
387
  let output = "";
351
388
  let cursor = 0;
352
389
  for (const item of stringValues) {
353
390
  output += text.slice(cursor, item.index);
391
+ if (item.isObjectProperty &&
392
+ item.fieldName === "value" &&
393
+ objectsWithStringKey.has(item.objectId) &&
394
+ objectsFlaggedMasked.has(item.objectId)) {
395
+ output += JSON.stringify(FLAGGED_CI_VARIABLE_REPLACEMENT);
396
+ cursor = item.index + item.token.length;
397
+ continue;
398
+ }
354
399
  const ciKey = objectCiKeys.get(item.objectId);
355
400
  const parentKey = item.fieldName === "value" && ciKey !== undefined ? ciKey : item.fieldName;
356
401
  const masked = maskFieldValue(item.decoded, parentKey, mask);
@@ -359,11 +404,352 @@ function maskJsonText(text, mask) {
359
404
  }
360
405
  return output + text.slice(cursor);
361
406
  }
407
+ /**
408
+ * Regex source for a JSON property name in raw text. Each letter may also be
409
+ * written as a JSON Unicode escape (`va\u006cue`), with one backslash in plain
410
+ * JSON or two in JSON printed inside a string, so escaped names still match.
411
+ */
412
+ function jsonNamePattern(name) {
413
+ return Array.from(name, char => {
414
+ const hex = Array.from(char.charCodeAt(0).toString(16).padStart(4, "0"), digit => /[a-f]/.test(digit) ? `[${digit}${digit.toUpperCase()}]` : digit).join("");
415
+ return `(?:${char}|\\\\{1,2}u${hex})`;
416
+ }).join("");
417
+ }
418
+ const VALUE_NAME_PATTERN = jsonNamePattern("value");
419
+ // A flag literal in raw text. The escaped form covers JSON that is itself a
420
+ // string value inside prose (`\"masked\":true`).
421
+ const FLAG_LITERAL_PATTERN = new RegExp(`\\\\?"(?:${jsonNamePattern("masked")}|${jsonNamePattern("hidden")})\\\\?"\\s*:\\s*true(?![A-Za-z0-9_])`);
422
+ // A `"value"` property with a JSON string value, used only in uncovered text.
423
+ // A value cut off before its closing quote runs to the end of the text.
424
+ const UNCOVERED_VALUE_PATTERN = new RegExp(`("${VALUE_NAME_PATTERN}"\\s*:\\s*)"(?:\\\\.|[^"\\\\])*(?:"|\\\\?$)`, "g");
425
+ // The start of the same property in escaped JSON (`\"value\":\"..."`), e.g. a
426
+ // log line that prints a JSON string. The closing quote is found by
427
+ // findEndOfEscapedJsonString so an embedded `\\\"` cannot end the value.
428
+ const UNCOVERED_ESCAPED_VALUE_START_PATTERN = new RegExp(`\\\\"${VALUE_NAME_PATTERN}\\\\"\\s*:\\s*\\\\"`, "g");
429
+ // A JSON Unicode escape such as `\u0073` in `ma\u0073ked`. Used to skip the
430
+ // text fast path when a flag name may be escaped this way.
431
+ const JSON_UNICODE_ESCAPE_PATTERN = /\\u[0-9a-fA-F]{4}/;
432
+ const FLAGGED_TEXT_SCAN_FACTOR = 8;
433
+ const FLAGGED_TEXT_SCAN_SLACK = 65536;
434
+ /** Index of the `}` closing the `{` at `open`, or -1. Charges the characters it walks. */
435
+ function findClosingBrace(text, open, budget) {
436
+ const limit = Math.min(text.length, open + budget.remaining);
437
+ let depth = 0;
438
+ let inString = false;
439
+ let index = open;
440
+ let close = -1;
441
+ for (; index < limit; index++) {
442
+ const char = text[index];
443
+ if (inString) {
444
+ if (char === "\\")
445
+ index++;
446
+ else if (char === '"')
447
+ inString = false;
448
+ }
449
+ else if (char === '"') {
450
+ inString = true;
451
+ }
452
+ else if (char === "{" || char === "[") {
453
+ depth++;
454
+ }
455
+ else if (char === "}" || char === "]") {
456
+ depth--;
457
+ if (depth === 0) {
458
+ if (char === "}")
459
+ close = index;
460
+ break;
461
+ }
462
+ }
463
+ }
464
+ budget.remaining -= Math.min(index + 1, limit) - open;
465
+ return close;
466
+ }
467
+ /**
468
+ * Index just past the `"` that closes a JSON string whose content starts at
469
+ * `from`, or the end of the text when it never closes. Charges the
470
+ * characters it walks.
471
+ */
472
+ function findEndOfJsonString(text, from, budget) {
473
+ let index = from;
474
+ for (; index < text.length; index++) {
475
+ const char = text[index];
476
+ if (char === "\\")
477
+ index++;
478
+ else if (char === '"')
479
+ break;
480
+ }
481
+ budget.remaining -= Math.min(index + 1, text.length) - from;
482
+ return index < text.length ? index + 1 : text.length;
483
+ }
484
+ function parsesAsJsonObject(span) {
485
+ try {
486
+ const parsed = JSON.parse(span);
487
+ return typeof parsed === "object" && parsed !== null;
488
+ }
489
+ catch {
490
+ return false;
491
+ }
492
+ }
493
+ /**
494
+ * Index just past the `\"` that closes an escaped JSON string starting at
495
+ * `from` (the first character after the opening `\"`), or -1 if it never
496
+ * closes. `\\` delivers a backslash to the inner string and may start an
497
+ * inner escape; `\"` ends the string unless the inner string is escaping.
498
+ */
499
+ function findEndOfEscapedJsonString(text, from) {
500
+ let innerEscaped = false;
501
+ for (let index = from; index < text.length; index++) {
502
+ const char = text[index];
503
+ if (char === "\\" && index + 1 < text.length) {
504
+ const next = text[index + 1];
505
+ if (next === "\\") {
506
+ innerEscaped = !innerEscaped;
507
+ index++;
508
+ continue;
509
+ }
510
+ if (next === '"') {
511
+ if (!innerEscaped)
512
+ return index + 2;
513
+ innerEscaped = false;
514
+ index++;
515
+ continue;
516
+ }
517
+ }
518
+ innerEscaped = false;
519
+ }
520
+ return -1;
521
+ }
522
+ function replaceUncoveredEscapedValues(gap) {
523
+ let output = "";
524
+ let cursor = 0;
525
+ UNCOVERED_ESCAPED_VALUE_START_PATTERN.lastIndex = 0;
526
+ for (const match of gap.matchAll(UNCOVERED_ESCAPED_VALUE_START_PATTERN)) {
527
+ const start = match.index;
528
+ if (start === undefined || start < cursor)
529
+ continue;
530
+ const close = findEndOfEscapedJsonString(gap, start + match[0].length);
531
+ output +=
532
+ gap.slice(cursor, start) + match[0].slice(0, -2) + `\\"${FLAGGED_CI_VARIABLE_REPLACEMENT}\\"`;
533
+ // A value cut off before its closing quote runs to the end of the gap.
534
+ cursor = close === -1 ? gap.length : close;
535
+ }
536
+ return output + gap.slice(cursor);
537
+ }
538
+ /**
539
+ * Fallback for text outside any valid JSON object. Only applied when that text
540
+ * also holds a flag literal, so `value` fields in unflagged text are untouched.
541
+ * Over-masks any `value` string in a broken gap that carries a flag.
542
+ */
543
+ function replaceUncoveredValues(gap) {
544
+ if (!FLAG_LITERAL_PATTERN.test(gap))
545
+ return gap;
546
+ return replaceUncoveredEscapedValues(gap.replace(UNCOVERED_VALUE_PATTERN, (_match, prefix) => prefix + JSON.stringify(FLAGGED_CI_VARIABLE_REPLACEMENT)));
547
+ }
548
+ function newGapStructure(start) {
549
+ return {
550
+ start,
551
+ scanned: start,
552
+ inString: false,
553
+ stringStart: -1,
554
+ escaped: false,
555
+ open: [],
556
+ firstObject: -1,
557
+ last: undefined,
558
+ };
559
+ }
560
+ /** Reads `[structure.scanned, end)` into `structure`. Charges the characters it reads. */
561
+ function advanceGapStructure(structure, text, end, budget) {
562
+ if (end <= structure.scanned)
563
+ return;
564
+ budget.remaining -= end - structure.scanned;
565
+ for (let index = structure.scanned; index < end; index++) {
566
+ const char = text[index];
567
+ if (structure.inString) {
568
+ if (structure.escaped)
569
+ structure.escaped = false;
570
+ else if (char === "\\")
571
+ structure.escaped = true;
572
+ else if (char === '"') {
573
+ structure.inString = false;
574
+ structure.last = '"';
575
+ }
576
+ continue;
577
+ }
578
+ if (/\s/.test(char))
579
+ continue;
580
+ structure.last = char;
581
+ if (char === '"') {
582
+ structure.inString = true;
583
+ structure.stringStart = index;
584
+ }
585
+ else if (char === "{" || char === "[") {
586
+ if (char === "{" && structure.firstObject === -1)
587
+ structure.firstObject = structure.open.length;
588
+ structure.open.push({ char, index });
589
+ }
590
+ else if ((char === "}" || char === "]") && structure.open.length > 0) {
591
+ structure.open.pop();
592
+ if (structure.open.length <= structure.firstObject)
593
+ structure.firstObject = -1;
594
+ }
595
+ }
596
+ structure.scanned = end;
597
+ }
598
+ /** Start and end of each flag literal in `text`, in order. */
599
+ function flagLiteralRanges(text) {
600
+ const pattern = new RegExp(FLAG_LITERAL_PATTERN.source, "g");
601
+ return Array.from(text.matchAll(pattern), match => [
602
+ match.index ?? 0,
603
+ (match.index ?? 0) + match[0].length,
604
+ ]);
605
+ }
606
+ /** True when some range lies inside `[start, end)`. `ranges` is sorted. */
607
+ function hasRangeWithin(ranges, start, end) {
608
+ let low = 0;
609
+ let high = ranges.length;
610
+ while (low < high) {
611
+ const middle = (low + high) >> 1;
612
+ if (ranges[middle][0] < start)
613
+ low = middle + 1;
614
+ else
615
+ high = middle;
616
+ }
617
+ return low < ranges.length && ranges[low][1] <= end;
618
+ }
619
+ /**
620
+ * True when the object at `open` is nested inside an incomplete flagged
621
+ * object in the current gap: it is a property value, an array element, or
622
+ * text inside an open string there, and that parent holds a flag before or
623
+ * after `open`. Also true when `open` is text inside a string at gap level
624
+ * (for example an escaped record in a quoted log argument) and the gap holds
625
+ * a flag before that string ends. Such a span must not split the gap, or a
626
+ * value and its flag could land in different gaps. `parentEnd` gives the end
627
+ * of the parent at an index, and `stringEnd` the end of the string opened at
628
+ * an index (the end of the text when either never closes).
629
+ */
630
+ function isNestedInIncompleteFlaggedObject(structure, flagRanges, open, parentEnd, stringEnd) {
631
+ if (structure.inString &&
632
+ hasRangeWithin(flagRanges, structure.start, stringEnd(structure.stringStart)))
633
+ return true;
634
+ if (structure.firstObject === -1)
635
+ return false;
636
+ const parent = structure.open[structure.firstObject];
637
+ if (!hasRangeWithin(flagRanges, parent.index, parentEnd(parent.index)))
638
+ return false;
639
+ // A brace inside an open string is string content, not a nested object.
640
+ if (structure.inString)
641
+ return true;
642
+ const innermost = structure.open[structure.open.length - 1];
643
+ if (innermost.char === "[")
644
+ return true;
645
+ return structure.last === ":";
646
+ }
647
+ /**
648
+ * Replaces the value of each GitLab CI/CD variable flagged masked or hidden in
649
+ * text that is not pure JSON: JSON in prose, job logs, error messages, and JSON
650
+ * inside a JSON string. Each `{` is tried as the start of a complete object.
651
+ * A span that parses as an object goes through the JSON scanner in flagged-only
652
+ * mode, so every other byte is kept. Text outside those spans goes through
653
+ * replaceUncoveredValues. Limits:
654
+ * - A truncated object cut before its flag cannot be detected, because the
655
+ * flag is not in the text yet.
656
+ * - Text after the scan budget runs out is handled as uncovered text.
657
+ * - In a broken span that carries a flag, the fallback masks any `"value"`
658
+ * string, including values of complete objects nested in that span. Those
659
+ * nested objects also go through maskJsonText first.
660
+ */
661
+ function maskFlaggedCiVariablesInText(text, budget = {
662
+ remaining: FLAGGED_TEXT_SCAN_FACTOR * text.length + FLAGGED_TEXT_SCAN_SLACK,
663
+ }) {
664
+ if (!FLAG_LITERAL_PATTERN.test(text) && !JSON_UNICODE_ESCAPE_PATTERN.test(text))
665
+ return text;
666
+ const pieces = [];
667
+ let gapStart = 0;
668
+ let structure = newGapStructure(0);
669
+ let flagRanges;
670
+ // Parents start at increasing indexes, so one cached entry scans each once.
671
+ let cachedParent = -1;
672
+ let cachedParentEnd = text.length;
673
+ const parentEnd = (index) => {
674
+ if (index !== cachedParent) {
675
+ const close = findClosingBrace(text, index, budget);
676
+ cachedParent = index;
677
+ cachedParentEnd = close === -1 ? text.length : close + 1;
678
+ }
679
+ return cachedParentEnd;
680
+ };
681
+ // Strings also start at increasing indexes within and across gaps.
682
+ let cachedString = -1;
683
+ let cachedStringEnd = text.length;
684
+ const stringEnd = (index) => {
685
+ if (index !== cachedString) {
686
+ cachedString = index;
687
+ cachedStringEnd = findEndOfJsonString(text, index + 1, budget);
688
+ }
689
+ return cachedStringEnd;
690
+ };
691
+ const maskSpan = (span) => maskJsonText(span, (value) => maskFlaggedCiVariablesInText(value, budget));
692
+ // Complete objects kept inside the current gap, outermost only, in order.
693
+ // They still go through maskJsonText, which decodes escaped property names
694
+ // that the uncovered-text fallback cannot match.
695
+ let retained = [];
696
+ const maskGap = (end) => {
697
+ let gap = "";
698
+ let cursor = gapStart;
699
+ for (const [spanStart, spanEnd] of retained) {
700
+ if (spanEnd > end)
701
+ break;
702
+ budget.remaining -= spanEnd - spanStart;
703
+ gap += text.slice(cursor, spanStart) + maskSpan(text.slice(spanStart, spanEnd));
704
+ cursor = spanEnd;
705
+ }
706
+ retained = [];
707
+ return replaceUncoveredValues(gap + text.slice(cursor, end));
708
+ };
709
+ let open = text.indexOf("{");
710
+ while (open !== -1 && budget.remaining > 0) {
711
+ const close = findClosingBrace(text, open, budget);
712
+ if (close !== -1) {
713
+ const spanLength = close - open + 1;
714
+ if (budget.remaining < spanLength)
715
+ break;
716
+ budget.remaining -= spanLength;
717
+ const span = text.slice(open, close + 1);
718
+ if (parsesAsJsonObject(span)) {
719
+ advanceGapStructure(structure, text, open, budget);
720
+ flagRanges ??= flagLiteralRanges(text);
721
+ if (isNestedInIncompleteFlaggedObject(structure, flagRanges, open, parentEnd, stringEnd)) {
722
+ const previous = retained[retained.length - 1];
723
+ if (previous === undefined || open >= previous[1])
724
+ retained.push([open, close + 1]);
725
+ open = text.indexOf("{", open + 1);
726
+ continue;
727
+ }
728
+ pieces.push(maskGap(open));
729
+ // maskJsonText parses the span again.
730
+ budget.remaining -= spanLength;
731
+ pieces.push(maskSpan(span));
732
+ gapStart = close + 1;
733
+ structure = newGapStructure(gapStart);
734
+ open = text.indexOf("{", close + 1);
735
+ continue;
736
+ }
737
+ }
738
+ open = text.indexOf("{", open + 1);
739
+ }
740
+ pieces.push(maskGap(text.length));
741
+ return pieces.join("");
742
+ }
362
743
  function createMaskingEngineFromFile(file, configPath) {
363
744
  const builtinRules = mergeBuiltinRules(file);
364
745
  const rules = [...(file.rules ?? []), ...builtinRules].map(compileRule);
365
746
  const maskedErrors = new WeakSet();
366
- const maskText = (text) => {
747
+ const maskText = (source) => {
748
+ // Flagged variable values are replaced first, so the regex rules below see
749
+ // the replacement text. On the text path that replacement is therefore
750
+ // subject to user keyword rules, while structured and JSON paths emit it
751
+ // verbatim.
752
+ const text = maskFlaggedCiVariablesInText(source);
367
753
  const matches = [];
368
754
  for (const rule of rules) {
369
755
  rule.expression.lastIndex = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zereight/mcp-gitlab",
3
- "version": "2.3.29-next.248",
3
+ "version": "2.3.29-next.249",
4
4
  "mcpName": "io.github.zereight/gitlab-mcp",
5
5
  "description": "GitLab MCP server for projects, merge requests, issues, pipelines, wiki, releases, and more",
6
6
  "keywords": [