File size: 3,211 Bytes
d197cf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/** Windows cmd `set` assignment renderer/parser for managed service scripts. */
type CmdSetAssignment = { key: string; value: string };

/** Rejects line breaks before rendering values into Windows cmd scripts. */
export function assertNoCmdLineBreak(value: string, field: string): void {
  if (/[\r\n]/.test(value)) {
    throw new Error(`${field} cannot contain CR or LF in Windows task scripts.`);
  }
}

function escapeCmdSetAssignmentComponent(value: string, delayedExpansion: boolean): string {
  // Keep the service-script encoding/readback contract by default. A launcher
  // that disables delayed expansion must not insert literal carets into paths.
  const escaped = delayedExpansion ? value.replace(/\^/g, "^^").replace(/!/g, "^!") : value;
  return escaped.replace(/%/g, "%%").replace(/"/g, '^"');
}

function unescapeCmdSetAssignmentComponent(value: string): string {
  let out = "";
  for (let i = 0; i < value.length; i += 1) {
    const ch = value[i];
    const next = value[i + 1];
    if (ch === "^" && (next === "^" || next === '"' || next === "!")) {
      out += next;
      i += 1;
      continue;
    }
    if (ch === "%" && next === "%") {
      out += "%";
      i += 1;
      continue;
    }
    out += ch;
  }
  return out;
}

export function parseCmdSetAssignment(
  line: string,
  requireLiteral = false,
): CmdSetAssignment | null {
  const raw = requireLiteral ? line.trimStart() : line.trim();
  if (!raw) {
    return null;
  }
  const quoted = raw.startsWith('"') && raw.endsWith('"') && raw.length >= 2;
  const assignment = quoted ? raw.slice(1, -1) : raw;
  const index = assignment.indexOf("=");
  if (index <= 0) {
    return null;
  }
  const key = assignment.slice(0, index).trim();
  const value = requireLiteral ? assignment.slice(index + 1) : assignment.slice(index + 1).trim();
  if (!key) {
    return null;
  }
  // Batch expansions and caret/quote decoding depend on the command processor.
  // Strict service inspection must not report those expressions as effective facts.
  if (
    requireLiteral &&
    (key !== assignment.slice(0, index) ||
      /[%!^"]/.test(assignment.replace(/%%/g, "")) ||
      (!quoted && (assignment.startsWith("/") || /[&|<>()]/.test(assignment))))
  ) {
    return null;
  }
  if (!quoted && !requireLiteral) {
    return { key, value };
  }
  // Recovery decodes managed-script escapes; validated literal input can only
  // contain paired percent escapes, including in unquoted assignments.
  return {
    // Windows names are case-insensitive; strict maps keep the last assignment.
    key: unescapeCmdSetAssignmentComponent(requireLiteral ? key.toUpperCase() : key),
    value: unescapeCmdSetAssignmentComponent(value),
  };
}

export function renderCmdSetAssignment(
  key: string,
  value: string,
  options: { delayedExpansion?: boolean } = {},
): string {
  assertNoCmdLineBreak(key, "Environment variable name");
  assertNoCmdLineBreak(value, "Environment variable value");
  const escapedKey = escapeCmdSetAssignmentComponent(key, options.delayedExpansion !== false);
  const escapedValue = escapeCmdSetAssignmentComponent(value, options.delayedExpansion !== false);
  return `set "${escapedKey}=${escapedValue}"`;
}