Skip to content

Commit d492746

Browse files
authored
Merge pull request #1028 from gymkirchenfeld/master
Ignoring SessionLock/Unlock (suspend and/or lock/unlock) to prevent false redscreens
2 parents 47a7ccb + e980cf6 commit d492746

8 files changed

Lines changed: 59 additions & 9 deletions

File tree

SafeExamBrowser.Client.UnitTests/Responsibilities/MonitoringResponsibilityTests.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.Linq;
1212
using Microsoft.VisualStudio.TestTools.UnitTesting;
13+
using Microsoft.Win32;
1314
using Moq;
1415
using SafeExamBrowser.Client.Contracts;
1516
using SafeExamBrowser.Client.Responsibilities;
@@ -363,7 +364,7 @@ public void SystemMonitor_MustShowLockScreenOnSessionSwitch()
363364
.Setup(f => f.CreateLockScreen(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IEnumerable<LockScreenOption>>(), It.IsAny<LockScreenSettings>()))
364365
.Returns(lockScreen.Object);
365366

366-
sentinel.Raise(s => s.SessionChanged += null);
367+
sentinel.Raise(s => s.SessionChanged += null, SessionSwitchReason.ConsoleDisconnect);
367368

368369
coordinator.Verify(c => c.RequestSessionLock(), Times.Once);
369370
coordinator.Verify(c => c.ReleaseSessionLock(), Times.Once);
@@ -385,7 +386,7 @@ public void SystemMonitor_MustTerminateIfRequestedByUser()
385386
.Callback(new Action<string, string, IEnumerable<LockScreenOption>, LockScreenSettings>((message, title, options, settings) => result.OptionId = options.Last().Id))
386387
.Returns(lockScreen.Object);
387388

388-
sentinel.Raise(s => s.SessionChanged += null);
389+
sentinel.Raise(s => s.SessionChanged += null, SessionSwitchReason.ConsoleConnect);
389390

390391
coordinator.Verify(c => c.RequestSessionLock(), Times.Once);
391392
coordinator.Verify(c => c.ReleaseSessionLock(), Times.Once);
@@ -406,9 +407,42 @@ public void SystemMonitor_MustDoNothingIfSessionSwitchAllowed()
406407
.Setup(f => f.CreateLockScreen(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IEnumerable<LockScreenOption>>(), It.IsAny<LockScreenSettings>()))
407408
.Returns(lockScreen.Object);
408409

409-
sentinel.Raise(s => s.SessionChanged += null);
410+
sentinel.Raise(s => s.SessionChanged += null, SessionSwitchReason.ConsoleConnect);
410411

411412
lockScreen.Verify(l => l.Show(), Times.Never);
412413
}
414+
415+
[TestMethod]
416+
public void SystemMonitor_MustDoNothingIfSessionLockUnlockWithoutService()
417+
{
418+
var lockScreen = new Mock<ILockScreen>();
419+
420+
settings.Service.IgnoreService = true;
421+
lockScreen.Setup(l => l.WaitForResult()).Returns(new LockScreenResult());
422+
uiFactory
423+
.Setup(f => f.CreateLockScreen(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IEnumerable<LockScreenOption>>(), It.IsAny<LockScreenSettings>()))
424+
.Returns(lockScreen.Object);
425+
426+
sentinel.Raise(s => s.SessionChanged += null, SessionSwitchReason.SessionLock);
427+
428+
lockScreen.Verify(l => l.Show(), Times.Never);
429+
}
430+
431+
[TestMethod]
432+
public void SystemMonitor_MustShowLockscreenIfNotSessionLockUnlockWithoutService()
433+
{
434+
var lockScreen = new Mock<ILockScreen>();
435+
436+
settings.Service.IgnoreService = true;
437+
coordinator.Setup(c => c.RequestSessionLock()).Returns(true);
438+
lockScreen.Setup(l => l.WaitForResult()).Returns(new LockScreenResult());
439+
uiFactory
440+
.Setup(f => f.CreateLockScreen(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IEnumerable<LockScreenOption>>(), It.IsAny<LockScreenSettings>()))
441+
.Returns(lockScreen.Object);
442+
443+
sentinel.Raise(s => s.SessionChanged += null, SessionSwitchReason.ConsoleConnect);
444+
445+
lockScreen.Verify(l => l.Show(), Times.Once);
446+
}
413447
}
414448
}

SafeExamBrowser.Client/Responsibilities/MonitoringResponsibility.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System;
1010
using System.Collections.Generic;
1111
using System.Linq;
12+
using Microsoft.Win32;
1213
using SafeExamBrowser.Client.Contracts;
1314
using SafeExamBrowser.I18n.Contracts;
1415
using SafeExamBrowser.Logging.Contracts;
@@ -251,18 +252,24 @@ private void Sentinel_EaseOfAccessChanged(SentinelEventArgs args)
251252
}
252253
}
253254

254-
private void Sentinel_SessionChanged()
255+
private void Sentinel_SessionChanged(SessionSwitchReason reason)
255256
{
257+
256258
var allow = !Settings.Service.IgnoreService && (!Settings.Service.DisableUserLock || !Settings.Service.DisableUserSwitch);
257259
var disable = Settings.Security.DisableSessionChangeLockScreen;
258-
260+
var isSessionLockEvent = reason == SessionSwitchReason.SessionLock;
261+
var isSessionUnlockEvent = reason == SessionSwitchReason.SessionUnlock;
259262
if (allow || disable)
260263
{
261264
Logger.Info($"Detected user session change, but {(allow ? "session locking and/or switching is allowed" : "lock screen is deactivated")}.");
262265
}
266+
else if (Settings.Service.IgnoreService && (isSessionLockEvent || isSessionUnlockEvent))
267+
{
268+
Logger.Info($"Detected user session {(isSessionLockEvent ? "lock" : "unlock")}, ignoring!");
269+
}
263270
else
264271
{
265-
var message = text.Get(TextKey.LockScreen_UserSessionMessage);
272+
var message = text.Get(Settings.Service.IgnoreService ? TextKey.LockScreen_UserSwitchMessage : TextKey.LockScreen_UserSessionMessage);
266273
var title = text.Get(TextKey.LockScreen_Title);
267274
var continueOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_UserSessionContinueOption) };
268275
var terminateOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_UserSessionTerminateOption) };

SafeExamBrowser.I18n.Contracts/TextKey.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public enum TextKey
9090
LockScreen_UnlockButton,
9191
LockScreen_UserSessionContinueOption,
9292
LockScreen_UserSessionMessage,
93+
LockScreen_UserSwitchMessage,
9394
LockScreen_UserSessionTerminateOption,
9495
LogWindow_AlwaysOnTop,
9596
LogWindow_AutoScroll,

SafeExamBrowser.I18n/Data/de.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@
228228
<Entry key="LockScreen_UserSessionMessage">
229229
Der aktive Benutzer hat sich geändert oder der Computer wurde gesperrt! Bitte wählen Sie eine der verfügbaren Optionen aus und geben Sie das korrekte Passwort ein, um SEB zu entsperren.
230230
</Entry>
231+
<Entry key="LockScreen_UserSwitchMessage">
232+
Der aktive Benutzer hat sich geändert! Bitte wählen Sie eine der verfügbaren Optionen aus und geben Sie das korrekte Passwort ein, um SEB zu entsperren.
233+
</Entry>
231234
<Entry key="LockScreen_UserSessionTerminateOption">
232235
Safe Exam Browser beenden. WARNUNG: Sie werden keine Möglichkeit haben, Daten zu speichern oder weitere Aktionen auszuführen, SEB wird sofort beendet!
233236
</Entry>

SafeExamBrowser.I18n/Data/en.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@
228228
<Entry key="LockScreen_UserSessionMessage">
229229
The active user has changed or the computer has been locked! In order to unlock SEB, please select one of the available options and enter the correct unlock password.
230230
</Entry>
231+
<Entry key="LockScreen_UserSwitchMessage">
232+
The active user has changed. In order to unlock SEB, please select one of the available options and enter the correct unlock password.
233+
</Entry>
231234
<Entry key="LockScreen_UserSessionTerminateOption">
232235
Terminate Safe Exam Browser. WARNING: There will be no possibility to save data or perform any further actions, the shutdown will be initiated immediately!
233236
</Entry>

SafeExamBrowser.Monitoring.Contracts/System/Events/SessionChangedEventHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
88

9+
using Microsoft.Win32;
10+
911
namespace SafeExamBrowser.Monitoring.Contracts.System.Events
1012
{
1113
/// <summary>
1214
/// Indicates that the active user session of the operating system has changed.
1315
/// </summary>
14-
public delegate void SessionChangedEventHandler();
16+
public delegate void SessionChangedEventHandler(SessionSwitchReason reason);
1517
}

SafeExamBrowser.Monitoring/System/Components/SystemEvents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
8181
private void SystemEvents_SessionChanged(object sender, SessionSwitchEventArgs e)
8282
{
8383
logger.Info($"User session change detected: {e.Reason}.");
84-
Task.Run(() => SessionChanged?.Invoke());
84+
Task.Run(() => SessionChanged?.Invoke(e.Reason));
8585
}
8686

8787
private void SystemEvents_TimeChanged(object sender, EventArgs e)

SafeExamBrowser.Monitoring/System/SystemSentinel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void StartMonitoringStickyKeys()
7070

7171
public void StartMonitoringSystemEvents()
7272
{
73-
systemEvents.SessionChanged += () => SessionChanged?.Invoke();
73+
systemEvents.SessionChanged += (reason) => SessionChanged?.Invoke(reason);
7474
systemEvents.StartMonitoring();
7575
}
7676

0 commit comments

Comments
 (0)