diff --git a/services/SonicRelay.Api/Contracts/LaunchIntentContracts.cs b/services/SonicRelay.Api/Contracts/LaunchIntentContracts.cs index fa37983..ca6beb5 100644 --- a/services/SonicRelay.Api/Contracts/LaunchIntentContracts.cs +++ b/services/SonicRelay.Api/Contracts/LaunchIntentContracts.cs @@ -1,7 +1,7 @@ namespace SonicRelay.Api.Contracts; public sealed record CreateShareLaunchIntentRequest( - string Provider, string GuildId, string ChannelId, string RequestedByUserId, int? TtlSeconds = null); + string Provider, string? GuildId, string ChannelId, string RequestedByUserId, int? TtlSeconds = null); public sealed record CreateShareLaunchIntentResponse(Guid Id, string LaunchUrl, DateTimeOffset ExpiresAt); public sealed record ConsumeShareLaunchIntentRequest(string Token); diff --git a/services/SonicRelay.Api/Endpoints/DiscordActivityLaunchIntentEndpoints.cs b/services/SonicRelay.Api/Endpoints/DiscordActivityLaunchIntentEndpoints.cs index 399f2e7..3bee321 100644 --- a/services/SonicRelay.Api/Endpoints/DiscordActivityLaunchIntentEndpoints.cs +++ b/services/SonicRelay.Api/Endpoints/DiscordActivityLaunchIntentEndpoints.cs @@ -66,7 +66,10 @@ public static IEndpointRouteBuilder MapDiscordActivityLaunchIntentEndpoints(this bot.MapPost("/{intentId:guid}/watch", async (Guid intentId, ReadyActivityRequest request, LaunchIntentService service, IOptions options, CancellationToken ct) => { - if (!ValidContext(request.GuildId, request.ChannelId, request.RequestedByUserId)) return Results.BadRequest(); + // DMs and Group DMs have no guild; the stored intent's channel still binds the watch link. + if (!ValidContext(request.ChannelId, request.RequestedByUserId) + || (!string.IsNullOrEmpty(request.GuildId) && !ValidContext(request.GuildId))) + return Results.BadRequest(); try { var (token, expiresAt) = await service.CreateWatchCapabilityForReadyIntentAsync( diff --git a/services/SonicRelay.Api/Services/LaunchIntentService.cs b/services/SonicRelay.Api/Services/LaunchIntentService.cs index 46eb990..52560e3 100644 --- a/services/SonicRelay.Api/Services/LaunchIntentService.cs +++ b/services/SonicRelay.Api/Services/LaunchIntentService.cs @@ -19,11 +19,13 @@ public sealed class LaunchIntentService( private readonly LaunchIntentOptions _options = options.Value; public async Task<(ShareLaunchIntent Intent, string LaunchUrl)> CreateShareAsync( - string provider, string guildId, string channelId, string requestedByUserId, int? ttlSeconds, + string provider, string? guildId, string channelId, string requestedByUserId, int? ttlSeconds, CancellationToken ct) { + // DMs and Group DMs have no guild; the channel alone scopes the share. + guildId ??= string.Empty; if (!string.Equals(provider, "discord", StringComparison.OrdinalIgnoreCase) - || string.IsNullOrWhiteSpace(guildId) || string.IsNullOrWhiteSpace(channelId) + || (guildId.Length > 0 && string.IsNullOrWhiteSpace(guildId)) || string.IsNullOrWhiteSpace(channelId) || string.IsNullOrWhiteSpace(requestedByUserId) || guildId.Length > 64 || channelId.Length > 64 || requestedByUserId.Length > 64) throw new LaunchIntentException("invalid_request"); @@ -167,8 +169,9 @@ public async Task MarkPublishedAsync(Guid intentId, string? messageId, Cancellat } public async Task<(string Token, DateTimeOffset ExpiresAt)> CreateWatchCapabilityForReadyIntentAsync( - Guid intentId, string guildId, string channelId, int? ttlSeconds, CancellationToken ct) + Guid intentId, string? guildId, string channelId, int? ttlSeconds, CancellationToken ct) { + guildId ??= string.Empty; var intent = await db.ShareLaunchIntents.AsNoTracking().SingleOrDefaultAsync(x => x.Id == intentId, ct); if (intent?.Status != ShareLaunchIntentStatuses.Ready || intent.SessionId is not { } sessionId || intent.GuildId != guildId || intent.ChannelId != channelId) diff --git a/tests/SonicRelay.Api.IntegrationTests/LaunchIntentTests.cs b/tests/SonicRelay.Api.IntegrationTests/LaunchIntentTests.cs index 2f1bda9..46dd1c5 100644 --- a/tests/SonicRelay.Api.IntegrationTests/LaunchIntentTests.cs +++ b/tests/SonicRelay.Api.IntegrationTests/LaunchIntentTests.cs @@ -110,6 +110,67 @@ await DeviceIdentityTestHelper.BootstrapAndAuthorizeAsync( ready.GetProperty("watchLaunchUrl").GetString(), StringComparison.Ordinal); } + [Theory] + [InlineData("")] + [InlineData(null)] + public async Task Private_channel_share_without_guild_issues_watch_links_only_for_that_channel(string? guildId) + { + using var factory = new SonicRelayApiFactory(); + using var bot = factory.CreateClient(); + bot.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( + "Bearer", "integration-test-discord-service-token"); + var createdIntent = await bot.PostAsJsonAsync("/api/launch-intents/share", new + { + provider = "discord", guildId, channelId = "22", requestedByUserId = "33" + }); + Assert.Equal(HttpStatusCode.Created, createdIntent.StatusCode); + var launch = await createdIntent.Content.ReadFromJsonAsync(); + var intentId = launch.GetProperty("id").GetGuid(); + var token = launch.GetProperty("launchUrl").GetString()![("https://framerelay.hugojava.dev/open/share/".Length)..]; + + var pending = await bot.GetFromJsonAsync("/api/launch-intents/pending"); + Assert.Equal(string.Empty, pending.EnumerateArray().Single().GetProperty("guildId").GetString()); + + using var publisher = factory.CreateClient(); + await DeviceIdentityTestHelper.BootstrapAndAuthorizeAsync( + publisher, DeviceTypes.WindowsPublisher, DevicePlatforms.Windows); + (await publisher.PostAsJsonAsync("/api/launch-intents/share/consume", new { token })).EnsureSuccessStatusCode(); + var sessionResponse = await publisher.PostAsJsonAsync("/api/sessions", new { maxViewers = 3, mode = "screen_share" }); + var session = await sessionResponse.Content.ReadFromJsonAsync(); + (await publisher.PostAsJsonAsync($"/api/launch-intents/share/{intentId}/complete", + new { sessionId = session.GetProperty("id").GetGuid() })).EnsureSuccessStatusCode(); + + var watch = await bot.PostAsJsonAsync($"/api/launch-intents/{intentId}/watch", + new { guildId, channelId = "22", requestedByUserId = "44", ttlSeconds = 120 }); + Assert.Equal(HttpStatusCode.OK, watch.StatusCode); + var personal = await watch.Content.ReadFromJsonAsync(); + Assert.StartsWith("https://framerelay.hugojava.dev/open/watch/", + personal.GetProperty("launchUrl").GetString(), StringComparison.Ordinal); + + Assert.Equal(HttpStatusCode.NotFound, (await bot.PostAsJsonAsync($"/api/launch-intents/{intentId}/watch", + new { guildId, channelId = "23", requestedByUserId = "44" })).StatusCode); + Assert.Equal(HttpStatusCode.NotFound, (await bot.PostAsJsonAsync($"/api/launch-intents/{intentId}/watch", + new { guildId = "1", channelId = "22", requestedByUserId = "44" })).StatusCode); + Assert.Equal(HttpStatusCode.BadRequest, (await bot.PostAsJsonAsync($"/api/launch-intents/{intentId}/watch", + new { guildId = "", channelId = "", requestedByUserId = "44" })).StatusCode); + } + + [Fact] + public async Task Share_intent_without_channel_is_rejected() + { + using var factory = new SonicRelayApiFactory(); + using var bot = factory.CreateClient(); + bot.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( + "Bearer", "integration-test-discord-service-token"); + + var response = await bot.PostAsJsonAsync("/api/launch-intents/share", new + { + provider = "discord", guildId = "", channelId = "", requestedByUserId = "33" + }); + + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + } + [Fact] public async Task Viewer_resolves_watch_capability_with_join_scope_only() {