mirror of
https://github.com/actions/runner.git
synced 2026-02-13 00:44:53 +08:00
Compare commits
1 Commits
dependabot
...
users/eric
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3ff338a0b |
@@ -174,6 +174,7 @@ namespace GitHub.Runner.Common
|
||||
public static readonly string CompareWorkflowParser = "actions_runner_compare_workflow_parser";
|
||||
public static readonly string SetOrchestrationIdEnvForActions = "actions_set_orchestration_id_env_for_actions";
|
||||
public static readonly string SendJobLevelAnnotations = "actions_send_job_level_annotations";
|
||||
public static readonly string EmitCompositeMarkers = "actions_runner_emit_composite_markers";
|
||||
}
|
||||
|
||||
// Node version migration related constants
|
||||
@@ -284,6 +285,7 @@ namespace GitHub.Runner.Common
|
||||
public static readonly string ForcedActionsNodeVersion = "ACTIONS_RUNNER_FORCE_ACTIONS_NODE_VERSION";
|
||||
public static readonly string PrintLogToStdout = "ACTIONS_RUNNER_PRINT_LOG_TO_STDOUT";
|
||||
public static readonly string ActionArchiveCacheDirectory = "ACTIONS_RUNNER_ACTION_ARCHIVE_CACHE";
|
||||
public static readonly string EmitCompositeMarkers = "ACTIONS_RUNNER_EMIT_COMPOSITE_MARKERS";
|
||||
}
|
||||
|
||||
public static class System
|
||||
|
||||
@@ -226,10 +226,34 @@ namespace GitHub.Runner.Worker.Handlers
|
||||
{
|
||||
ArgUtil.NotNull(embeddedSteps, nameof(embeddedSteps));
|
||||
|
||||
bool emitCompositeMarkers =
|
||||
(ExecutionContext.Global.Variables.GetBoolean(Constants.Runner.Features.EmitCompositeMarkers) ?? false)
|
||||
|| StringUtil.ConvertToBoolean(
|
||||
System.Environment.GetEnvironmentVariable(Constants.Variables.Agent.EmitCompositeMarkers));
|
||||
|
||||
// Read nesting prefix from execution context (set by parent composite, if any)
|
||||
string idPrefix = "";
|
||||
if (emitCompositeMarkers)
|
||||
{
|
||||
idPrefix = ExecutionContext.Global.Variables.Get("system.compositeMarkerIdPrefix") ?? "";
|
||||
}
|
||||
|
||||
foreach (IStep step in embeddedSteps)
|
||||
{
|
||||
Trace.Info($"Processing embedded step: DisplayName='{step.DisplayName}'");
|
||||
|
||||
// Build step ID from ContextName for markers
|
||||
var contextName = (step as IActionRunner)?.Action?.ContextName ?? $"__{embeddedSteps.IndexOf(step)}";
|
||||
var stepId = string.IsNullOrEmpty(idPrefix) ? contextName : $"{idPrefix}.{contextName}";
|
||||
var stepStopwatch = new System.Diagnostics.Stopwatch();
|
||||
|
||||
// Emit start marker before condition evaluation
|
||||
if (emitCompositeMarkers)
|
||||
{
|
||||
ExecutionContext.Output($"##[start-action display={EscapeProperty(SanitizeDisplayName(step.DisplayName))};id={EscapeProperty(stepId)}]");
|
||||
stepStopwatch.Start();
|
||||
}
|
||||
|
||||
// Add Expression Functions
|
||||
step.ExecutionContext.ExpressionFunctions.Add(new FunctionInfo<HashFilesFunction>(PipelineTemplateConstants.HashFiles, 1, byte.MaxValue));
|
||||
step.ExecutionContext.ExpressionFunctions.Add(new FunctionInfo<AlwaysFunction>(PipelineTemplateConstants.Always, 0, 0));
|
||||
@@ -381,6 +405,13 @@ namespace GitHub.Runner.Worker.Handlers
|
||||
// Condition is false
|
||||
Trace.Info("Skipping step due to condition evaluation.");
|
||||
SetStepConclusion(step, TaskResult.Skipped);
|
||||
|
||||
if (emitCompositeMarkers)
|
||||
{
|
||||
stepStopwatch.Stop();
|
||||
ExecutionContext.Output($"##[end-action id={EscapeProperty(stepId)};outcome=skipped;conclusion=skipped;duration_ms=0]");
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
else if (conditionEvaluateError != null)
|
||||
@@ -389,11 +420,40 @@ namespace GitHub.Runner.Worker.Handlers
|
||||
step.ExecutionContext.Error(conditionEvaluateError);
|
||||
SetStepConclusion(step, TaskResult.Failed);
|
||||
ExecutionContext.Result = TaskResult.Failed;
|
||||
|
||||
if (emitCompositeMarkers)
|
||||
{
|
||||
stepStopwatch.Stop();
|
||||
ExecutionContext.Output($"##[end-action id={EscapeProperty(stepId)};outcome=failure;conclusion=failure;duration_ms={stepStopwatch.ElapsedMilliseconds}]");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Set the prefix for nested composites BEFORE running the step
|
||||
if (emitCompositeMarkers)
|
||||
{
|
||||
ExecutionContext.Global.Variables.Set("system.compositeMarkerIdPrefix", stepId);
|
||||
}
|
||||
|
||||
await RunStepAsync(step);
|
||||
|
||||
// Restore prefix for sibling steps (in case a nested composite changed it)
|
||||
if (emitCompositeMarkers)
|
||||
{
|
||||
ExecutionContext.Global.Variables.Set("system.compositeMarkerIdPrefix", idPrefix);
|
||||
}
|
||||
|
||||
if (emitCompositeMarkers)
|
||||
{
|
||||
stepStopwatch.Stop();
|
||||
// Outcome = raw result before continue-on-error (null when continue-on-error didn't fire)
|
||||
// Result = final result after continue-on-error
|
||||
var outcome = (step.ExecutionContext.Outcome ?? step.ExecutionContext.Result ?? TaskResult.Succeeded).ToActionResult().ToString().ToLowerInvariant();
|
||||
var conclusion = (step.ExecutionContext.Result ?? TaskResult.Succeeded).ToActionResult().ToString().ToLowerInvariant();
|
||||
ExecutionContext.Output($"##[end-action id={EscapeProperty(stepId)};outcome={outcome};conclusion={conclusion};duration_ms={stepStopwatch.ElapsedMilliseconds}]");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -470,5 +530,40 @@ namespace GitHub.Runner.Worker.Handlers
|
||||
step.ExecutionContext.Result = result;
|
||||
step.ExecutionContext.UpdateGlobalStepsContext();
|
||||
}
|
||||
|
||||
private static string EscapeProperty(string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value)) return value;
|
||||
return value
|
||||
.Replace("%", "%25")
|
||||
.Replace(";", "%3B")
|
||||
.Replace("\r", "%0D")
|
||||
.Replace("\n", "%0A")
|
||||
.Replace("]", "%5D");
|
||||
}
|
||||
|
||||
private const int MaxDisplayNameLength = 1000;
|
||||
|
||||
private static string SanitizeDisplayName(string displayName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(displayName)) return displayName;
|
||||
|
||||
// Take first line only (FormatStepName in ActionRunner.cs already does this
|
||||
// for most cases, but be defensive for any code path that skips it)
|
||||
var result = displayName.TrimStart(' ', '\t', '\r', '\n');
|
||||
var firstNewLine = result.IndexOfAny(new[] { '\r', '\n' });
|
||||
if (firstNewLine >= 0)
|
||||
{
|
||||
result = result.Substring(0, firstNewLine);
|
||||
}
|
||||
|
||||
// Truncate excessively long names
|
||||
if (result.Length > MaxDisplayNameLength)
|
||||
{
|
||||
result = result.Substring(0, MaxDisplayNameLength);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +90,14 @@ namespace GitHub.Runner.Worker.Handlers
|
||||
}
|
||||
}
|
||||
|
||||
// Strip runner-controlled markers from user output to prevent injection
|
||||
if (!String.IsNullOrEmpty(line) &&
|
||||
(line.Contains("##[start-action") || line.Contains("##[end-action")))
|
||||
{
|
||||
line = line.Replace("##[start-action", @"##[\start-action")
|
||||
.Replace("##[end-action", @"##[\end-action");
|
||||
}
|
||||
|
||||
// Problem matchers
|
||||
if (_matchers.Length > 0)
|
||||
{
|
||||
|
||||
297
src/Test/L0/Worker/Handlers/CompositeActionHandlerL0.cs
Normal file
297
src/Test/L0/Worker/Handlers/CompositeActionHandlerL0.cs
Normal file
@@ -0,0 +1,297 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using GitHub.DistributedTask.WebApi;
|
||||
using GitHub.Runner.Common.Util;
|
||||
using GitHub.Runner.Sdk;
|
||||
using GitHub.Runner.Worker;
|
||||
using GitHub.Runner.Worker.Handlers;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using DTWebApi = GitHub.DistributedTask.WebApi;
|
||||
|
||||
namespace GitHub.Runner.Common.Tests.Worker.Handlers
|
||||
{
|
||||
public sealed class CompositeActionHandlerL0
|
||||
{
|
||||
// Test EscapeProperty helper logic via reflection or by testing the markers output
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void EscapeProperty_EscapesSpecialCharacters()
|
||||
{
|
||||
// Test the escaping logic that would be applied
|
||||
var input = "value;with%special\r\n]chars";
|
||||
var escaped = EscapeProperty(input);
|
||||
Assert.Equal("value%3Bwith%25special%0D%0A%5Dchars", escaped);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void EscapeProperty_HandlesNullAndEmpty()
|
||||
{
|
||||
Assert.Null(EscapeProperty(null));
|
||||
Assert.Equal("", EscapeProperty(""));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void SanitizeDisplayName_TruncatesLongNames()
|
||||
{
|
||||
var longName = new string('a', 1500);
|
||||
var sanitized = SanitizeDisplayName(longName);
|
||||
Assert.Equal(1000, sanitized.Length);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void SanitizeDisplayName_TakesFirstLineOnly()
|
||||
{
|
||||
var multiline = "First line\nSecond line\nThird line";
|
||||
var sanitized = SanitizeDisplayName(multiline);
|
||||
Assert.Equal("First line", sanitized);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void SanitizeDisplayName_TrimsLeadingWhitespace()
|
||||
{
|
||||
var withLeading = " \n \t Actual name\nSecond line";
|
||||
var sanitized = SanitizeDisplayName(withLeading);
|
||||
Assert.Equal("Actual name", sanitized);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void SanitizeDisplayName_HandlesCarriageReturn()
|
||||
{
|
||||
var withCR = "First line\r\nSecond line";
|
||||
var sanitized = SanitizeDisplayName(withCR);
|
||||
Assert.Equal("First line", sanitized);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void SanitizeDisplayName_HandlesNullAndEmpty()
|
||||
{
|
||||
Assert.Null(SanitizeDisplayName(null));
|
||||
Assert.Equal("", SanitizeDisplayName(""));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void EmitMarkers_DisplayNameEscaping()
|
||||
{
|
||||
// Verify that special characters in display names get escaped properly
|
||||
var displayName = "Step with semicolons; and more; here";
|
||||
var escaped = EscapeProperty(SanitizeDisplayName(displayName));
|
||||
Assert.Equal("Step with semicolons%3B and more%3B here", escaped);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void EmitMarkers_DisplayNameWithBrackets()
|
||||
{
|
||||
var displayName = "Step with [brackets] inside";
|
||||
var escaped = EscapeProperty(SanitizeDisplayName(displayName));
|
||||
Assert.Equal("Step with [brackets%5D inside", escaped);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void StripUserEmittedMarkers_StartAction()
|
||||
{
|
||||
// Simulate what OutputManager does to strip markers
|
||||
var userLine = "##[start-action display=Fake;id=fake]";
|
||||
var stripped = StripMarkers(userLine);
|
||||
Assert.Equal(@"##[\start-action display=Fake;id=fake]", stripped);
|
||||
Assert.DoesNotContain("##[start-action", stripped);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void StripUserEmittedMarkers_EndAction()
|
||||
{
|
||||
var userLine = "##[end-action id=fake;outcome=success;conclusion=success;duration_ms=100]";
|
||||
var stripped = StripMarkers(userLine);
|
||||
Assert.Equal(@"##[\end-action id=fake;outcome=success;conclusion=success;duration_ms=100]", stripped);
|
||||
Assert.DoesNotContain("##[end-action", stripped);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void StripUserEmittedMarkers_PreservesOtherCommands()
|
||||
{
|
||||
var userLine = "##[group]My Group";
|
||||
var stripped = StripMarkers(userLine);
|
||||
Assert.Equal("##[group]My Group", stripped);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void StripUserEmittedMarkers_HandlesEmbeddedMarkers()
|
||||
{
|
||||
var userLine = "Some text ##[start-action display=fake;id=fake] more text";
|
||||
var stripped = StripMarkers(userLine);
|
||||
Assert.Equal(@"Some text ##[\start-action display=fake;id=fake] more text", stripped);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void TaskResultToActionResult_Success()
|
||||
{
|
||||
var result = GitHub.DistributedTask.WebApi.TaskResult.Succeeded;
|
||||
var actionResult = result.ToActionResult();
|
||||
Assert.Equal(ActionResult.Success, actionResult);
|
||||
Assert.Equal("success", actionResult.ToString().ToLowerInvariant());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void TaskResultToActionResult_Failure()
|
||||
{
|
||||
var result = GitHub.DistributedTask.WebApi.TaskResult.Failed;
|
||||
var actionResult = result.ToActionResult();
|
||||
Assert.Equal(ActionResult.Failure, actionResult);
|
||||
Assert.Equal("failure", actionResult.ToString().ToLowerInvariant());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void TaskResultToActionResult_Cancelled()
|
||||
{
|
||||
var result = GitHub.DistributedTask.WebApi.TaskResult.Canceled;
|
||||
var actionResult = result.ToActionResult();
|
||||
Assert.Equal(ActionResult.Cancelled, actionResult);
|
||||
Assert.Equal("cancelled", actionResult.ToString().ToLowerInvariant());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void TaskResultToActionResult_Skipped()
|
||||
{
|
||||
var result = GitHub.DistributedTask.WebApi.TaskResult.Skipped;
|
||||
var actionResult = result.ToActionResult();
|
||||
Assert.Equal(ActionResult.Skipped, actionResult);
|
||||
Assert.Equal("skipped", actionResult.ToString().ToLowerInvariant());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void MarkerFormat_StartAction()
|
||||
{
|
||||
var display = "My Step";
|
||||
var id = "my-step";
|
||||
var marker = $"##[start-action display={EscapeProperty(display)};id={EscapeProperty(id)}]";
|
||||
Assert.Equal("##[start-action display=My Step;id=my-step]", marker);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void MarkerFormat_EndAction()
|
||||
{
|
||||
var id = "my-step";
|
||||
var outcome = "success";
|
||||
var conclusion = "success";
|
||||
var durationMs = 1234;
|
||||
var marker = $"##[end-action id={EscapeProperty(id)};outcome={outcome};conclusion={conclusion};duration_ms={durationMs}]";
|
||||
Assert.Equal("##[end-action id=my-step;outcome=success;conclusion=success;duration_ms=1234]", marker);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void MarkerFormat_NestedId()
|
||||
{
|
||||
var prefix = "outer-composite";
|
||||
var contextName = "inner-step";
|
||||
var stepId = $"{prefix}.{contextName}";
|
||||
Assert.Equal("outer-composite.inner-step", stepId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void MarkerFormat_SkippedStep()
|
||||
{
|
||||
var id = "skipped-step";
|
||||
var marker = $"##[end-action id={EscapeProperty(id)};outcome=skipped;conclusion=skipped;duration_ms=0]";
|
||||
Assert.Equal("##[end-action id=skipped-step;outcome=skipped;conclusion=skipped;duration_ms=0]", marker);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Level", "L0")]
|
||||
[Trait("Category", "Worker")]
|
||||
public void MarkerFormat_ContinueOnError()
|
||||
{
|
||||
// When continue-on-error is true and step fails:
|
||||
// outcome = failure (raw result)
|
||||
// conclusion = success (after continue-on-error applied)
|
||||
var id = "failing-step";
|
||||
var marker = $"##[end-action id={EscapeProperty(id)};outcome=failure;conclusion=success;duration_ms=500]";
|
||||
Assert.Equal("##[end-action id=failing-step;outcome=failure;conclusion=success;duration_ms=500]", marker);
|
||||
}
|
||||
|
||||
// Helper methods that mirror the implementation
|
||||
private static string EscapeProperty(string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value)) return value;
|
||||
return value
|
||||
.Replace("%", "%25")
|
||||
.Replace(";", "%3B")
|
||||
.Replace("\r", "%0D")
|
||||
.Replace("\n", "%0A")
|
||||
.Replace("]", "%5D");
|
||||
}
|
||||
|
||||
private const int MaxDisplayNameLength = 1000;
|
||||
|
||||
private static string SanitizeDisplayName(string displayName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(displayName)) return displayName;
|
||||
|
||||
var result = displayName.TrimStart(' ', '\t', '\r', '\n');
|
||||
var firstNewLine = result.IndexOfAny(new[] { '\r', '\n' });
|
||||
if (firstNewLine >= 0)
|
||||
{
|
||||
result = result.Substring(0, firstNewLine);
|
||||
}
|
||||
|
||||
if (result.Length > MaxDisplayNameLength)
|
||||
{
|
||||
result = result.Substring(0, MaxDisplayNameLength);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string StripMarkers(string line)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(line) &&
|
||||
(line.Contains("##[start-action") || line.Contains("##[end-action")))
|
||||
{
|
||||
line = line.Replace("##[start-action", @"##[\start-action")
|
||||
.Replace("##[end-action", @"##[\end-action");
|
||||
}
|
||||
return line;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user