Skip to content

Commit 0d6d2bb

Browse files
baywetCopilot
andauthored
.NET: fix: declarative workflow http variables (#8797)
* fix(dotnet): reject protected values in HTTP requests Co-authored-by: Copilot <[email protected]> * fix(dotnet): validate declarative HTTP methods Co-authored-by: Copilot <[email protected]> Copilot-Session: c37927da-21e9-4dc8-923b-7613ae0f4d9b --------- Co-authored-by: Copilot <[email protected]> Copilot-Session: c37927da-21e9-4dc8-923b-7613ae0f4d9b
1 parent 2024d4d commit 0d6d2bb

2 files changed

Lines changed: 119 additions & 9 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/HttpRequestExecutor.cs‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Agents.AI.Workflows.Declarative.Interpreter;
1010
using Microsoft.Agents.AI.Workflows.Declarative.PowerFx;
1111
using Microsoft.Agents.ObjectModel;
12+
using Microsoft.Agents.ObjectModel.Abstractions;
1213
using Microsoft.Extensions.AI;
1314
using Microsoft.PowerFx.Types;
1415
using Microsoft.Shared.Diagnostics;
@@ -195,15 +196,19 @@ private string GetMethod()
195196
return "GET";
196197
}
197198

198-
HttpMethodTypeWrapper wrapper = this.Evaluator.GetValue(methodExpression).Value;
199+
HttpMethodTypeWrapper wrapper = this.GetRequestValue(this.Evaluator.GetValue(methodExpression), "method");
199200
return !string.IsNullOrEmpty(wrapper.UnknownValue) ? wrapper.UnknownValue! : wrapper.Value.ToString().ToUpperInvariant();
200201
}
201202

202-
private string GetUrl() =>
203-
this.Evaluator.GetValue(
203+
private string GetUrl()
204+
{
205+
EvaluationResult<string> result = this.Evaluator.GetValue(
204206
Throw.IfNull(
205207
this.Model.Url,
206-
$"{nameof(this.Model)}.{nameof(this.Model.Url)}")).Value;
208+
$"{nameof(this.Model)}.{nameof(this.Model.Url)}"));
209+
210+
return this.GetRequestValue(result, "URL");
211+
}
207212

208213
private Dictionary<string, string>? GetHeaders()
209214
{
@@ -215,7 +220,7 @@ private string GetUrl() =>
215220
Dictionary<string, string> result = new(StringComparer.OrdinalIgnoreCase);
216221
foreach (KeyValuePair<string, StringExpression> header in this.Model.Headers)
217222
{
218-
string value = this.Evaluator.GetValue(header.Value).Value;
223+
string value = this.GetRequestValue(this.Evaluator.GetValue(header.Value), $"header '{header.Key}'");
219224
if (!string.IsNullOrEmpty(value))
220225
{
221226
result[header.Key] = value;
@@ -235,7 +240,7 @@ private string GetUrl() =>
235240

236241
case JsonRequestContent jsonContent when jsonContent.Content is not null:
237242
{
238-
FormulaValue formula = this.Evaluator.GetValue(jsonContent.Content).Value.ToFormula();
243+
FormulaValue formula = this.GetRequestValue(this.Evaluator.GetValue(jsonContent.Content), "JSON body").ToFormula();
239244
string json = formula.ToJson().ToJsonString();
240245
return (json, "application/json");
241246
}
@@ -244,11 +249,11 @@ private string GetUrl() =>
244249
{
245250
string? content = rawContent.Content is null
246251
? null
247-
: this.Evaluator.GetValue(rawContent.Content).Value;
252+
: this.GetRequestValue(this.Evaluator.GetValue(rawContent.Content), "raw body");
248253

249254
string? contentType = rawContent.ContentType is null
250255
? null
251-
: this.Evaluator.GetValue(rawContent.ContentType).Value;
256+
: this.GetRequestValue(this.Evaluator.GetValue(rawContent.ContentType), "content type");
252257

253258
return (content, string.IsNullOrEmpty(contentType) ? null : contentType);
254259
}
@@ -284,7 +289,9 @@ private string GetUrl() =>
284289
continue;
285290
}
286291

287-
object? rawValue = this.Evaluator.GetValue(parameter.Value).Value.ToObject();
292+
object? rawValue = this.GetRequestValue(
293+
this.Evaluator.GetValue(parameter.Value),
294+
$"query parameter '{parameter.Key}'").ToObject();
288295
string? formatted = FormatQueryValue(rawValue);
289296
if (formatted is not null)
290297
{
@@ -305,6 +312,16 @@ private string GetUrl() =>
305312
_ => value.ToString(),
306313
};
307314

315+
private T GetRequestValue<T>(EvaluationResult<T> result, string location)
316+
{
317+
if (result.Sensitivity == SensitivityLevel.Sensitive)
318+
{
319+
throw this.Exception($"Cannot use a protected value in HTTP request {location}.");
320+
}
321+
322+
return result.Value;
323+
}
324+
308325
private string? GetConversationId()
309326
{
310327
if (this.Model.ConversationId is null)

‎dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/ObjectModel/HttpRequestExecutorTest.cs‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,36 @@ public async Task HttpRequestForwardsQueryParametersAsync()
410410
info.QueryParameters["includeDeleted"] == "false");
411411
}
412412

413+
[Theory]
414+
[InlineData(RequestValueLocation.Method)]
415+
[InlineData(RequestValueLocation.Url)]
416+
[InlineData(RequestValueLocation.Header)]
417+
[InlineData(RequestValueLocation.QueryParameter)]
418+
[InlineData(RequestValueLocation.JsonBody)]
419+
[InlineData(RequestValueLocation.RawBody)]
420+
[InlineData(RequestValueLocation.ContentType)]
421+
public async Task HttpRequestWithProtectedEnvironmentValueThrowsAsync(RequestValueLocation location)
422+
{
423+
// Arrange
424+
this.State.InitializeSystem();
425+
this.State.Set(
426+
"PROTECTED_SETTING",
427+
FormulaValue.New("protected-value"),
428+
VariableScopeNames.Environment,
429+
SensitivityLevel.Sensitive);
430+
431+
HttpRequestAction model = this.CreateModelWithEnvironmentExpression(location);
432+
MockHttpRequestHandler handler = new(HttpRequestResult("{}"));
433+
HttpRequestExecutor action = new(model, handler.Object, this._agentProvider.Object, this.State);
434+
435+
// Act
436+
Task ExecuteAsync() => this.ExecuteAsync(action);
437+
438+
// Assert
439+
await Assert.ThrowsAsync<DeclarativeActionException>(ExecuteAsync);
440+
handler.VerifyNotSent();
441+
}
442+
413443
[Fact]
414444
public async Task HttpRequestAddsResponseToConversationAsync()
415445
{
@@ -732,6 +762,67 @@ private HttpRequestAction CreateModel(
732762
return AssignParent<HttpRequestAction>(builder);
733763
}
734764

765+
private HttpRequestAction CreateModelWithEnvironmentExpression(RequestValueLocation location)
766+
{
767+
const string EnvironmentExpression = "Env.PROTECTED_SETTING";
768+
769+
HttpRequestAction.Builder builder = new()
770+
{
771+
Id = this.CreateActionId(),
772+
DisplayName = this.FormatDisplayName($"{nameof(HttpRequestWithProtectedEnvironmentValueThrowsAsync)}_{location}"),
773+
Url = new StringExpression.Builder(
774+
location == RequestValueLocation.Url
775+
? StringExpression.Expression(EnvironmentExpression)
776+
: StringExpression.Literal(TestUrl)),
777+
Method = new EnumExpression<HttpMethodTypeWrapper>.Builder(
778+
location == RequestValueLocation.Method
779+
? EnumExpression<HttpMethodTypeWrapper>.Expression(EnvironmentExpression)
780+
: EnumExpression<HttpMethodTypeWrapper>.Literal(HttpMethodTypeWrapper.Get(HttpMethodType.Post))),
781+
};
782+
783+
switch (location)
784+
{
785+
case RequestValueLocation.Header:
786+
builder.Headers.Add("X-Value", new StringExpression.Builder(StringExpression.Expression(EnvironmentExpression)));
787+
break;
788+
case RequestValueLocation.QueryParameter:
789+
builder.QueryParameters.Add("value", new ValueExpression.Builder(ValueExpression.Expression(EnvironmentExpression)));
790+
break;
791+
case RequestValueLocation.JsonBody:
792+
builder.Body = new JsonRequestContent.Builder
793+
{
794+
Content = new ValueExpression.Builder(ValueExpression.Expression(EnvironmentExpression)),
795+
};
796+
break;
797+
case RequestValueLocation.RawBody:
798+
builder.Body = new RawRequestContent.Builder
799+
{
800+
Content = new StringExpression.Builder(StringExpression.Expression(EnvironmentExpression)),
801+
};
802+
break;
803+
case RequestValueLocation.ContentType:
804+
builder.Body = new RawRequestContent.Builder
805+
{
806+
Content = new StringExpression.Builder(StringExpression.Literal("body")),
807+
ContentType = new StringExpression.Builder(StringExpression.Expression(EnvironmentExpression)),
808+
};
809+
break;
810+
}
811+
812+
return AssignParent<HttpRequestAction>(builder);
813+
}
814+
815+
public enum RequestValueLocation
816+
{
817+
Method,
818+
Url,
819+
Header,
820+
QueryParameter,
821+
JsonBody,
822+
RawBody,
823+
ContentType,
824+
}
825+
735826
private sealed class MockHttpRequestHandler : Mock<IHttpRequestHandler>
736827
{
737828
private HttpRequestInfo? _lastRequest;
@@ -755,5 +846,7 @@ public void VerifySent(Func<HttpRequestInfo, bool> predicate)
755846
Assert.NotNull(this._lastRequest);
756847
Assert.True(predicate(this._lastRequest!), "Sent HTTP request did not match expected predicate.");
757848
}
849+
850+
public void VerifyNotSent() => Assert.Null(this._lastRequest);
758851
}
759852
}

0 commit comments

Comments
 (0)