Skip to content

Commit

Permalink
Disable script injection when dynamic build loading is disabled.
Browse files Browse the repository at this point in the history
Fixed a bug where earth prayers was not a recognized attribute.
  • Loading branch information
Alexandru Macocian committed Apr 23, 2021
1 parent bc66ca9 commit 1d774ab
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
14 changes: 11 additions & 3 deletions Daybreak/Controls/ChromiumBrowserWrapper.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ private async Task InitializeBrowser()
this.WebBrowser.WebMessageReceived += this.CoreWebView2_WebMessageReceived;
this.WebBrowser.CoreWebView2.Settings.AreDevToolsEnabled = false;
this.WebBrowser.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
await this.WebBrowser.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(Scripts.AlterContextMenu);
if (this.CanDownloadBuild)
{
await this.WebBrowser.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(Scripts.SendSelectionOnContextMenu);
}
}
}

Expand Down Expand Up @@ -211,12 +214,17 @@ private void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessa
if (payload?.Key == BrowserPayload.PayloadKeys.ContextMenu)
{
var contextMenuPayload = args.WebMessageAsJson.Deserialize<BrowserPayload<OnContextMenuPayload>>();
if (this.CanDownloadBuild is false)
var maybeTemplate = contextMenuPayload.Value.Selection;
if (string.IsNullOrWhiteSpace(maybeTemplate))
{
return;
}

if (this.buildTemplateManager.IsTemplate(maybeTemplate) is false)
{
return;
}

var maybeTemplate = contextMenuPayload.Value.Selection;
Task.Run(() =>
{
try
Expand Down
8 changes: 4 additions & 4 deletions Daybreak/Daybreak.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
<LangVersion>preview</LangVersion>
<ApplicationIcon>Daybreak.ico</ApplicationIcon>
<Version>0.7.1</Version>
<Version>0.7.2</Version>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.32" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.774.44" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.33" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.818.41" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Slim" Version="1.2.1" />
<PackageReference Include="System.Windows.Interactivity.WPF" Version="2.0.20525" />
<PackageReference Include="SystemExtensions.NetStandard" Version="1.1.3" />
<PackageReference Include="SystemExtensions.NetStandard" Version="1.1.4" />
<PackageReference Include="WCL" Version="1.0.2" />
<PackageReference Include="WpfExtended" Version="0.2.0" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Daybreak/Models/Builds/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public sealed class Attribute
Motivation,
Leadership,
ScytheMastery,
EarthPrayers,
WindPrayers,
EarthMagic,
Mysticism
Expand Down
15 changes: 15 additions & 0 deletions Daybreak/Services/BuildTemplates/BuildTemplateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ public BuildTemplateManager(
this.logger = logger.ThrowIfNull(nameof(logger));
}

public bool IsTemplate(string template)
{
if (template.IsNullOrWhiteSpace())
{
return false;
}

if (template.Where(c => DecodingLookupTable.Contains(c) is false).Any())
{
return false;
}

return true;
}

public BuildEntry CreateBuild()
{
var emptyBuild = new Build();
Expand Down
1 change: 1 addition & 0 deletions Daybreak/Services/BuildTemplates/IBuildTemplateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Daybreak.Services.BuildTemplates
{
public interface IBuildTemplateManager
{
bool IsTemplate(string template);
BuildEntry CreateBuild();
BuildEntry CreateBuild(string name);
void SaveBuild(BuildEntry buildEntry);
Expand Down
16 changes: 10 additions & 6 deletions Daybreak/Utils/Scripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
{
public static class Scripts
{
public const string AlterContextMenu = @"
public const string SendSelectionOnContextMenu = @"
document.addEventListener('contextmenu', function (event)
{
var text = '';
if (window.getSelection)
var activeEl = document.activeElement;
var activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;
if ((activeElTagName == 'textarea') || (activeElTagName == 'input' &&
/^(?:text|search|password|tel|url)$/i.test(activeEl.type)) &&
(typeof activeEl.selectionStart == 'number'))
{
text = window.getSelection().toString();
}
else if (document.selection && document.selection.type != 'Control')
text = activeEl.value.slice(activeEl.selectionStart, activeEl.selectionEnd);
}
else if (window.getSelection)
{
text = document.selection.createRange().text;
text = window.getSelection().toString();
}
let jsonObject =
Expand Down

0 comments on commit 1d774ab

Please sign in to comment.