Skip to content

Commit b96a8a7

Browse files
committed
better custom security manager, less janky application restarts, Java debug configuration, clearing up README terminology
1 parent 5f9dfa0 commit b96a8a7

14 files changed

Lines changed: 279 additions & 78 deletions

FlashpointSecurePlayer/CustomSecurityManager.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ int InternetInterfaces.IInternetSecurityManager.GetSecuritySite(out IntPtr pSite
6464
int InternetInterfaces.IInternetSecurityManager.MapUrlToZone([MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, ref uint pdwZone, uint dwFlags) {
6565
// behave like local intranet
6666
pdwZone = 1;
67+
68+
if ((dwFlags & MUTZ_ISFILE) == MUTZ_ISFILE) {
69+
return INET_E_DEFAULT_ACTION;
70+
}
71+
72+
if (pwszUrl == null) {
73+
return E_INVALIDARG;
74+
}
75+
76+
if ((dwFlags & MUTZ_DONT_UNESCAPE) != MUTZ_DONT_UNESCAPE) {
77+
try {
78+
pwszUrl = Uri.UnescapeDataString(pwszUrl);
79+
} catch (ArgumentNullException) {
80+
return INET_E_DEFAULT_ACTION;
81+
}
82+
}
83+
84+
pwszUrl = pwszUrl.ToLower();
85+
86+
if (pwszUrl.IndexOf("http://") != 0 && pwszUrl.IndexOf("https://") != 0 && pwszUrl.IndexOf("ftp://") != 0) {
87+
// we've wandered off from Flashpoint Server, revert to default zone settings
88+
return INET_E_DEFAULT_ACTION;
89+
}
6790
return S_OK;
6891
}
6992

@@ -74,6 +97,24 @@ int InternetInterfaces.IInternetSecurityManager.GetSecurityId([MarshalAs(Unmanag
7497
int InternetInterfaces.IInternetSecurityManager.ProcessUrlAction([MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, uint dwAction, out uint pPolicy, uint cbPolicy, byte pContext, uint cbContext, uint dwFlags, uint dwReserved) {
7598
pPolicy = URLPOLICY_DISALLOW;
7699

100+
if ((dwFlags & PUAF_ISFILE) == PUAF_ISFILE) {
101+
return INET_E_DEFAULT_ACTION;
102+
}
103+
104+
if (pwszUrl == null) {
105+
return E_INVALIDARG;
106+
}
107+
108+
pwszUrl = pwszUrl.ToLower();
109+
110+
if (pwszUrl.IndexOf("http://") != 0 && pwszUrl.IndexOf("https://") != 0 && pwszUrl.IndexOf("ftp://") != 0) {
111+
// we've wandered off from Flashpoint Server, don't allow zone elevation
112+
if (dwAction == URLACTION_FEATURE_ZONE_ELEVATION) {
113+
return S_OK;
114+
}
115+
return INET_E_DEFAULT_ACTION;
116+
}
117+
77118
if (dwAction == URLACTION_ACTIVEX_TREATASUNTRUSTED || // trust ActiveX Controls always
78119
dwAction == URLACTION_HTML_MIXED_CONTENT || // block HTTPS content on HTTP websites for Flashpoint Proxy
79120
dwAction == URLACTION_CLIENT_CERT_PROMPT || // don't allow invalid certificates
@@ -112,6 +153,7 @@ int InternetInterfaces.IInternetSecurityManager.ProcessUrlAction([MarshalAs(Unma
112153
dwAction == URLACTION_MANAGED_SIGNED || // run components regardless of if they're signed or not
113154
dwAction == URLACTION_MANAGED_UNSIGNED ||
114155
dwAction == URLACTION_DOTNET_USERCONTROLS || // allow .NET user controls
156+
dwAction == URLACTION_FEATURE_ZONE_ELEVATION || // allow entering this zone from about:blank
115157
dwAction == URLACTION_FEATURE_DATA_BINDING || // allow databinding
116158
dwAction == URLACTION_FEATURE_CROSSDOMAIN_FOCUS_CHANGE || // allow crossdomain
117159
dwAction == URLACTION_ALLOW_RESTRICTEDPROTOCOLS || // allow active content regardless of if the protocol is restricted

FlashpointSecurePlayer/EnvironmentVariables.cs

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,28 @@
1313

1414
namespace FlashpointSecurePlayer {
1515
class EnvironmentVariables : Modifications {
16-
const string COMPATIBILITY_LAYER = "__COMPAT_LAYER";
16+
const string COMPATIBILITY_LAYER_NAME = "__COMPAT_LAYER";
1717

1818
public EnvironmentVariables(Form Form) : base(Form) { }
1919

2020
public void Activate(string name, string server, string applicationMutexName) {
2121
base.Activate(name);
2222
ModificationsElement modificationsElement = GetModificationsElement(true, Name);
23+
string value = null;
24+
string compatibilityLayerValue = null;
25+
26+
try {
27+
compatibilityLayerValue = Environment.GetEnvironmentVariable(COMPATIBILITY_LAYER_NAME);
28+
} catch (ArgumentException) {
29+
throw new EnvironmentVariablesFailedException();
30+
} catch (SecurityException) {
31+
throw new TaskRequiresElevationException();
32+
}
33+
34+
if (compatibilityLayerValue != null) {
35+
compatibilityLayerValue = compatibilityLayerValue.ToUpper();
36+
}
37+
2338
EnvironmentVariablesElement environmentVariablesElement = null;
2439

2540
for (int i = 0;i < modificationsElement.EnvironmentVariables.Count;i++) {
@@ -29,39 +44,57 @@ public void Activate(string name, string server, string applicationMutexName) {
2944
throw new EnvironmentVariablesFailedException();
3045
}
3146

32-
string compatibilityLayer = null;
47+
value = environmentVariablesElement.Value;
3348

3449
try {
35-
compatibilityLayer = Environment.GetEnvironmentVariable(COMPATIBILITY_LAYER);
50+
Environment.SetEnvironmentVariable(environmentVariablesElement.Name, RemoveVariablesFromValue(value) as string);
3651
} catch (ArgumentException) {
3752
throw new EnvironmentVariablesFailedException();
3853
} catch (SecurityException) {
3954
throw new TaskRequiresElevationException();
4055
}
4156

42-
try {
43-
Environment.SetEnvironmentVariable(environmentVariablesElement.Name, RemoveVariablesFromValue(environmentVariablesElement.Value) as string);
44-
} catch (ArgumentException) {
45-
throw new EnvironmentVariablesFailedException();
46-
} catch (SecurityException) {
47-
throw new TaskRequiresElevationException();
57+
if (value != null) {
58+
value = value.ToUpper();
4859
}
4960

50-
if (environmentVariablesElement.Name == COMPATIBILITY_LAYER && String.IsNullOrEmpty(compatibilityLayer) && !String.IsNullOrEmpty(server)) {
51-
RestartApplication(false, Form, applicationMutexName);
52-
throw new InvalidModificationException();
61+
// if this is the compatibility layer variable
62+
// and the value is not what we want to set it to
63+
// and we're in server mode...
64+
if (environmentVariablesElement.Name == COMPATIBILITY_LAYER_NAME && value != compatibilityLayerValue && !String.IsNullOrEmpty(server)) {
65+
throw new CompatibilityLayersException();
5366
}
5467
}
5568
}
5669

57-
new public void Deactivate() {
70+
public void Deactivate(string server) {
5871
base.Deactivate();
72+
73+
if (String.IsNullOrEmpty(Name)) {
74+
return;
75+
}
76+
5977
ModificationsElement modificationsElement = GetModificationsElement(false, Name);
6078

6179
if (modificationsElement == null) {
6280
return;
6381
}
6482

83+
string value = null;
84+
string compatibilityLayerValue = null;
85+
86+
try {
87+
compatibilityLayerValue = Environment.GetEnvironmentVariable(COMPATIBILITY_LAYER_NAME);
88+
} catch (ArgumentException) {
89+
throw new EnvironmentVariablesFailedException();
90+
} catch (SecurityException) {
91+
throw new TaskRequiresElevationException();
92+
}
93+
94+
if (compatibilityLayerValue != null) {
95+
compatibilityLayerValue = compatibilityLayerValue.ToUpper();
96+
}
97+
6598
EnvironmentVariablesElement environmentVariablesElement = null;
6699

67100
for (int i = 0;i < modificationsElement.EnvironmentVariables.Count;i++) {
@@ -71,12 +104,23 @@ public void Activate(string name, string server, string applicationMutexName) {
71104
throw new EnvironmentVariablesFailedException();
72105
}
73106

74-
try {
75-
Environment.SetEnvironmentVariable(environmentVariablesElement.Name, null);
76-
} catch (ArgumentException) {
77-
throw new EnvironmentVariablesFailedException();
78-
} catch (SecurityException) {
79-
throw new TaskRequiresElevationException();
107+
value = environmentVariablesElement.Value;
108+
109+
if (value != null) {
110+
value = value.ToUpper();
111+
}
112+
113+
// if this isn't the compatibility layer variable
114+
// or the value isn't what we want to set it to
115+
// or we're not in server mode...
116+
if (environmentVariablesElement.Name != COMPATIBILITY_LAYER_NAME || value != compatibilityLayerValue && String.IsNullOrEmpty(server)) {
117+
try {
118+
Environment.SetEnvironmentVariable(environmentVariablesElement.Name, null);
119+
} catch (ArgumentException) {
120+
throw new EnvironmentVariablesFailedException();
121+
} catch (SecurityException) {
122+
throw new TaskRequiresElevationException();
123+
}
80124
}
81125
}
82126
}

FlashpointSecurePlayer/FlashpointSecurePlayer.cs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ private void ShowError(string errorLabelText) {
7575
this.errorLabel.Text = errorLabelText;
7676
}
7777

78-
private void AskLaunchInAdministratorMode() {
79-
if (!TestProcessRunningAsAdministrator()) {
78+
private void AskLaunchAsAdministratorUser() {
79+
if (!TestLaunchedAsAdministratorUser()) {
8080
// popup message box and restart program here
8181
// https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox?view=netframework-4.8
8282
/*
@@ -89,7 +89,7 @@ then there'd be no dialog except this one - and I don't want
8989
the program to enter an infinite restart loop
9090
*/
9191
ShowOutput();
92-
DialogResult dialogResult = MessageBox.Show(Properties.Resources.LaunchInAdministratorMode, Properties.Resources.FlashpointSecurePlayer, MessageBoxButtons.YesNo, MessageBoxIcon.None);
92+
DialogResult dialogResult = MessageBox.Show(String.Format(Properties.Resources.LaunchGame, Properties.Resources.AsAdministratorUser), Properties.Resources.FlashpointSecurePlayer, MessageBoxButtons.YesNo, MessageBoxIcon.None);
9393

9494
if (dialogResult == DialogResult.No) {
9595
Application.Exit();
@@ -101,7 +101,20 @@ the program to enter an infinite restart loop
101101
}
102102

103103
// we're already running as admin?
104-
ShowError(Properties.Resources.GameFailedAdministratorMode);
104+
ShowError(String.Format(Properties.Resources.GameFailedLaunch, Properties.Resources.AsAdministratorUser));
105+
throw new InvalidModificationException();
106+
}
107+
108+
private void AskLaunchWithCompatibilitySettings() {
109+
ShowOutput();
110+
DialogResult dialogResult = MessageBox.Show(String.Format(Properties.Resources.LaunchGame, Properties.Resources.WithCompatibilitySettings), Properties.Resources.FlashpointSecurePlayer, MessageBoxButtons.YesNo, MessageBoxIcon.None);
111+
112+
if (dialogResult == DialogResult.No) {
113+
Application.Exit();
114+
throw new InvalidModificationException();
115+
}
116+
117+
RestartApplication(false, this, APPLICATION_MUTEX_NAME);
105118
throw new InvalidModificationException();
106119
}
107120

@@ -164,7 +177,7 @@ private async Task ActivateModificationsAsync(string commandLine, ErrorDelegate
164177
} catch (System.Configuration.ConfigurationErrorsException) {
165178
errorDelegate(Properties.Resources.ConfigurationFailedLoad);
166179
} catch (TaskRequiresElevationException) {
167-
AskLaunchInAdministratorMode();
180+
AskLaunchAsAdministratorUser();
168181
}
169182

170183
if (modificationsElement.ModeTemplates.ServerModeTemplate.ElementInformation.IsPresent || modificationsElement.ModeTemplates.SoftwareModeTemplate.ElementInformation.IsPresent) {
@@ -175,7 +188,7 @@ private async Task ActivateModificationsAsync(string commandLine, ErrorDelegate
175188
} catch (System.Configuration.ConfigurationErrorsException) {
176189
errorDelegate(Properties.Resources.ConfigurationFailedLoad);
177190
} catch (TaskRequiresElevationException) {
178-
AskLaunchInAdministratorMode();
191+
AskLaunchAsAdministratorUser();
179192
}
180193
}
181194

@@ -187,7 +200,9 @@ private async Task ActivateModificationsAsync(string commandLine, ErrorDelegate
187200
} catch (System.Configuration.ConfigurationErrorsException) {
188201
errorDelegate(Properties.Resources.ConfigurationFailedLoad);
189202
} catch (TaskRequiresElevationException) {
190-
AskLaunchInAdministratorMode();
203+
AskLaunchAsAdministratorUser();
204+
} catch (CompatibilityLayersException) {
205+
AskLaunchWithCompatibilitySettings();
191206
}
192207
}
193208

@@ -209,7 +224,7 @@ private async Task ActivateModificationsAsync(string commandLine, ErrorDelegate
209224
} catch (System.Configuration.ConfigurationErrorsException) {
210225
errorDelegate(Properties.Resources.ConfigurationFailedLoad);
211226
} catch (TaskRequiresElevationException) {
212-
AskLaunchInAdministratorMode();
227+
AskLaunchAsAdministratorUser();
213228
}
214229
}
215230

@@ -219,7 +234,7 @@ private async Task ActivateModificationsAsync(string commandLine, ErrorDelegate
219234
} catch (InvalidModificationException ex) {
220235
throw ex;
221236
} catch (TaskRequiresElevationException) {
222-
AskLaunchInAdministratorMode();
237+
AskLaunchAsAdministratorUser();
223238
} catch {
224239
errorDelegate(Properties.Resources.UnknownProcessCompatibilityConflict);
225240
}
@@ -242,17 +257,19 @@ private async Task DeactivateModificationsAsync(ErrorDelegate errorDelegate) {
242257
} catch (System.Configuration.ConfigurationErrorsException) {
243258
errorDelegate(Properties.Resources.ConfigurationFailedLoad);
244259
} catch (TaskRequiresElevationException) {
245-
AskLaunchInAdministratorMode();
260+
AskLaunchAsAdministratorUser();
246261
}
247262

248263
try {
249-
EnvironmentVariables.Deactivate();
264+
EnvironmentVariables.Deactivate(Server);
250265
} catch (EnvironmentVariablesFailedException) {
251266
errorDelegate(Properties.Resources.EnvironmentVariablesFailed);
252267
} catch (System.Configuration.ConfigurationErrorsException) {
253268
errorDelegate(Properties.Resources.ConfigurationFailedLoad);
254269
} catch (TaskRequiresElevationException) {
255-
AskLaunchInAdministratorMode();
270+
AskLaunchAsAdministratorUser();
271+
} catch (CompatibilityLayersException) {
272+
AskLaunchWithCompatibilitySettings();
256273
}
257274

258275
try {
@@ -278,12 +295,9 @@ private async Task StartSecurePlayback() {
278295
throw new InvalidModificationException();
279296
}
280297

281-
//this.ShowInTaskbar = true;
282-
//this.WindowState = FormWindowState.Normal;
283-
284298
// this requires admin
285-
if (!TestProcessRunningAsAdministrator()) {
286-
AskLaunchInAdministratorMode();
299+
if (!TestLaunchedAsAdministratorUser()) {
300+
AskLaunchAsAdministratorUser();
287301
}
288302

289303
ResetProgressBar();
@@ -341,7 +355,7 @@ private async Task StartSecurePlayback() {
341355
return;
342356
} catch (TaskRequiresElevationException) {
343357
// we're already running as admin?
344-
ShowError(Properties.Resources.GameFailedAdministratorMode);
358+
ShowError(String.Format(Properties.Resources.GameFailedLaunch, Properties.Resources.AsAdministratorUser));
345359
return;
346360
} catch (InvalidOperationException) {
347361
ShowError(Properties.Resources.RegistryBackupAlreadyRunning);
@@ -535,13 +549,15 @@ private async void FlashpointSecurePlayer_Load(object sender, EventArgs e) {
535549
}
536550
} catch (TaskRequiresElevationException) {
537551
try {
538-
AskLaunchInAdministratorMode();
552+
AskLaunchAsAdministratorUser();
539553
} catch (InvalidModificationException) {
540554
Application.Exit();
541555
return;
542556
}
543557
}
544558

559+
BringToFront();
560+
Activate();
545561
ShowOutput(Properties.Resources.RequiredComponentsAreUnloading);
546562

547563
string arg = null;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<configSections>
4+
<section name="flashpointSecurePlayer" type="FlashpointSecurePlayer.Shared+FlashpointSecurePlayerSection, FlashpointSecurePlayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
5+
</configSections>
6+
7+
<flashpointSecurePlayer>
8+
<modifications>
9+
<modification name="javadebug">
10+
<modeTemplates>
11+
<softwareModeTemplate hideWindow="false">
12+
<regexes>
13+
<regex name="^\s*(?:(&quot;[^\\&quot;]*(?:\\&quot;[^\\&quot;]*)*&quot;?)\s*(.*)$)?([^\\&quot;\s]*(?:\\&quot;[^\\&quot;\s]*)*)\s*(.*)$" replace="Java\JDK_1.8.0_181\bin\appletviewer.exe -J-Dhttp.proxyHost=127.0.0.1 -J-Dhttp.proxyPort=22500 -J-Dhttps.proxyHost=127.0.0.1 -J-Dhttps.proxyPort=22500 -J-Dftp.proxyHost=127.0.0.1 -J-Dftp.proxyPort=22500 -J-DsocksProxyHost=127.0.0.1 -J-DsocksProxyPort= -J-Xbootclasspath/a:..\jre\lib\deploy.jar;..\jre\lib\javaws.jar;..\jre\lib\plugin.jar $2$4 $1$3" />
14+
</regexes>
15+
</softwareModeTemplate>
16+
</modeTemplates>
17+
</modification>
18+
</modifications>
19+
</flashpointSecurePlayer>
20+
<startup>
21+
22+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
23+
</configuration>

0 commit comments

Comments
 (0)