Skip to content

Javascript Binding - Finalize JavascriptBindingApiAllowOrigins: Per-browser isolation and origin normalization#5218

Draft
luca-sonntag wants to merge 8 commits intocefsharp:feature/jsbindingalloworiginlistfrom
luca-sonntag:feature/jsbindingalloworiginlist-completion
Draft

Javascript Binding - Finalize JavascriptBindingApiAllowOrigins: Per-browser isolation and origin normalization#5218
luca-sonntag wants to merge 8 commits intocefsharp:feature/jsbindingalloworiginlistfrom
luca-sonntag:feature/jsbindingalloworiginlist-completion

Conversation

@luca-sonntag
Copy link

@luca-sonntag luca-sonntag commented Feb 27, 2026

Fixes:
#5001

Summary:
This PR completes and optimizes the implementation of the JavascriptBindingApiAllowOrigins feature. The primary focus is shifting binding settings from a global state to a per-browser scope for better isolation and introducing robust origin normalization to ensure consistent security validation across different URL formats.

Changes:

  • Per-Browser Settings Isolation:
    • Relocated JavaScript binding settings (JavascriptBindingApiEnabled, JavascriptBindingApiAllowOrigins, etc.) from the global CefAppUnmanagedWrapper to CefBrowserWrapper. This allows each browser instance to have its own independent list of allowed origins.
  • Origin Normalization (C#):
    • Updated JavascriptBindingSettings.cs to automatically trim trailing slashes from strings added to the JavascriptBindingApiAllowOrigins list, preventing common configuration errors.
  • Optimized C++ Validation Logic:
    • Centralized the security check into CefAppUnmanagedWrapper::IsJavascriptBindingApiAllowed.
    • Implemented normalization in the browser subprocess to strip trailing slashes from the frame origin string before comparison.
    • Switched to case-insensitive comparison (_wcsicmp) to align with standard origin matching behavior.
    • Integrated null checks for origin string pointers to improve stability.

How Has This Been Tested?

  • Executed unit tests in CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs
  • Verified that JavascriptBindingApiAllowOrigins correctly blocks or allows the cefSharp object based on the frame's URL.
  • Confirmed that settings changed in one browser instance do not affect other instances (testing the move to CefBrowserWrapper).
  • OS: Windows.

Screenshots (if appropriate): N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Updated documentation

Checklist:

  • Tested the code(if applicable)
  • Commented my code
  • Changed the documentation(if applicable)
  • New files have a license disclaimer
  • The formatting is consistent with the project (project supports .editorconfig)

Summary by CodeRabbit

Release Notes

  • Enhancements
    • Added origin-based access control for the JavaScript binding API; frames now validate whether their origin is permitted before binding setup.
    • Implemented URL origin normalization (trailing slash handling) for consistent origin matching across comparisons.
    • Improved security validation to ensure the JavaScript binding API is enabled and the current frame's origin is authorized.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 27, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The changes consolidate JavaScript binding API configuration from CefAppUnmanagedWrapper to CefBrowserWrapper, and implement origin-based access validation for JavaScript bound objects. The system now checks if a frame's origin is in the allowed list before enabling bindings, with proper origin normalization across C# and C++ layers.

Changes

Cohort / File(s) Summary
CefBrowserWrapper property exposure
CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h
Added three new public properties (JavascriptBindingApiEnabled, JavascriptBindingApiHasAllowOrigins, JavascriptBindingApiAllowOrigins) backed by a private _javascriptBindingApiAllowOrigins field, with initialization in constructor and cleanup in destructor.
CefAppUnmanagedWrapper refactoring
CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp, CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h
Moved JavaScript binding API configuration from local members to CefBrowserWrapper. Removed three private data members. Added new IsJavascriptBindingApiAllowed(CefRefPtr<CefFrame>) method that validates frame origin against allowed origins with URL parsing, normalization, and case-insensitive comparison. Updated OnContextCreated to use browser wrapper configuration and origin validation before binding setup.
Origin normalization
CefSharp/JavascriptBinding/JavascriptBindingSettings.cs
Updated JavascriptBindingApiAllowOrigins setter to normalize origins by trimming trailing slashes, ensuring consistent comparison across C# and C++ layers.

Sequence Diagram

sequenceDiagram
    participant JS as JavaScript<br/>Context Creation
    participant CefApp as CefAppUnmanagedWrapper<br/>(OnContextCreated)
    participant BrowserWrap as CefBrowserWrapper
    participant Validator as IsJavascriptBinding<br/>ApiAllowed
    participant BindingObj as Binding Object<br/>Creation

    JS->>CefApp: OnContextCreated(frame)
    CefApp->>BrowserWrap: Retrieve browser wrapper
    CefApp->>BrowserWrap: Check JavascriptBindingApiEnabled
    alt API Enabled
        CefApp->>Validator: IsJavascriptBindingApiAllowed(frame)
        Validator->>BrowserWrap: Get JavascriptBindingApiHasAllowOrigins
        alt Has Allow Origins Configured
            Validator->>Validator: Parse & normalize frame origin
            Validator->>BrowserWrap: Get JavascriptBindingApiAllowOrigins
            Validator->>Validator: Compare origin (case-insensitive)
            alt Origin Matches
                Validator-->>CefApp: true
            else Origin Not Allowed
                Validator-->>CefApp: false
            end
        else No Origins Configured
            Validator-->>CefApp: true (allow all)
        end
        alt Origin Allowed
            CefApp->>BindingObj: Create & assign to global scope
            BindingObj-->>JS: Binding available
        else Origin Not Allowed
            CefApp-->>JS: No binding
        end
    else API Disabled
        CefApp-->>JS: Skip binding
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • PR #5197 — Both PRs modify CefAppUnmanagedWrapper and CefBrowserWrapper to change how per-frame JavaScript binding state is stored and accessed, touching the same OnContextCreated code paths.

Poem

🐰 Origins checked with careful care,
Trailing slashes trimmed with flair,
Browser wrapper holds the keys,
JavaScript bindings put at ease,
Security bounds now in place!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The PR title clearly and specifically describes the main changes: moving JavaScript binding settings to per-browser scope and implementing origin normalization, which are the core objectives of this changeset.
Description check ✅ Passed The PR description is comprehensive and well-structured, covering all template sections including fixes, summary, changes, testing details, and appropriate checklist items.
Linked Issues check ✅ Passed The PR successfully implements the third proposed solution from issue #5001 by introducing allowed domains settings in JavascriptBindingSettings and enforcing validation at the browser level [#5001].
Out of Scope Changes check ✅ Passed All changes are directly related to the stated objectives: moving binding settings to per-browser scope, adding origin normalization, and implementing centralized validation logic align with issue #5001 requirements.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@luca-sonntag luca-sonntag changed the title Feature/jsbindingalloworiginlist completion Javascript Binding - Finalize JavascriptBindingApiAllowOrigins: Per-browser isolation and origin normalization Feb 27, 2026
@AppVeyorBot
Copy link

Build CefSharp 136.1.40-CI5434 failed (commit c7297af541 by @)

@luca-sonntag luca-sonntag marked this pull request as draft February 27, 2026 13:34
@luca-sonntag luca-sonntag marked this pull request as ready for review March 2, 2026 07:54
@luca-sonntag
Copy link
Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 2, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp`:
- Around line 348-370: The code currently checks browserWrapper and
JavascriptBindingApiHasAllowOrigins but then dereferences
browserWrapper->JavascriptBindingApiAllowOrigins (used with GetSize/GetAt) which
can be null; add a local smart-pointer/local reference like allowOrigins =
browserWrapper->JavascriptBindingApiAllowOrigins right after the existing flag
check, guard it with if (!allowOrigins.get()) { return false; } (fail closed)
and then replace subsequent direct uses of
browserWrapper->JavascriptBindingApiAllowOrigins with allowOrigins when calling
GetSize/GetAt; keep the existing frameUrl/frameUrlOrigin logic intact.
- Around line 103-113: The JS binding API flags are only being set when the
wrapper is created for non-popup browsers, leaving popup wrappers with defaults
(Enabled=true, HasAllowOrigins=false) which makes
IsJavascriptBindingApiAllowed() return true for popups; move the block that
reads and assigns wrapper->JavascriptBindingApiEnabled,
wrapper->JavascriptBindingApiHasAllowOrigins and the
wrapper->JavascriptBindingApiAllowOrigins population (the
extraInfo->GetBool/GetList logic) out of the if (!browser->IsPopup()) scope so
these settings are always applied to every CefAppUnmanagedWrapper instance,
ensuring IsJavascriptBindingApiAllowed() enforces configured restrictions for
popups as well.

In `@CefSharp/JavascriptBinding/JavascriptBindingSettings.cs`:
- Around line 52-57: The normalization currently calls origin.EndsWith(...)
inside the setter and will throw if any element of the incoming string[] value
is null; update the mapping used to populate javascriptBindingApiAllowOrigins so
it guards against null entries (e.g., map null to null or to an empty string) by
checking origin == null before calling EndsWith, and otherwise apply the
existing trimming logic; modify the conversion that uses Array.ConvertAll (the
lambda that references origin) to perform that null check to avoid
NullReferenceException.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9aeb5cb and 5d445bb.

📒 Files selected for processing (4)
  • CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp
  • CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h
  • CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h
  • CefSharp/JavascriptBinding/JavascriptBindingSettings.cs

Comment on lines 103 to 113
wrapper->JavascriptBindingApiEnabled = extraInfo->GetBool("JavascriptBindingApiEnabled");
wrapper->JavascriptBindingApiHasAllowOrigins = extraInfo->GetBool("JavascriptBindingApiHasAllowOrigins");

if (_jsBindingApiHasAllowOrigins)
if (wrapper->JavascriptBindingApiHasAllowOrigins)
{
auto allowOrigins = extraInfo->GetList("JavascriptBindingApiAllowOrigins");
if (allowOrigins.get() && allowOrigins->IsValid())
{
_jsBindingApiAllowOrigins = allowOrigins->Copy();
wrapper->JavascriptBindingApiAllowOrigins = allowOrigins->Copy();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

# First, let's locate and examine the file
find . -name "CefAppUnmanagedWrapper.cpp" -type f

Repository: cefsharp/CefSharp

Length of output: 121


🏁 Script executed:

# Search for the file and check its size
git ls-files | grep -i "CefAppUnmanagedWrapper"

Repository: cefsharp/CefSharp

Length of output: 176


🏁 Script executed:

# Search for the code section and broader context
rg -n "JavascriptBindingApiEnabled" --type=cpp --type=h -B 10 -A 15

Repository: cefsharp/CefSharp

Length of output: 13992


🏁 Script executed:

# Look for popup-related creation and IsPopup checks
rg -n "IsPopup\|OnBeforePopup" --type=cpp --type=h -B 3 -A 5

Repository: cefsharp/CefSharp

Length of output: 43


🏁 Script executed:

# Find CefBrowserWrapper class definition and defaults
rg -n "class CefBrowserWrapper|JavascriptBindingApiEnabled\s*=" --type=cpp --type=h -B 2 -A 2

Repository: cefsharp/CefSharp

Length of output: 2067


🏁 Script executed:

# Read larger context around lines 103-113 in CefAppUnmanagedWrapper.cpp
sed -n '75,130p' ./CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp

Repository: cefsharp/CefSharp

Length of output: 3062


🏁 Script executed:

# Find the OnBrowserCreated function to see full context
rg -n "OnBrowserCreated" --type=cpp -B 5 -A 40 | head -100

Repository: cefsharp/CefSharp

Length of output: 10877


🏁 Script executed:

# Look for the if (!browser->IsPopup()) block
rg -n "IsPopup" ./CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp -B 5 -A 5

Repository: cefsharp/CefSharp

Length of output: 519


🏁 Script executed:

# Check IsJavascriptBindingApiAllowed implementation
rg -n "IsJavascriptBindingApiAllowed" --type=cpp -A 20

Repository: cefsharp/CefSharp

Length of output: 7643


🏁 Script executed:

# Check if popups receive extraInfo with JS binding keys from parent process
rg -n "OnBeforePopup" --type=cpp -B 5 -A 20 | head -80

Repository: cefsharp/CefSharp

Length of output: 8706


🏁 Script executed:

# Check CefRenderProcessHandler to understand OnBrowserCreated contract
rg -n "CefRenderProcessHandler\|OnBrowserCreated" --type=cpp --type=h | head -20

Repository: cefsharp/CefSharp

Length of output: 43


🏁 Script executed:

# Verify if popups get extraInfo passed in OnBrowserCreated
rg -n "extraInfo" ./CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp -B 2 -A 2

Repository: cefsharp/CefSharp

Length of output: 1941


Popup browsers can bypass origin restrictions with current assignment scope.

Lines 103–113 set JavascriptBindingApi* properties only inside the if (!browser->IsPopup()) block. Popup wrappers retain constructor defaults (Enabled=true, HasAllowOrigins=false). In IsJavascriptBindingApiAllowed() (line 348–351), when HasAllowOrigins=false, the function returns true unconditionally, allowing popup contexts to bypass origin restrictions regardless of parent settings.

Move the JS binding API settings outside the popup check to apply configured restrictions to all browser wrappers:

Suggested fix
             if (!browser->IsPopup())
             {
                 _legacyBindingEnabled = extraInfo->GetBool("LegacyBindingEnabled");

                 if (_legacyBindingEnabled)
                 {
                     auto objects = extraInfo->GetList("LegacyBindingObjects");
                     if (objects.get() && objects->IsValid())
                     {
                         auto javascriptObjects = DeserializeJsObjects(objects, 0);

                         for each (JavascriptObject ^ obj in Enumerable::OfType<JavascriptObject^>(javascriptObjects))
                         {
                             if (_javascriptObjects->ContainsKey(obj->JavascriptName))
                             {
                                 _javascriptObjects->Remove(obj->JavascriptName);
                             }
                             _javascriptObjects->Add(obj->JavascriptName, obj);
                         }
                     }
                 }

+            wrapper->JavascriptBindingApiEnabled = extraInfo->GetBool("JavascriptBindingApiEnabled");
+            wrapper->JavascriptBindingApiHasAllowOrigins = extraInfo->GetBool("JavascriptBindingApiHasAllowOrigins");
+
+            if (wrapper->JavascriptBindingApiHasAllowOrigins)
+            {
+                auto allowOrigins = extraInfo->GetList("JavascriptBindingApiAllowOrigins");
+                if (allowOrigins.get() && allowOrigins->IsValid())
+                {
+                    wrapper->JavascriptBindingApiAllowOrigins = allowOrigins->Copy();
+                }
+            }
+
+            if (!browser->IsPopup())
+            {
                 if (extraInfo->HasKey("JsBindingPropertyName") || extraInfo->HasKey("JsBindingPropertyNameCamelCase"))
                 {
                     _jsBindingPropertyName = extraInfo->GetString("JsBindingPropertyName");
                     _jsBindingPropertyNameCamelCase = extraInfo->GetString("JsBindingPropertyNameCamelCase");
                 }
             }
-
-            wrapper->JavascriptBindingApiEnabled = extraInfo->GetBool("JavascriptBindingApiEnabled");
-            wrapper->JavascriptBindingApiHasAllowOrigins = extraInfo->GetBool("JavascriptBindingApiHasAllowOrigins");
-
-            if (wrapper->JavascriptBindingApiHasAllowOrigins)
-            {
-                auto allowOrigins = extraInfo->GetList("JavascriptBindingApiAllowOrigins");
-                if (allowOrigins.get() && allowOrigins->IsValid())
-                {
-                    wrapper->JavascriptBindingApiAllowOrigins = allowOrigins->Copy();
-                }
-            }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp` around lines 103
- 113, The JS binding API flags are only being set when the wrapper is created
for non-popup browsers, leaving popup wrappers with defaults (Enabled=true,
HasAllowOrigins=false) which makes IsJavascriptBindingApiAllowed() return true
for popups; move the block that reads and assigns
wrapper->JavascriptBindingApiEnabled,
wrapper->JavascriptBindingApiHasAllowOrigins and the
wrapper->JavascriptBindingApiAllowOrigins population (the
extraInfo->GetBool/GetList logic) out of the if (!browser->IsPopup()) scope so
these settings are always applied to every CefAppUnmanagedWrapper instance,
ensuring IsJavascriptBindingApiAllowed() enforces configured restrictions for
popups as well.

Comment on lines 348 to 370
if (browserWrapper == nullptr || !browserWrapper->JavascriptBindingApiHasAllowOrigins)
{
return true;
}

auto frameUrl = frame->GetURL();

CefURLParts frameUrlParts;

if (CefParseURL(frameUrl, frameUrlParts))
{
auto originStr = frameUrlParts.origin.str;
auto originLen = frameUrlParts.origin.length;

if (originLen > 0 && originStr[originLen - 1] == L'/')
{
originLen--;
}

auto frameUrlOrigin = CefString(originStr, originLen);

auto size = static_cast<int>(browserWrapper->JavascriptBindingApiAllowOrigins->GetSize());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

find . -name "CefAppUnmanagedWrapper.cpp" -type f

Repository: cefsharp/CefSharp

Length of output: 121


🏁 Script executed:

sed -n '340,380p' ./CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp | cat -n

Repository: cefsharp/CefSharp

Length of output: 1906


🏁 Script executed:

rg "JavascriptBindingApiAllowOrigins" --type cpp --type h -B 2 -A 2 | head -100

Repository: cefsharp/CefSharp

Length of output: 4816


🏁 Script executed:

sed -n '200,250p' ./CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp | cat -n

Repository: cefsharp/CefSharp

Length of output: 3388


🏁 Script executed:

rg "JavascriptBindingApiAllowOrigins\s*=" --type cpp -B 5 -A 5 | head -80

Repository: cefsharp/CefSharp

Length of output: 3230


🏁 Script executed:

rg -A 10 "JavascriptBindingApiHasAllowOrigins\s*=" ./CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp | head -30

Repository: cefsharp/CefSharp

Length of output: 587


🏁 Script executed:

rg "JavascriptBindingApiAllowOrigins" ./CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp

Repository: cefsharp/CefSharp

Length of output: 451


Add null-safety guard before dereferencing JavascriptBindingApiAllowOrigins pointer.

The flag JavascriptBindingApiHasAllowOrigins and the pointer JavascriptBindingApiAllowOrigins can become inconsistent: if the IPC payload validation fails during initialization, the flag stays true while the pointer remains nullptr. Lines 369 and 371 dereference the pointer without null checks, causing a render process crash.

Add a null check after the flag check:

if (browserWrapper == nullptr || !browserWrapper->JavascriptBindingApiHasAllowOrigins)
{
    return true;
}

auto allowOrigins = browserWrapper->JavascriptBindingApiAllowOrigins;
if (!allowOrigins.get())
{
    return false; // fail closed when allow-list is expected but unavailable
}

Then replace direct accesses to browserWrapper->JavascriptBindingApiAllowOrigins with allowOrigins.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp` around lines 348
- 370, The code currently checks browserWrapper and
JavascriptBindingApiHasAllowOrigins but then dereferences
browserWrapper->JavascriptBindingApiAllowOrigins (used with GetSize/GetAt) which
can be null; add a local smart-pointer/local reference like allowOrigins =
browserWrapper->JavascriptBindingApiAllowOrigins right after the existing flag
check, guard it with if (!allowOrigins.get()) { return false; } (fail closed)
and then replace subsequent direct uses of
browserWrapper->JavascriptBindingApiAllowOrigins with allowOrigins when calling
GetSize/GetAt; keep the existing frameUrl/frameUrlOrigin logic intact.

Comment on lines 52 to 57
if (value != null)
{
javascriptBindingApiAllowOrigins = Array.ConvertAll(
value,
origin => origin.EndsWith("/") ? origin.Substring(0, origin.Length - 1) : origin
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

find . -name "JavascriptBindingSettings.cs" -type f

Repository: cefsharp/CefSharp

Length of output: 118


🏁 Script executed:

wc -l CefSharp/JavascriptBinding/JavascriptBindingSettings.cs

Repository: cefsharp/CefSharp

Length of output: 120


🏁 Script executed:

sed -n '40,70p' CefSharp/JavascriptBinding/JavascriptBindingSettings.cs

Repository: cefsharp/CefSharp

Length of output: 1455


🏁 Script executed:

rg "JavascriptBindingApiAllowOrigins" --type csharp -B 2 -A 2

Repository: cefsharp/CefSharp

Length of output: 2575


🏁 Script executed:

grep -r "JavascriptBindingApiAllowOrigins" --include="*.cs" | head -20

Repository: cefsharp/CefSharp

Length of output: 840


🏁 Script executed:

grep -B 10 -A 5 'settings.JavascriptBindingApiAllowOrigins = origins' CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs

Repository: cefsharp/CefSharp

Length of output: 823


🏁 Script executed:

sed -n '1,100p' CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs | grep -A 20 "origins"

Repository: cefsharp/CefSharp

Length of output: 43


🏁 Script executed:

sed -n '1,150p' CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs

Repository: cefsharp/CefSharp

Length of output: 5846


🏁 Script executed:

ast-grep --pattern 'JavascriptBindingApiAllowOrigins = $_'

Repository: cefsharp/CefSharp

Length of output: 175


🏁 Script executed:

sed -n '45,70p' CefSharp/JavascriptBinding/JavascriptBindingSettings.cs

Repository: cefsharp/CefSharp

Length of output: 1116


🏁 Script executed:

rg "ConvertAll" --type csharp -B 2 -A 2

Repository: cefsharp/CefSharp

Length of output: 602


🏁 Script executed:

cat <<'EOF' > /tmp/test.cs
// Test to understand Array.ConvertAll behavior with null elements
using System;

class Test {
    static void Main() {
        string[] test = new string[] { "http://example.com/", null, "test" };
        try {
            var result = Array.ConvertAll(test, 
                origin => origin.EndsWith("/") ? origin.Substring(0, origin.Length - 1) : origin);
        } catch (NullReferenceException ex) {
            Console.WriteLine("NullReferenceException thrown: " + ex.Message);
        }
    }
}
EOF
cat /tmp/test.cs

Repository: cefsharp/CefSharp

Length of output: 563


Handle null entries in allow-origins normalization.

Line 56 dereferences origin unconditionally. The property accepts string[] which can contain null elements—a single null entry will throw a NullReferenceException at EndsWith(), breaking settings assignment.

Suggested fix
                 if (value != null)
                 {
                     javascriptBindingApiAllowOrigins = Array.ConvertAll(
                         value,
-                        origin => origin.EndsWith("/") ? origin.Substring(0, origin.Length - 1) : origin
+                        origin => origin?.TrimEnd('/')
                     );
                 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (value != null)
{
javascriptBindingApiAllowOrigins = Array.ConvertAll(
value,
origin => origin.EndsWith("/") ? origin.Substring(0, origin.Length - 1) : origin
);
if (value != null)
{
javascriptBindingApiAllowOrigins = Array.ConvertAll(
value,
origin => origin?.TrimEnd('/')
);
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@CefSharp/JavascriptBinding/JavascriptBindingSettings.cs` around lines 52 -
57, The normalization currently calls origin.EndsWith(...) inside the setter and
will throw if any element of the incoming string[] value is null; update the
mapping used to populate javascriptBindingApiAllowOrigins so it guards against
null entries (e.g., map null to null or to an empty string) by checking origin
== null before calling EndsWith, and otherwise apply the existing trimming
logic; modify the conversion that uses Array.ConvertAll (the lambda that
references origin) to perform that null check to avoid NullReferenceException.

@luca-sonntag luca-sonntag marked this pull request as draft March 2, 2026 08:17
Luca Sonntag added 3 commits March 2, 2026 11:23
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.
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.
@AppVeyorBot
Copy link

Build CefSharp 136.1.40-CI5435 failed (commit e96399376a by @)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants