Files
cefsharp/CefSharp/JavascriptBinding/JavascriptBindingSettings.cs
luca-sonntag 00d57a555c Javascript Binding - Finalize JavascriptBindingApiAllowOrigins: Per-browser isolation and origin normalization (#5218)
* Javascript Binding - Add Ability to limit origins

* Move JS binding settings to CefBrowserWrapper and optimize origin validation

* Initialize origins to null

* Normalize allowed origins

* Optimize origin validation

* Add null check for origin compare

* Fix JavaScript Binding API settings for popups

Move the JS binding API configuration block out of the !browser->IsPopup() scope
in OnBrowserCreated. This ensures that IsJavascriptBindingApiAllowed() enforces
configured restrictions for popups as well.

* Fix possible null deref for JavascriptBindingApiAllowOrigins

In IsJavascriptBindingApiAllowed, added a null check for JavascriptBindingApiAllowOrigins
to prevent a potential null dereference if JavascriptBindingApiHasAllowOrigins is true
but the allowOrigins list itself was null.

* Simplify origin normalization and add null check

* Add more test cases

* Refactor IsJavascriptBindingApiAllowed signature

Refactored IsJavascriptBindingApiAllowed to accept a CefBrowserWrapper^ parameter directly, eliminating redundant lookups and improving clarity. Updated all call sites to pass the browser wrapper explicitly.

* Clarify summary for HasJavascriptBindingApiAllowOrigins

Clarify documentation for HasJavascriptBindingApiAllowOrigins method to specify zero or more origins

* Simplify origin comparison logic in CefAppUnmanagedWrapper

Refactored code to directly compare wide string origins using
_wcsicmp, removing unnecessary pointer casts and null checks.
This streamlines the logic and improves readability.

* Add tests for JavaScript binding API behavior across cross-origin navigation

---------

Co-authored-by: amaitland <307872+amaitland@users.noreply.github.com>
Co-authored-by: Luca Sonntag <luca.sonntag@cgm.com>
2026-03-22 07:35:54 +10:00

142 lines
5.6 KiB
C#

// Copyright © 2020 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System.Collections.Generic;
using CefSharp.Internals;
using System;
namespace CefSharp.JavascriptBinding
{
/// <summary>
/// Javascript Binding Settings
/// </summary>
public class JavascriptBindingSettings : FreezableBase
{
private bool alwaysInterceptAsynchronously;
private bool legacyBindingEnabled;
private string jsBindingGlobalObjectName;
private bool jsBindingApiEnabled = true;
private string[] javascriptBindingApiAllowOrigins;
/// <summary>
/// The Javascript methods that CefSharp provides in relation to JavaScript Binding are
/// created using a Global (window) Object. Settings this property allows you to disable
/// the creation of this object. Features like EvaluateScriptAsPromiseAsync that rely on
/// the creation of this object will no longer function.
/// </summary>
public bool JavascriptBindingApiEnabled
{
get { return jsBindingApiEnabled; }
set
{
ThrowIfFrozen();
jsBindingApiEnabled = value;
}
}
/// <summary>
/// When <see cref="JavascriptBindingApiEnabled"/> is set to true, set a collection
/// of origins to limit which origins CefSharp will create it's global (window) object.
/// </summary>
/// <remarks>
/// If you wish to create the CefSharp object for a limited set of origins then set this property
/// </remarks>
public string[] JavascriptBindingApiAllowOrigins
{
get { return javascriptBindingApiAllowOrigins; }
set
{
ThrowIfFrozen();
if (value != null)
{
javascriptBindingApiAllowOrigins = Array.ConvertAll(
value,
origin => origin?.TrimEnd('/')
);
}
else
{
javascriptBindingApiAllowOrigins = null;
}
}
}
/// <summary>
/// The Javascript methods that CefSharp provides in relation to JavaScript Binding are
/// created using a Global (window) Object. Settings this property allows you to customise
/// the name of this object. If not specified then both cefSharp and CefSharp objects will
/// be created e.g. cefSharp.bindObjectAsync, CefSharp.BindObjectAsync.
/// If specified then your custom name will be used, if the name starts with a lowercase letter
/// then all the functions will be lowercase, e.g. myObjName.bindObjectAsync otherwise
/// the functions will start with a uppercase letter e.g. MyObjName.BindObjectAsync
/// </summary>
/// <remarks>
/// This object is also accessible through the window property. e.g. window.cefSharp.bindObjectAsync
/// </remarks>
public string JavascriptBindingApiGlobalObjectName
{
get { return jsBindingGlobalObjectName; }
set
{
ThrowIfFrozen();
if (!StringCheck.IsLettersAndNumbers(value))
{
//TODO: See if there's a better suited Exception class for this.
throw new System.Exception("invalid or illegal characters used for binding property names. Alphanumeric and underscores characters only.");
}
jsBindingGlobalObjectName = value;
}
}
/// <summary>
/// Objects registered using <see cref="IJavascriptObjectRepository.Register"/>
/// will be automatically bound when a V8Context is created. (Soon as the Javascript
/// context is created for a browser). This behaviour is like that seen with Javascript
/// Binding in version 57 and earlier.
/// </summary>
public bool LegacyBindingEnabled
{
get { return legacyBindingEnabled; }
set
{
ThrowIfFrozen();
legacyBindingEnabled = value;
}
}
/// <summary>
/// When using an <see cref="CefSharp.ModelBinding.IAsyncMethodInterceptor"/>
/// the <see cref="CefSharp.ModelBinding.IAsyncMethodInterceptor.InterceptAsync(System.Func{object[], object}, object[], string)"/>
/// method is call for all methods (the default is to call InterceptAsync only for methods that return a Task).
/// This only applies when <see cref="BindingOptions.MethodInterceptor"/> is of type <see cref="CefSharp.ModelBinding.IAsyncMethodInterceptor"/>
/// </summary>
public bool AlwaysInterceptAsynchronously
{
get { return alwaysInterceptAsynchronously; }
set
{
ThrowIfFrozen();
alwaysInterceptAsynchronously = value;
}
}
/// <summary>
/// Indicates whether <see cref="JavascriptBindingApiAllowOrigins"/> has been set with zero or more origins.
/// </summary>
/// <returns>true if <see cref="JavascriptBindingApiAllowOrigins"/> is a non-empty collection, otherwise false.</returns>
public bool HasJavascriptBindingApiAllowOrigins()
{
if (javascriptBindingApiAllowOrigins == null)
return false;
return javascriptBindingApiAllowOrigins.Length > 0;
}
}
}