Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webapi): Require legacy scope for HTML support #1073

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public static class Constants
public static readonly Uri UnauthorizedUri = new("urn:dialogporten:unauthorized");
public const string CorrespondenceScope = "digdir:dialogporten.correspondence";
public const string ServiceOwnerAdminScope = "digdir:dialogporten.serviceprovider.admin";
public const string LegacyHtmlScope = "digdir:dialogporten.serviceprovider.legacyhtml";
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Digdir.Domain.Dialogporten.Application.Common.Authorization;
using Digdir.Domain.Dialogporten.Application.Common.Extensions;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation;
using Digdir.Domain.Dialogporten.Application.Externals.Presentation;
using Digdir.Domain.Dialogporten.Application.Features.V1.Common.Localizations;
using Digdir.Domain.Dialogporten.Domain;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Contents;
Expand All @@ -14,19 +17,19 @@ internal interface IIgnoreOnAssemblyScan;

internal sealed class ContentValueDtoValidator : AbstractValidator<ContentValueDto>, IIgnoreOnAssemblyScan
{
private const string LegacyHtmlMediaType = "text/html";

public ContentValueDtoValidator(DialogTransmissionContentType contentType)
{
RuleFor(x => x.MediaType)
.NotEmpty()
.Must(value => value is not null && contentType.AllowedMediaTypes.Contains(value))
.WithMessage($"{{PropertyName}} '{{PropertyValue}}' is not allowed for content type {contentType.Name}. " +
$"Allowed media types are {string.Join(", ", contentType.AllowedMediaTypes.Select(x => $"'{x}'"))}");
RuleForEach(x => x.Value)
.ContainsValidHtml()
.When(x => x.MediaType is not null and MediaTypes.Html);

RuleForEach(x => x.Value)
.ContainsValidMarkdown()
.When(x => x.MediaType is not null and MediaTypes.Markdown);
.When(x => x.MediaType is MediaTypes.Markdown);
RuleForEach(x => x.Value)
.Must(x => Uri.TryCreate(x.Value, UriKind.Absolute, out var uri) && uri.Scheme == Uri.UriSchemeHttps)
.When(x => x.MediaType is not null && x.MediaType.StartsWith(MediaTypes.EmbeddablePrefix, StringComparison.InvariantCultureIgnoreCase))
Expand All @@ -36,19 +39,20 @@ public ContentValueDtoValidator(DialogTransmissionContentType contentType)
.SetValidator(_ => new LocalizationDtosValidator(contentType.MaxLength));
}

public ContentValueDtoValidator(DialogContentType contentType)
public ContentValueDtoValidator(DialogContentType contentType, IUser? user = null)
{
var allowedMediaTypes = GetAllowedMediaTypes(contentType, user);
RuleFor(x => x.MediaType)
.NotEmpty()
.Must(value => value is not null && contentType.AllowedMediaTypes.Contains(value))
.Must(value => value is not null && allowedMediaTypes.Contains(value))
.WithMessage($"{{PropertyName}} '{{PropertyValue}}' is not allowed for content type {contentType.Name}. " +
$"Allowed media types are {string.Join(", ", contentType.AllowedMediaTypes.Select(x => $"'{x}'"))}");
$"Allowed media types are {string.Join(", ", allowedMediaTypes.Select(x => $"'{x}'"))}");
RuleForEach(x => x.Value)
.ContainsValidHtml()
.When(x => x.MediaType is not null and MediaTypes.Html);
.When(x => x.MediaType.Equals(LegacyHtmlMediaType, StringComparison.OrdinalIgnoreCase));
RuleForEach(x => x.Value)
.ContainsValidMarkdown()
.When(x => x.MediaType is not null and MediaTypes.Markdown);
.When(x => x.MediaType is MediaTypes.Markdown);
RuleForEach(x => x.Value)
.Must(x => Uri.TryCreate(x.Value, UriKind.Absolute, out var uri) && uri.Scheme == Uri.UriSchemeHttps)
.When(x => x.MediaType is not null && x.MediaType.StartsWith(MediaTypes.EmbeddablePrefix, StringComparison.InvariantCultureIgnoreCase))
Expand All @@ -57,4 +61,18 @@ public ContentValueDtoValidator(DialogContentType contentType)
.NotEmpty()
.SetValidator(_ => new LocalizationDtosValidator(contentType.MaxLength));
}

private static string[] GetAllowedMediaTypes(DialogContentType contentType, IUser? user)
{
if (user == null)
{
return contentType.AllowedMediaTypes;
}

var allowHtmlSupport = user.GetPrincipal().HasScope(Constants.LegacyHtmlScope);

return allowHtmlSupport
? contentType.AllowedMediaTypes.Append(LegacyHtmlMediaType).ToArray()
: contentType.AllowedMediaTypes;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Reflection;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.Enumerables;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation;
using Digdir.Domain.Dialogporten.Application.Features.V1.Common;
using Digdir.Domain.Dialogporten.Application.Externals.Presentation;
using Digdir.Domain.Dialogporten.Application.Features.V1.Common.Content;
using Digdir.Domain.Dialogporten.Application.Features.V1.Common.Localizations;
using Digdir.Domain.Dialogporten.Domain.Actors;
Expand Down Expand Up @@ -168,7 +168,7 @@ internal sealed class CreateDialogContentDtoValidator : AbstractValidator<Create
})
.ToDictionary(x => x.Property.Name, StringComparer.InvariantCultureIgnoreCase);

public CreateDialogContentDtoValidator()
public CreateDialogContentDtoValidator(IUser? user)
{
foreach (var (propertyName, propMetadata) in SourcePropertyMetaDataByName)
{
Expand All @@ -179,12 +179,12 @@ public CreateDialogContentDtoValidator()
.NotNull()
.WithMessage($"{propertyName} must not be empty.")
.SetValidator(new ContentValueDtoValidator(
DialogContentType.Parse(propertyName))!);
DialogContentType.Parse(propertyName), user)!);
break;
case NullabilityState.Nullable:
RuleFor(x => propMetadata.Property.GetValue(x) as ContentValueDto)
.SetValidator(new ContentValueDtoValidator(
DialogContentType.Parse(propertyName))!)
DialogContentType.Parse(propertyName), user)!)
.When(x => propMetadata.Property.GetValue(x) is not null);
break;
case NullabilityState.Unknown:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reflection;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.Enumerables;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation;
using Digdir.Domain.Dialogporten.Application.Externals.Presentation;
using Digdir.Domain.Dialogporten.Application.Features.V1.Common;
using Digdir.Domain.Dialogporten.Application.Features.V1.Common.Content;
using Digdir.Domain.Dialogporten.Application.Features.V1.Common.Localizations;
Expand Down Expand Up @@ -222,7 +223,7 @@ internal sealed class UpdateDialogContentDtoValidator : AbstractValidator<Update
})
.ToDictionary(x => x.Property.Name, StringComparer.InvariantCultureIgnoreCase);

public UpdateDialogContentDtoValidator()
public UpdateDialogContentDtoValidator(IUser? user)
{
foreach (var (propertyName, propMetadata) in SourcePropertyMetaDataByName)
{
Expand All @@ -233,12 +234,12 @@ public UpdateDialogContentDtoValidator()
.NotNull()
.WithMessage($"{propertyName} must not be empty.")
.SetValidator(
new ContentValueDtoValidator(DialogContentType.Parse(propertyName))!);
new ContentValueDtoValidator(DialogContentType.Parse(propertyName), user)!);
break;
case NullabilityState.Nullable:
RuleFor(x => propMetadata.Property.GetValue(x) as ContentValueDto)
.SetValidator(
new ContentValueDtoValidator(DialogContentType.Parse(propertyName))!)
new ContentValueDtoValidator(DialogContentType.Parse(propertyName), user)!)
.When(x => propMetadata.Property.GetValue(x) is not null);
break;
case NullabilityState.Unknown:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public enum Values
Required = false,
MaxLength = 1023,
OutputInList = false,
AllowedMediaTypes = [MediaTypes.Html, MediaTypes.PlainText, MediaTypes.Markdown]
AllowedMediaTypes = [MediaTypes.PlainText, MediaTypes.Markdown]
},
Values.ExtendedStatus => new(id)
{
Expand Down
1 change: 0 additions & 1 deletion src/Digdir.Domain.Dialogporten.Domain/MediaTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ public static class MediaTypes

public const string Markdown = "text/markdown";
public const string PlainText = "text/plain";
public const string Html = "text/html";
}
Loading
Loading