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

fix(webapi): Mask unauthorized attachment URLs in EndUser transmission endpoints #1890

Merged

Conversation

oskogstad
Copy link
Collaborator

@oskogstad oskogstad commented Feb 16, 2025

Description

EndUser transmission endpoints gives out original URLs when the user is not authorized to see them

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)

Documentation

  • Documentation is updated (either in docs-directory, Altinnpedia or a separate linked PR in altinn-studio-docs., if applicable)

@oskogstad oskogstad requested a review from a team as a code owner February 16, 2025 15:52
Copy link
Contributor

coderabbitai bot commented Feb 16, 2025

📝 Walkthrough

Walkthrough

The changes update how attachment URLs are processed in transmission queries. In the GetTransmissionQuery, the URL source shifts from the transmission’s attachments to the mapped DTO’s attachments when the authorization flag is false. In the SearchTransmissionQuery, the returned list of DTOs is iterated to set an authorization flag, and for transmissions that are not authorized, attachment URLs are replaced with a constant unauthorized URI. There are no changes to public entity declarations.

Changes

File(s) Change Summary
src/Digdir.Domain.Dialogporten.Application/.../Queries/Get/GetTransmissionQuery.cs Updated URL access to use dto.Attachments instead of transmission.Attachments when dto.IsAuthorized is false.
src/Digdir.Domain.Dialogporten.Application/.../Queries/Search/SearchTransmissionQuery.cs Modified the handle method to store mapped DTOs, set the IsAuthorized property for each transmission, and replace unauthorized attachment URLs with a constant URI.

Suggested reviewers

  • arealmaas

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/Digdir.Domain.Dialogporten.Application/Features/V1/EndUser/DialogTransmissions/Queries/Search/SearchTransmissionQuery.cs (1)

72-88: Verify performance impact of post-mapping modifications.

The code maps the entire list of transmissions and then iterates through it to modify unauthorized URLs. This could be optimized by:

  1. Projecting only necessary fields during the database query
  2. Handling authorization during mapping

Consider optimizing by creating a custom mapping profile that handles authorization during the mapping process:

- var dto = _mapper.Map<List<TransmissionDto>>(dialog.Transmissions);
- 
- foreach (var transmission in dto)
- {
-     transmission.IsAuthorized = authorizationResult.HasReadAccessToDialogTransmission(transmission.AuthorizationAttribute);
- 
-     if (transmission.IsAuthorized) continue;
- 
-     var urls = transmission.Attachments.SelectMany(a => a.Urls).ToList();
-     foreach (var url in urls)
-     {
-         url.Url = Constants.UnauthorizedUri;
-     }
- }
- 
- return dto;
+ var dto = dialog.Transmissions.Select(transmission => {
+     var mappedDto = _mapper.Map<TransmissionDto>(transmission);
+     mappedDto.IsAuthorized = authorizationResult.HasReadAccessToDialogTransmission(transmission.AuthorizationAttribute);
+     
+     if (!mappedDto.IsAuthorized)
+     {
+         foreach (var attachment in mappedDto.Attachments)
+         {
+             foreach (var url in attachment.Urls)
+             {
+                 url.Url = Constants.UnauthorizedUri;
+             }
+         }
+     }
+     
+     return mappedDto;
+ }).ToList();
+ 
+ return dto;
src/Digdir.Domain.Dialogporten.Application/Features/V1/EndUser/DialogTransmissions/Queries/Get/GetTransmissionQuery.cs (1)

86-91: Ensure consistent URL masking across endpoints.

The URL masking implementation is similar to SearchTransmissionQuery but with subtle differences in the code structure. While functionally correct, consider extracting this common logic into a shared helper method to ensure consistent behavior and easier maintenance.

Consider creating a shared helper method:

public static class TransmissionUrlHelper
{
    public static void MaskUnauthorizedUrls(TransmissionDto transmission)
    {
        if (transmission.IsAuthorized) return;
        
        foreach (var url in transmission.Attachments.SelectMany(a => a.Urls))
        {
            url.Url = Constants.UnauthorizedUri;
        }
    }
}

Then use it in both query handlers:

- var urls = dto.Attachments.SelectMany(a => a.Urls).ToList();
- foreach (var url in urls)
- {
-     url.Url = Constants.UnauthorizedUri;
- }
+ TransmissionUrlHelper.MaskUnauthorizedUrls(dto);
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dcc5ee7 and a6c3a33.

📒 Files selected for processing (2)
  • src/Digdir.Domain.Dialogporten.Application/Features/V1/EndUser/DialogTransmissions/Queries/Get/GetTransmissionQuery.cs (1 hunks)
  • src/Digdir.Domain.Dialogporten.Application/Features/V1/EndUser/DialogTransmissions/Queries/Search/SearchTransmissionQuery.cs (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Dry run deploy apps / Deploy job sync-resource-policy-information-job to test
  • GitHub Check: Dry run deploy apps / Deploy job sync-subject-resource-mappings-job to test
  • GitHub Check: build / build-and-test
🔇 Additional comments (1)
src/Digdir.Domain.Dialogporten.Application/Features/V1/EndUser/DialogTransmissions/Queries/Get/GetTransmissionQuery.cs (1)

75-79:

❓ Verification inconclusive

Verify transmission lookup logic.

The current implementation uses FirstOrDefault() without any ordering, which might not consistently return the expected transmission when multiple transmissions exist for the dialog.

Consider adding explicit ordering or using SingleOrDefault() with the transmission ID filter:

- var transmission = dialog.Transmissions.FirstOrDefault();
+ var transmission = dialog.Transmissions.SingleOrDefault();

Also, verify that the Where clause in the Include statements correctly filters the transmissions:


🏁 Script executed:

#!/bin/bash
# Description: Check if there are any cases where multiple transmissions might exist for a dialog

# Search for transmission-related queries
rg -A 5 "Transmissions\.Where"

# Search for transmission ID usage
rg -A 5 "TransmissionId"

Length of output: 355


Action: Validate Transmission Lookup and Filtering Approach

  • Confirm that using FirstOrDefault() on dialog.Transmissions reliably returns the intended transmission when more than one is present. If the intent is to ensure a single matching transmission, consider filtering by TransmissionId and using SingleOrDefault() instead.
  • If multiple transmissions are a possibility, introduce explicit ordering (for example, by creation date or another business-relevant field) before selecting the first element.
  • Additionally, verify that any Include statements employing Where clauses correctly filter the transmissions as intended.

@oskogstad oskogstad merged commit f2f817b into main Feb 16, 2025
24 checks passed
@oskogstad oskogstad deleted the fix/mask-unauthorized-urls-in-enduser-transmission-endpoints branch February 16, 2025 17:28
oskogstad pushed a commit that referenced this pull request Feb 17, 2025
🤖 I have created a release *beep* *boop*
---


##
[1.51.0](v1.50.9...v1.51.0)
(2025-02-17)


### Features

* Add event for transmission created
([#1893](#1893))
([ae0f7dc](ae0f7dc))


### Bug Fixes

* **webapi:** Mask unauthorized attachment URLs in EndUser transmission
endpoints ([#1890](#1890))
([f2f817b](f2f817b))


### Miscellaneous Chores

* **ci:** Always push new container version tags on release
([#1887](#1887))
([7d31b3b](7d31b3b))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants