507 lines
20 KiB
C#
507 lines
20 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using D3ROVoice.Api.Controllers;
|
|
using D3ROVoice.Api.Data;
|
|
using D3ROVoice.Api.Dtos;
|
|
using D3ROVoice.Api.Services;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Xunit;
|
|
|
|
namespace D3ROVoice.Api.Tests;
|
|
|
|
public class EndpointSecurityAndSttProxyTests
|
|
{
|
|
private static readonly byte[] SampleWav = BuildTinyWavBytes();
|
|
|
|
[Theory]
|
|
[InlineData("https://169.254.169.254/latest/meta-data", "OpenAI")]
|
|
[InlineData("https://10.0.0.1/v1/chat/completions", "OpenAI")]
|
|
[InlineData("https://172.16.0.10/v1/chat/completions", "OpenAI")]
|
|
[InlineData("https://192.168.1.50/v1/chat/completions", "OpenAI")]
|
|
[InlineData("https://100.64.0.5/v1/chat/completions", "OpenAI")]
|
|
[InlineData("https://[::1]/v1/chat/completions", "OpenAI")]
|
|
[InlineData("https://[fe80::1]/v1/chat/completions", "OpenAI")]
|
|
[InlineData("https://metadata.google.internal/computeMetadata/v1", "OpenAI")]
|
|
[InlineData("http://localhost:11434/api/generate", "OpenAI")]
|
|
public async Task CreateEndpoint_내부SSRF및허용되지않은루프백URL_BadRequest로차단한다(string url, string provider)
|
|
{
|
|
await using var harness = await ApiHarness.CreateAsync();
|
|
|
|
var result = await harness.AdminController.CreateEndpoint(new CreateModelEndpointDto(
|
|
ModelId: "test-model",
|
|
ModelName: "Test Model",
|
|
Provider: provider,
|
|
EndpointUrl: url,
|
|
ApiKey: "sk-test",
|
|
CostPer1kPromptTokens: 0.001m,
|
|
CostPer1kCompletionTokens: 0.002m,
|
|
Memo: "security-audit-test"));
|
|
|
|
Assert.IsType<BadRequestObjectResult>(result);
|
|
Assert.Empty(await harness.Db.ModelEndpoints.ToListAsync());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateEndpoint_URL자격증명포함시_BadRequest로차단한다()
|
|
{
|
|
await using var harness = await ApiHarness.CreateAsync();
|
|
var urlWithUserInfo = new UriBuilder("https", "api.openai.com")
|
|
{
|
|
UserName = "user",
|
|
Password = "pass",
|
|
Path = "/v1/chat/completions",
|
|
}.Uri.AbsoluteUri;
|
|
|
|
var result = await harness.AdminController.CreateEndpoint(new CreateModelEndpointDto(
|
|
ModelId: "test-model",
|
|
ModelName: "Test Model",
|
|
Provider: "OpenAI",
|
|
EndpointUrl: urlWithUserInfo,
|
|
ApiKey: "sk-test",
|
|
CostPer1kPromptTokens: 0.001m,
|
|
CostPer1kCompletionTokens: 0.002m,
|
|
Memo: "security-audit-test"));
|
|
|
|
Assert.IsType<BadRequestObjectResult>(result);
|
|
Assert.Empty(await harness.Db.ModelEndpoints.ToListAsync());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateEndpoint_로컬Ollama와공개HTTPS엔드포인트_정상생성한다()
|
|
{
|
|
await using var harness = await ApiHarness.CreateAsync();
|
|
|
|
var ollamaResult = await harness.AdminController.CreateEndpoint(new CreateModelEndpointDto(
|
|
ModelId: "ollama-gemma",
|
|
ModelName: "Ollama Local",
|
|
Provider: "Ollama Local",
|
|
EndpointUrl: "http://localhost:11434/api/generate",
|
|
ApiKey: "",
|
|
CostPer1kPromptTokens: 0m,
|
|
CostPer1kCompletionTokens: 0m,
|
|
Memo: "security-audit-test"));
|
|
|
|
Assert.IsType<OkObjectResult>(ollamaResult);
|
|
|
|
harness.SetNewIdempotencyKey();
|
|
var publicResult = await harness.AdminController.CreateEndpoint(new CreateModelEndpointDto(
|
|
ModelId: "openai-gpt",
|
|
ModelName: "GPT",
|
|
Provider: "OpenAI",
|
|
EndpointUrl: "https://api.openai.com/v1/chat/completions",
|
|
ApiKey: "sk-live",
|
|
CostPer1kPromptTokens: 0.001m,
|
|
CostPer1kCompletionTokens: 0.002m,
|
|
Memo: "security-audit-test"));
|
|
|
|
Assert.IsType<OkObjectResult>(publicResult);
|
|
Assert.Equal(2, await harness.Db.ModelEndpoints.CountAsync());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateSttEndpoint_내부SSRF주소와NaN온도및원격루프백_BadRequest로차단한다()
|
|
{
|
|
await using var harness = await ApiHarness.CreateAsync();
|
|
|
|
var metadataBlocked = await harness.AdminController.CreateSttEndpoint(new CreateSttEndpointDto(
|
|
Name: "Metadata",
|
|
ProviderType: "groq",
|
|
EndpointUrl: "https://169.254.169.254/latest/meta-data",
|
|
ApiKey: "k",
|
|
ModelId: "whisper-large-v3",
|
|
Method: "multipart",
|
|
Language: "ko",
|
|
Prompt: null,
|
|
Temperature: 0.0,
|
|
CostPerMinute: 0m,
|
|
CostPerSecond: 0m,
|
|
IsDefault: false,
|
|
IsActive: true,
|
|
FallbackPriority: 1,
|
|
ExtraHeadersJson: null,
|
|
Memo: "security-audit-test"));
|
|
Assert.IsType<BadRequestObjectResult>(metadataBlocked);
|
|
|
|
harness.SetNewIdempotencyKey();
|
|
var remoteLoopbackBlocked = await harness.AdminController.CreateSttEndpoint(new CreateSttEndpointDto(
|
|
Name: "RemoteLoopback",
|
|
ProviderType: "groq",
|
|
EndpointUrl: "http://127.0.0.1:8971/stt/transcribe",
|
|
ApiKey: "k",
|
|
ModelId: "whisper-large-v3",
|
|
Method: "multipart",
|
|
Language: "ko",
|
|
Prompt: null,
|
|
Temperature: 0.0,
|
|
CostPerMinute: 0m,
|
|
CostPerSecond: 0m,
|
|
IsDefault: false,
|
|
IsActive: true,
|
|
FallbackPriority: 1,
|
|
ExtraHeadersJson: null,
|
|
Memo: "security-audit-test"));
|
|
Assert.IsType<BadRequestObjectResult>(remoteLoopbackBlocked);
|
|
|
|
harness.SetNewIdempotencyKey();
|
|
var nanTempBlocked = await harness.AdminController.CreateSttEndpoint(new CreateSttEndpointDto(
|
|
Name: "BadTemp",
|
|
ProviderType: "groq",
|
|
EndpointUrl: "https://api.groq.com/openai/v1/audio/transcriptions",
|
|
ApiKey: "k",
|
|
ModelId: "whisper-large-v3",
|
|
Method: "multipart",
|
|
Language: "ko",
|
|
Prompt: null,
|
|
Temperature: double.NaN,
|
|
CostPerMinute: 0m,
|
|
CostPerSecond: 0m,
|
|
IsDefault: false,
|
|
IsActive: true,
|
|
FallbackPriority: 1,
|
|
ExtraHeadersJson: null,
|
|
Memo: "security-audit-test"));
|
|
Assert.IsType<BadRequestObjectResult>(nanTempBlocked);
|
|
|
|
harness.SetNewIdempotencyKey();
|
|
var localSidecarOk = await harness.AdminController.CreateSttEndpoint(new CreateSttEndpointDto(
|
|
Name: "Sidecar",
|
|
ProviderType: "local-sidecar",
|
|
EndpointUrl: "http://localhost:8971/stt/transcribe",
|
|
ApiKey: "",
|
|
ModelId: "whisper-large-v3-turbo",
|
|
Method: "multipart",
|
|
Language: "ko",
|
|
Prompt: null,
|
|
Temperature: 0.0,
|
|
CostPerMinute: 0m,
|
|
CostPerSecond: 0m,
|
|
IsDefault: false,
|
|
IsActive: true,
|
|
FallbackPriority: 2,
|
|
ExtraHeadersJson: null,
|
|
Memo: "security-audit-test"));
|
|
Assert.IsType<OkObjectResult>(localSidecarOk);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("https://169.254.169.254/latest/meta-data")]
|
|
[InlineData("http://127.0.0.1:8971/stt/transcribe")]
|
|
[InlineData("https://10.0.0.5/stt")]
|
|
public async Task TestSttDirect_내부및루프백URL_요청없이BadRequest로차단한다(string url)
|
|
{
|
|
await using var harness = await ApiHarness.CreateAsync();
|
|
|
|
var result = await harness.AdminController.TestSttDirect(new DirectSttTestDto(url, "sk-test"));
|
|
|
|
Assert.IsType<BadRequestObjectResult>(result);
|
|
Assert.Empty(harness.CapturedRequests);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TranscribeAsync_악의적언어문자열_쿼리및프롬프트인젝션을차단한다()
|
|
{
|
|
await using var harness = await ApiHarness.CreateAsync();
|
|
harness.Db.SttProviderEndpoints.Add(new SttProviderEndpoint
|
|
{
|
|
Name = "Deepgram",
|
|
ProviderType = "deepgram",
|
|
EndpointUrl = "https://api.deepgram.com/v1/listen",
|
|
ApiKey = "dg-secret",
|
|
ModelId = "nova-3",
|
|
Method = "binary-stream",
|
|
Language = "ko",
|
|
IsDefault = true,
|
|
IsActive = true,
|
|
FallbackPriority = 1,
|
|
});
|
|
await harness.Db.SaveChangesAsync();
|
|
|
|
await Assert.ThrowsAsync<ArgumentException>(() =>
|
|
harness.SttService.TranscribeAsync(
|
|
1,
|
|
"user@d3ro.test",
|
|
new SttTranscribeRequest(Language: "ko&callback=https://attacker.test/hook"),
|
|
SampleWav,
|
|
"audio/wav",
|
|
"clip.wav",
|
|
recordUsage: false));
|
|
|
|
Assert.Empty(harness.CapturedRequests);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TranscribeAsync_특수문자포함모델ID_Deepgram쿼리를URL인코딩한다()
|
|
{
|
|
await using var harness = await ApiHarness.CreateAsync(_ => new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent("""{"results":{"channels":[{"alternatives":[{"transcript":"hello","confidence":0.99}]}]},"metadata":{"duration":1.2}}""", Encoding.UTF8, "application/json")
|
|
});
|
|
|
|
harness.Db.SttProviderEndpoints.Add(new SttProviderEndpoint
|
|
{
|
|
Name = "Deepgram",
|
|
ProviderType = "deepgram",
|
|
EndpointUrl = "https://api.deepgram.com/v1/listen",
|
|
ApiKey = "dg-secret",
|
|
ModelId = "nova-3&tier=enterprise",
|
|
Method = "binary-stream",
|
|
Language = "ko",
|
|
IsDefault = true,
|
|
IsActive = true,
|
|
FallbackPriority = 1,
|
|
});
|
|
await harness.Db.SaveChangesAsync();
|
|
|
|
var res = await harness.SttService.TranscribeAsync(
|
|
1,
|
|
"user@d3ro.test",
|
|
new SttTranscribeRequest(Language: "ko-KR"),
|
|
SampleWav,
|
|
"audio/wav",
|
|
"clip.wav",
|
|
recordUsage: false);
|
|
|
|
Assert.Equal("hello", res.Text);
|
|
var captured = Assert.Single(harness.CapturedRequests);
|
|
Assert.Contains("model=nova-3%26tier%3Denterprise", captured.RequestUri!.Query, StringComparison.Ordinal);
|
|
Assert.DoesNotContain("&tier=enterprise", captured.RequestUri!.Query, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("openai", "https://api.openai.com/v1/audio/transcriptions", """{"text":"안녕","duration":1.0,"language":"ko"}""")]
|
|
[InlineData("deepgram", "https://api.deepgram.com/v1/listen", """{"results":{"channels":[{"alternatives":[{"transcript":"안녕","confidence":0.95}]}]},"metadata":{"duration":1.0}}""")]
|
|
[InlineData("google", "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent", """{"candidates":[{"content":{"parts":[{"text":"안녕"}]}}]}""")]
|
|
[InlineData("azure", "https://koreacentral.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1", """{"DisplayText":"안녕"}""")]
|
|
public async Task TranscribeAsync_앞뒤공백및개행포함ApiKey_Trim후정상전송한다(string providerType, string url, string jsonBody)
|
|
{
|
|
await using var harness = await ApiHarness.CreateAsync(_ => new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent(jsonBody, Encoding.UTF8, "application/json")
|
|
});
|
|
|
|
harness.Db.SttProviderEndpoints.Add(new SttProviderEndpoint
|
|
{
|
|
Name = providerType,
|
|
ProviderType = providerType,
|
|
EndpointUrl = url,
|
|
ApiKey = " secret-token-with-whitespace \r\n",
|
|
ModelId = "model-1",
|
|
Method = "multipart",
|
|
Language = "ko",
|
|
IsDefault = true,
|
|
IsActive = true,
|
|
FallbackPriority = 1,
|
|
});
|
|
await harness.Db.SaveChangesAsync();
|
|
|
|
var res = await harness.SttService.TranscribeAsync(
|
|
1,
|
|
"user@d3ro.test",
|
|
new SttTranscribeRequest(Language: "ko"),
|
|
SampleWav,
|
|
"audio/wav",
|
|
"clip.wav",
|
|
recordUsage: false);
|
|
|
|
Assert.Equal("안녕", res.Text);
|
|
Assert.Single(harness.CapturedRequests);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TranscribeAsync_AssemblyAI비정상폴링ID_경로탐색요청없이실패폐쇄한다()
|
|
{
|
|
await using var harness = await ApiHarness.CreateAsync(req =>
|
|
{
|
|
var path = req.RequestUri!.AbsolutePath;
|
|
if (path == "/v2/upload")
|
|
{
|
|
return new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent("""{"upload_url":"https://cdn.assemblyai.com/upload/abc"}""", Encoding.UTF8, "application/json")
|
|
};
|
|
}
|
|
if (path == "/v2/transcript")
|
|
{
|
|
return new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent("""{"id":"../../secret/leak?x=1"}""", Encoding.UTF8, "application/json")
|
|
};
|
|
}
|
|
return new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent("""{"status":"completed","text":"bad"}""", Encoding.UTF8, "application/json")
|
|
};
|
|
});
|
|
|
|
harness.Db.SttProviderEndpoints.Add(new SttProviderEndpoint
|
|
{
|
|
Name = "AssemblyAI",
|
|
ProviderType = "assemblyai",
|
|
EndpointUrl = "https://api.assemblyai.com/v2/transcript",
|
|
ApiKey = "aai-secret",
|
|
ModelId = "best",
|
|
Method = "async-poll",
|
|
Language = "ko",
|
|
IsDefault = true,
|
|
IsActive = true,
|
|
FallbackPriority = 1,
|
|
});
|
|
await harness.Db.SaveChangesAsync();
|
|
|
|
await Assert.ThrowsAsync<SttProviderUnavailableException>(() =>
|
|
harness.SttService.TranscribeAsync(
|
|
1,
|
|
"user@d3ro.test",
|
|
new SttTranscribeRequest(Language: "ko"),
|
|
SampleWav,
|
|
"audio/wav",
|
|
"clip.wav",
|
|
recordUsage: false));
|
|
|
|
Assert.DoesNotContain(harness.CapturedRequests, r => r.RequestUri!.AbsoluteUri.Contains("secret", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LlmProxyService_DB내부SSRF엔드포인트_네트워크요청없이안전폴백으로실패폐쇄한다()
|
|
{
|
|
await using var harness = await ApiHarness.CreateAsync();
|
|
harness.Db.ModelEndpoints.Add(new ServiceModelEndpoint
|
|
{
|
|
ModelId = "ssrf-llm",
|
|
ModelName = "Evil",
|
|
Provider = "OpenAI",
|
|
EndpointUrl = "https://169.254.169.254/latest/meta-data",
|
|
ApiKey = "sk-should-not-leak",
|
|
CostPer1kPromptTokens = 0.001m,
|
|
CostPer1kCompletionTokens = 0.002m,
|
|
IsActive = true,
|
|
});
|
|
await harness.Db.SaveChangesAsync();
|
|
|
|
var res = await harness.LlmService.GenerateAsync(
|
|
1,
|
|
"user@d3ro.test",
|
|
new LlmGenerateRequest("Hello", Model: "ssrf-llm"));
|
|
|
|
Assert.Empty(harness.CapturedRequests);
|
|
Assert.Contains("Fallback", res.Text, StringComparison.Ordinal);
|
|
}
|
|
|
|
private static byte[] BuildTinyWavBytes()
|
|
{
|
|
var wav = new byte[64];
|
|
Encoding.ASCII.GetBytes("RIFF").CopyTo(wav, 0);
|
|
Encoding.ASCII.GetBytes("WAVE").CopyTo(wav, 8);
|
|
return wav;
|
|
}
|
|
|
|
private sealed class ApiHarness : IAsyncDisposable
|
|
{
|
|
private readonly SqliteConnection _connection;
|
|
|
|
public AppDbContext Db { get; }
|
|
public AdminController AdminController { get; }
|
|
public SttProxyService SttService { get; }
|
|
public LlmProxyService LlmService { get; }
|
|
public List<HttpRequestMessage> CapturedRequests { get; }
|
|
|
|
private ApiHarness(
|
|
SqliteConnection connection,
|
|
AppDbContext db,
|
|
AdminController adminController,
|
|
SttProxyService sttService,
|
|
LlmProxyService llmService,
|
|
List<HttpRequestMessage> capturedRequests)
|
|
{
|
|
_connection = connection;
|
|
Db = db;
|
|
AdminController = adminController;
|
|
SttService = sttService;
|
|
LlmService = llmService;
|
|
CapturedRequests = capturedRequests;
|
|
}
|
|
|
|
public void SetNewIdempotencyKey()
|
|
{
|
|
AdminController.ControllerContext.HttpContext.Request.Headers["Idempotency-Key"] = Guid.NewGuid().ToString("D");
|
|
}
|
|
|
|
public static async Task<ApiHarness> CreateAsync(Func<HttpRequestMessage, HttpResponseMessage>? responder = null)
|
|
{
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
await connection.OpenAsync();
|
|
var options = new DbContextOptionsBuilder<AppDbContext>()
|
|
.UseSqlite(connection)
|
|
.Options;
|
|
var db = new AppDbContext(options);
|
|
await db.Database.EnsureCreatedAsync();
|
|
|
|
var captured = new List<HttpRequestMessage>();
|
|
var handler = new CaptureHandler(captured, responder);
|
|
var httpFactory = new FixedHttpClientFactory(handler);
|
|
|
|
var sttService = new SttProxyService(db, httpFactory, NullLogger<SttProxyService>.Instance);
|
|
var llmService = new LlmProxyService(db, httpFactory, NullLogger<LlmProxyService>.Instance);
|
|
var adminOps = new AdminOperationService(db);
|
|
var controller = new AdminController(db, sttService, adminOps);
|
|
|
|
var httpContext = new DefaultHttpContext
|
|
{
|
|
User = new ClaimsPrincipal(new ClaimsIdentity(new[]
|
|
{
|
|
new Claim(ClaimTypes.Email, "admin@d3ro.test"),
|
|
new Claim(ClaimTypes.Role, "SuperAdmin"),
|
|
}, "TestAuth")),
|
|
};
|
|
httpContext.Request.Headers["Idempotency-Key"] = Guid.NewGuid().ToString("D");
|
|
controller.ControllerContext = new ControllerContext { HttpContext = httpContext };
|
|
|
|
return new ApiHarness(connection, db, controller, sttService, llmService, captured);
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await Db.DisposeAsync();
|
|
await _connection.DisposeAsync();
|
|
}
|
|
}
|
|
|
|
private sealed class FixedHttpClientFactory : IHttpClientFactory
|
|
{
|
|
private readonly HttpMessageHandler _handler;
|
|
public FixedHttpClientFactory(HttpMessageHandler handler) => _handler = handler;
|
|
public HttpClient CreateClient(string name) => new(_handler, disposeHandler: false);
|
|
}
|
|
|
|
private sealed class CaptureHandler : HttpMessageHandler
|
|
{
|
|
private readonly List<HttpRequestMessage> _captured;
|
|
private readonly Func<HttpRequestMessage, HttpResponseMessage>? _responder;
|
|
|
|
public CaptureHandler(List<HttpRequestMessage> captured, Func<HttpRequestMessage, HttpResponseMessage>? responder)
|
|
{
|
|
_captured = captured;
|
|
_responder = responder;
|
|
}
|
|
|
|
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
_captured.Add(request);
|
|
var resp = _responder?.Invoke(request) ?? new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent("{}", Encoding.UTF8, "application/json")
|
|
};
|
|
return Task.FromResult(resp);
|
|
}
|
|
}
|
|
}
|