Progmasoft Git

progmaweb

The site and account service of Progmasoft: a Next.js frontend and an ASP.NET Core API.

Clonegit clone https://git.progmasoft.com/progmaweb

Harden Progmaweb account API and CI coverage credentials

Commit
bcf10c927358157bedf6ab09ff6bff0a8aa86a14
Author
Leitwolf11 ·
Parents
e174050d380c

Files · Download .tar.gz

Changes

 .github/workflows/backend.yml               | 28 +++++++++++++++++++++++----
 .github/workflows/codeql.yml                |  8 ++++----
 .github/workflows/frontend.yml              | 30 ++++++++++++++++++++++++-----
 .github/workflows/security.yml              | 22 +++++++++++++++------
 README.md                                   |  6 ++++--
 apps/api-tests/AccountEndpointTests.cs      | 16 +++++++++++++++
 apps/api-tests/Progmaweb.Api.Tests.csproj   |  1 +
 apps/api-tests/SecurityHeaderTests.cs       | 17 ++++++++++++++++
 apps/api/Accounts/AccountService.cs         | 24 ++++++++++++++---------
 apps/api/Program.cs                         | 10 +++-------
 apps/api/Security/ForwardedHeadersPolicy.cs | 29 ++++++++++++++++++++++++++++
 11 files changed, 154 insertions(+), 37 deletions(-)

diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml
index 0aa2c83..173c5c6 100644
--- a/.github/workflows/backend.yml
+++ b/.github/workflows/backend.yml
@@ -23,7 +23,6 @@ on:
 
 permissions:
   contents: read
-  id-token: write
 
 concurrency:
   group: backend-${{ github.ref }}
@@ -34,8 +33,8 @@ jobs:
     runs-on: ubuntu-latest
     timeout-minutes: 15
     steps:
-      - uses: actions/checkout@v6
-      - uses: actions/setup-dotnet@v5
+      - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
+      - uses: actions/setup-dotnet@26b0ec14cb23fa6904739307f278c14f94c95bf1 # v5
         with:
           dotnet-version: 10.0.x
           cache: true
@@ -43,9 +42,30 @@ jobs:
       - run: dotnet restore Progmaweb.slnx --locked-mode
       - run: dotnet build Progmaweb.slnx --configuration Release --no-restore
       - run: dotnet test --project apps/api-tests/Progmaweb.Api.Tests.csproj --configuration Release --no-build --coverlet --coverlet-output-format cobertura
+
+      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
+        with:
+          name: progmaweb-backend-coverage
+          path: TestResults/coverage.cobertura.xml
+          if-no-files-found: error
+          retention-days: 1
+
+  upload-coverage:
+    needs: verify
+    # Fork PR code is untrusted and must never receive an OIDC-capable job.
+    if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
+    runs-on: ubuntu-latest
+    timeout-minutes: 10
+    permissions:
+      id-token: write
+    steps:
+      - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
+        with:
+          name: progmaweb-backend-coverage
+          path: coverage
       - uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0
         with:
           use_oidc: true
-          files: TestResults/coverage.cobertura.xml
+          files: coverage/coverage.cobertura.xml
           flags: progmaweb-backend
           fail_ci_if_error: true
diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml
index 1f6db33..5974d6f 100644
--- a/.github/workflows/codeql.yml
+++ b/.github/workflows/codeql.yml
@@ -28,12 +28,12 @@ jobs:
           - language: csharp
             build_mode: manual
     steps:
-      - uses: actions/checkout@v7
-      - uses: actions/setup-dotnet@v5
+      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
+      - uses: actions/setup-dotnet@26b0ec14cb23fa6904739307f278c14f94c95bf1 # v5
         if: matrix.language == 'csharp'
         with:
           dotnet-version: 10.0.x
-      - uses: github/codeql-action/init@7999b86c43a865dc79d8923397f35af22de63401 # v4
+      - uses: github/codeql-action/init@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4
         with:
           languages: ${{ matrix.language }}
           build-mode: ${{ matrix.build_mode }}
@@ -44,7 +44,7 @@ jobs:
         run: |
           dotnet restore Progmaweb.slnx --locked-mode
           dotnet build Progmaweb.slnx --configuration Release --no-restore
-      - uses: github/codeql-action/analyze@7999b86c43a865dc79d8923397f35af22de63401 # v4
+      - uses: github/codeql-action/analyze@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4
         with:
           category: /language:${{ matrix.language }}
 
diff --git a/.github/workflows/frontend.yml b/.github/workflows/frontend.yml
index 87e17da..af8b85a 100644
--- a/.github/workflows/frontend.yml
+++ b/.github/workflows/frontend.yml
@@ -23,7 +23,6 @@ on:
 
 permissions:
   contents: read
-  id-token: write
 
 concurrency:
   group: frontend-${{ github.ref }}
@@ -34,12 +33,12 @@ jobs:
     runs-on: ubuntu-latest
     timeout-minutes: 15
     steps:
-      - uses: actions/checkout@v6
-      - uses: pnpm/action-setup@v5
+      - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
+      - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v5
         with:
           version: 11.24.0
           run_install: false
-      - uses: actions/setup-node@v6
+      - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6
         with:
           node-version: 24
           cache: pnpm
@@ -48,9 +47,30 @@ jobs:
       - run: pnpm test:coverage
       - run: pnpm lint
       - run: pnpm build
+
+      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
+        with:
+          name: progmaweb-frontend-coverage
+          path: apps/web/coverage/lcov.info
+          if-no-files-found: error
+          retention-days: 1
+
+  upload-coverage:
+    needs: verify
+    # Fork PR code is untrusted and must never receive an OIDC-capable job.
+    if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
+    runs-on: ubuntu-latest
+    timeout-minutes: 10
+    permissions:
+      id-token: write
+    steps:
+      - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
+        with:
+          name: progmaweb-frontend-coverage
+          path: coverage
       - uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0
         with:
           use_oidc: true
-          files: apps/web/coverage/lcov.info
+          files: coverage/lcov.info
           flags: progmaweb-frontend
           fail_ci_if_error: true
diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml
index de39998..4504f65 100644
--- a/.github/workflows/security.yml
+++ b/.github/workflows/security.yml
@@ -23,22 +23,32 @@ jobs:
     if: github.event_name == 'pull_request'
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v6
-      - uses: actions/dependency-review-action@v4
+      - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
+      - uses: actions/dependency-review-action@2031cfc080254a8a887f58cffee85186f0e49e48 # v4
 
   dependency-audit:
-    if: github.event_name != 'pull_request'
     runs-on: ubuntu-latest
     timeout-minutes: 10
     steps:
-      - uses: actions/checkout@v6
-      - uses: pnpm/action-setup@v5
+      - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
+      - uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v5
         with:
           version: 11.24.0
           run_install: false
-      - uses: actions/setup-node@v6
+      - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6
         with:
           node-version: 24
           cache: pnpm
       - run: pnpm install --frozen-lockfile
       - run: pnpm audit --audit-level high
+
+  nuget-audit:
+    runs-on: ubuntu-latest
+    timeout-minutes: 10
+    steps:
+      - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
+      - uses: actions/setup-dotnet@26b0ec14cb23fa6904739307f278c14f94c95bf1 # v5
+        with:
+          dotnet-version: 10.0.x
+      - run: dotnet restore Progmaweb.slnx --locked-mode -warnaserror
+      - run: dotnet list Progmaweb.slnx package --vulnerable --include-transitive
diff --git a/README.md b/README.md
index 339dc8a..db69dac 100644
--- a/README.md
+++ b/README.md
@@ -80,8 +80,10 @@ dotnet test --project apps/api-tests/Progmaweb.Api.Tests.csproj --configuration
 ```
 
 Frontend tests exercise the published locale contract and account/ViGet metadata without starting a production service.
-The frontend LCOV and API Cobertura reports are uploaded to Codecov as separate components in CI. CodeQL and Codacy
-provide static analysis; Dependabot proposes dependency updates without merging them automatically.
+The frontend LCOV and API Cobertura reports are retained as one-day artifacts, then uploaded to Codecov by separate
+jobs. Only those upload jobs receive `id-token: write`; dependency installation, builds, and tests do not receive OIDC
+permissions, and fork-originated pull requests skip the upload jobs. CodeQL and Codacy provide static analysis;
+Dependabot proposes dependency updates without merging them automatically.
 
 ## License
 
diff --git a/apps/api-tests/AccountEndpointTests.cs b/apps/api-tests/AccountEndpointTests.cs
index 8453824..9459081 100644
--- a/apps/api-tests/AccountEndpointTests.cs
+++ b/apps/api-tests/AccountEndpointTests.cs
@@ -234,6 +234,22 @@ public sealed class AccountEndpointTests
             wrongBody.RootElement.GetProperty("detail").GetString());
     }
 
+    [Fact]
+    public async Task LoginRejectsPasswordsAboveTheRegistrationLimit()
+    {
+        await using AccountApiFactory factory = new();
+        using HttpClient client = factory.CreateAccountClient();
+
+        HttpResponseMessage response = await client.PostAsJsonAsync("/api/v1/accounts/login", new
+        {
+            email = "person@example.com",
+            password = new string('x', PasswordPolicy.MaximumLength + 1)
+        }, TestContext.Current.CancellationToken);
+
+        Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
+        Assert.False(response.Headers.Contains("Set-Cookie"));
+    }
+
     [Fact]
     public async Task StatusResponseAdvertisesProgmawebVersion()
     {
diff --git a/apps/api-tests/Progmaweb.Api.Tests.csproj b/apps/api-tests/Progmaweb.Api.Tests.csproj
index efb6a2f..3ac6aee 100644
--- a/apps/api-tests/Progmaweb.Api.Tests.csproj
+++ b/apps/api-tests/Progmaweb.Api.Tests.csproj
@@ -10,6 +10,7 @@
     <RootNamespace>Progmasoft.Progmaweb.Api.Tests</RootNamespace>
   </PropertyGroup>
   <ItemGroup>
+    <FrameworkReference Include="Microsoft.AspNetCore.App" />
     <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.11" />
     <PackageReference Include="coverlet.MTP" Version="8.0.0" />
     <!-- xUnit's MTP-v2 package is the native .NET 10 test host. Keeping the
diff --git a/apps/api-tests/SecurityHeaderTests.cs b/apps/api-tests/SecurityHeaderTests.cs
index 348f10e..2d53ecf 100644
--- a/apps/api-tests/SecurityHeaderTests.cs
+++ b/apps/api-tests/SecurityHeaderTests.cs
@@ -2,11 +2,28 @@
 // SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1
 
 using System.Net;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.HttpOverrides;
+using Progmasoft.Progmaweb.Api.Security;
 
 namespace Progmasoft.Progmaweb.Api.Tests;
 
 public sealed class SecurityHeaderTests
 {
+    [Fact]
+    public void ForwardedHeadersTrustOnlyTheLocalReverseProxy()
+    {
+        ForwardedHeadersOptions options = new();
+
+        ForwardedHeadersPolicy.Configure(options);
+
+        Assert.Equal(ForwardedHeadersPolicy.MaximumForwardedHops, options.ForwardLimit);
+        Assert.Equal(ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedHost |
+            ForwardedHeaders.XForwardedProto, options.ForwardedHeaders);
+        Assert.Equal(ForwardedHeadersPolicy.TrustedProxies, options.KnownProxies);
+        Assert.Empty(options.KnownIPNetworks);
+    }
+
     [Fact]
     public async Task ApiResponsesSetDefensiveHeaders()
     {
diff --git a/apps/api/Accounts/AccountService.cs b/apps/api/Accounts/AccountService.cs
index 2126f2b..7168eb6 100644
--- a/apps/api/Accounts/AccountService.cs
+++ b/apps/api/Accounts/AccountService.cs
@@ -9,6 +9,18 @@ internal sealed class AccountService(
     IAccountStore store,
     TimeProvider timeProvider)
 {
+    private static readonly AccountRecord DummyAccount = new(
+        Guid.Empty,
+        "Missing00",
+        "MISSING00",
+        "missing@example.invalid",
+        "MISSING@EXAMPLE.INVALID",
+        null,
+        string.Empty,
+        DateTimeOffset.UnixEpoch);
+    private static readonly string DummyPasswordHash =
+        new PasswordHasher<AccountRecord>().HashPassword(DummyAccount, "not-the-provided-password");
+
     private readonly PasswordHasher<AccountRecord> passwordHasher = new();
 
     public async ValueTask<(AuthenticatedAccount? Authentication, Dictionary<string, string[]> Errors)> RegisterAsync(
@@ -57,7 +69,7 @@ internal sealed class AccountService(
         CancellationToken cancellationToken)
     {
         if (!EmailAddressPolicy.TryNormalize(request.Email, out _, out string normalizedEmail, out _) ||
-            request.Password is null)
+            request.Password is null || request.Password.Length > PasswordPolicy.MaximumLength)
         {
             return null;
         }
@@ -65,14 +77,8 @@ internal sealed class AccountService(
         AccountRecord? account = await store.FindByEmailAsync(normalizedEmail, cancellationToken);
         if (account is null || account.PasswordHash is null)
         {
-            // A fixed dummy record keeps the missing-account path on the same password-hashing primitive.
-            AccountRecord dummyBase = new(Guid.Empty, "Missing00", "MISSING00", "missing@example.invalid",
-                "MISSING@EXAMPLE.INVALID", null, string.Empty, DateTimeOffset.UnixEpoch);
-            AccountRecord dummy = dummyBase with
-            {
-                PasswordHash = passwordHasher.HashPassword(dummyBase, "not-the-provided-password")
-            };
-            _ = passwordHasher.VerifyHashedPassword(dummy, dummy.PasswordHash, request.Password);
+            // Reuse one private hash so an unknown email follows one expensive verification, like a bad password.
+            _ = passwordHasher.VerifyHashedPassword(DummyAccount, DummyPasswordHash, request.Password);
             return null;
         }
 
diff --git a/apps/api/Program.cs b/apps/api/Program.cs
index deb3472..0b71d06 100644
--- a/apps/api/Program.cs
+++ b/apps/api/Program.cs
@@ -53,13 +53,9 @@ builder.Services.AddRateLimiter(options =>
     });
 });
 
-builder.Services.Configure<ForwardedHeadersOptions>(options =>
-{
-    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedHost |
-        ForwardedHeaders.XForwardedProto;
-    options.KnownIPNetworks.Clear();
-    options.KnownProxies.Clear();
-});
+builder.Services.Configure<ForwardedHeadersOptions>(ForwardedHeadersPolicy.Configure);
+
+builder.WebHost.ConfigureKestrel(options => options.Limits.MaxRequestBodySize = 16 * 1024);
 
 WebApplication app = builder.Build();
 
diff --git a/apps/api/Security/ForwardedHeadersPolicy.cs b/apps/api/Security/ForwardedHeadersPolicy.cs
new file mode 100644
index 0000000..046ad56
--- /dev/null
+++ b/apps/api/Security/ForwardedHeadersPolicy.cs
@@ -0,0 +1,29 @@
+// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
+// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1
+
+using System.Net;
+using Microsoft.AspNetCore.HttpOverrides;
+
+namespace Progmasoft.Progmaweb.Api.Security;
+
+internal static class ForwardedHeadersPolicy
+{
+    internal const int MaximumForwardedHops = 1;
+    internal static readonly IPAddress[] TrustedProxies = [IPAddress.Loopback, IPAddress.IPv6Loopback];
+
+    public static void Configure(ForwardedHeadersOptions options)
+    {
+        options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedHost |
+            ForwardedHeaders.XForwardedProto;
+        options.ForwardLimit = MaximumForwardedHops;
+
+        // The API is bound to loopback and only receives forwarded headers from the local Nginx/Next.js proxy.
+        // Trusting every peer would let a direct caller forge the IP used by the authentication rate limiter.
+        options.KnownIPNetworks.Clear();
+        options.KnownProxies.Clear();
+        foreach (IPAddress trustedProxy in TrustedProxies)
+        {
+            options.KnownProxies.Add(trustedProxy);
+        }
+    }
+}