-
Notifications
You must be signed in to change notification settings - Fork 2
Port the pen-test security fixes to r10 #286
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b69e139
fix: replace the CDN-loaded Monaco editor with bundled CodeMirror
hamzahalq fb02451
fix: sign out after 30 minutes of inactivity
hamzahalq ad6a41a
fix: check the password policy in the profile and member forms
hamzahalq c6f2068
fix: parameterize the run-flag SQL
hamzahalq 0fcbf7d
chore: declare the directly-imported CodeMirror packages
hamzahalq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using SW.Bitween.Domain; | ||
| using SW.Bitween.IntegrationTests.Fixtures; | ||
| using SW.Bitween.Model; | ||
| using Xunit; | ||
|
|
||
| namespace SW.Bitween.IntegrationTests.Tests; | ||
|
|
||
| // The run flag is written with raw (parameterized) SQL that differs per database | ||
| // provider, so it needs a real round trip to prove the statement and its parameter | ||
| // binding are correct. There was no coverage here before. | ||
| [Collection("Bitween")] | ||
| public class RunFlagUpdaterTests | ||
| { | ||
| private readonly BitweenFixture _fixture; | ||
|
|
||
| public RunFlagUpdaterTests(BitweenFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Run_flag_claims_once_then_blocks_until_idle() | ||
| { | ||
| await using var scope = _fixture.CreateScope(); | ||
| var db = scope.ServiceProvider.GetRequiredService<BitweenDbContext>(); | ||
| var runFlag = scope.ServiceProvider.GetRequiredService<RunFlagUpdater>(); | ||
|
|
||
| var document = new Document(6101, "Run Flag Test Doc"); | ||
| db.Set<Document>().Add(document); | ||
| var subscription = new Subscription("Run Flag Test", document.Id); | ||
| subscription.Inactive = false; | ||
| db.Set<Subscription>().Add(subscription); | ||
| await db.SaveChangesAsync(); | ||
|
|
||
| Assert.True(await runFlag.MarkAsRunning(subscription.Id)); // first claim wins | ||
| Assert.False(await runFlag.MarkAsRunning(subscription.Id)); // already running | ||
|
|
||
| await runFlag.MarkAsIdle(subscription.Id); | ||
|
|
||
| Assert.True(await runFlag.MarkAsRunning(subscription.Id)); // claimable again | ||
| await runFlag.MarkAsIdle(subscription.Id); | ||
| } | ||
|
|
||
| // Guards the parameter binding: a broken placeholder would either match no rows | ||
| // or every row, and both would show up here. | ||
| [Fact] | ||
| public async Task Run_flag_only_affects_the_requested_subscription() | ||
| { | ||
| await using var scope = _fixture.CreateScope(); | ||
| var db = scope.ServiceProvider.GetRequiredService<BitweenDbContext>(); | ||
| var runFlag = scope.ServiceProvider.GetRequiredService<RunFlagUpdater>(); | ||
|
|
||
| var document = new Document(6102, "Run Flag Isolation Doc"); | ||
| db.Set<Document>().Add(document); | ||
| var a = new Subscription("Run Flag A", document.Id); | ||
| a.Inactive = false; | ||
| var b = new Subscription("Run Flag B", document.Id); | ||
| b.Inactive = false; | ||
| db.Set<Subscription>().AddRange(a, b); | ||
| await db.SaveChangesAsync(); | ||
|
|
||
| Assert.True(await runFlag.MarkAsRunning(a.Id)); | ||
| Assert.True(await runFlag.MarkAsRunning(b.Id)); // b untouched by a's update | ||
|
|
||
| await runFlag.MarkAsIdle(a.Id); | ||
| Assert.False(await runFlag.MarkAsRunning(b.Id)); // b still running, a's idle did not clear it | ||
|
|
||
| await runFlag.MarkAsIdle(b.Id); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { useEffect, useRef } from "react"; | ||
|
|
||
| const IDLE_TIMEOUT_MS = 30 * 60 * 1000; // 30 minutes | ||
| const CHECK_INTERVAL_MS = 30 * 1000; // re-check every 30s | ||
| const ACTIVITY_EVENTS = ["mousemove", "mousedown", "keydown", "scroll", "touchstart"]; | ||
| /** Shared so activity in any tab keeps every tab alive (see the interval below). */ | ||
| const LAST_ACTIVITY_KEY = "last_activity"; | ||
|
|
||
| const readSharedActivity = (): number => { | ||
| try { | ||
| return Number(localStorage.getItem(LAST_ACTIVITY_KEY)) || 0; | ||
| } catch { | ||
| return 0; // storage unavailable: fall back to this tab's own timer | ||
| } | ||
| }; | ||
|
|
||
| const writeSharedActivity = (ts: number) => { | ||
| try { | ||
| localStorage.setItem(LAST_ACTIVITY_KEY, String(ts)); | ||
| } catch { | ||
| // ignore: the in-tab ref still tracks activity | ||
| } | ||
| }; | ||
|
|
||
| const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
|
||
| /** | ||
| * Signs the user out after a period with no activity in any tab. Runs the normal | ||
| * sign-out path, so an idle session ends exactly like clicking Sign out. | ||
| */ | ||
| export function useIdleLogout( | ||
| enabled: boolean, | ||
| signOut: () => Promise<void>, | ||
| timeoutMs: number = IDLE_TIMEOUT_MS, | ||
| ) { | ||
| const lastActivity = useRef<number>(Date.now()); | ||
| // Held in a ref so a new signOut identity doesn't restart the idle timer. | ||
| const signOutRef = useRef(signOut); | ||
| useEffect(() => { | ||
| signOutRef.current = signOut; | ||
| }, [signOut]); | ||
|
|
||
| useEffect(() => { | ||
| if (!enabled) return; | ||
|
|
||
| // Start counting from the moment we become enabled. The ref is created when the | ||
| // provider first mounts, which is while the login page is showing and no activity | ||
| // listeners are attached, so without this a login page left open longer than | ||
| // timeoutMs would sign the user out immediately after signing in. | ||
| const enabledAt = Date.now(); | ||
| lastActivity.current = enabledAt; | ||
| writeSharedActivity(enabledAt); | ||
|
|
||
| const markActivity = () => { | ||
| const ts = Date.now(); | ||
| lastActivity.current = ts; | ||
| writeSharedActivity(ts); | ||
| }; | ||
| ACTIVITY_EVENTS.forEach((e) => window.addEventListener(e, markActivity, { passive: true })); | ||
|
|
||
| const interval = window.setInterval(async () => { | ||
| // Activity events only fire in the focused tab, so take the most recent activity | ||
| // across tabs. Otherwise a background tab would sign out a user who is actively | ||
| // working in another one. | ||
| const last = Math.max(lastActivity.current, readSharedActivity()); | ||
| if (Date.now() - last < timeoutMs) return; | ||
|
|
||
| window.clearInterval(interval); | ||
| ACTIVITY_EVENTS.forEach((e) => window.removeEventListener(e, markActivity)); | ||
| try { | ||
| await signOutRef.current(); | ||
| } catch { | ||
| // Only the server call invalidates the refresh-token cookie (it is HttpOnly, | ||
| // so JS cannot clear it). Retry once before giving up. | ||
| await sleep(2000); | ||
| try { | ||
| await signOutRef.current(); | ||
| } catch { | ||
| // api.logout() clears the stored Jwt in a finally, so it is already gone by | ||
| // now; reloading is what drops the in-memory session and shows the login page. | ||
| window.location.reload(); | ||
| } | ||
| } | ||
| }, CHECK_INTERVAL_MS); | ||
|
|
||
| return () => { | ||
| window.clearInterval(interval); | ||
| ACTIVITY_EVENTS.forEach((e) => window.removeEventListener(e, markActivity)); | ||
| }; | ||
| }, [enabled, timeoutMs]); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.