<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>249872</bug_id>
          
          <creation_ts>2022-12-24 10:15:57 -0800</creation_ts>
          <short_desc>Crash in PDFDocument::injectStyleAndContentScript when downloading PDF</short_desc>
          <delta_ts>2024-04-12 10:49:02 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebKitGTK</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=272590</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          <blocked>235969</blocked>
          <everconfirmed>1</everconfirmed>
          <reporter name="Michael Catanzaro">mcatanzaro</reporter>
          <assigned_to name="Michael Catanzaro">mcatanzaro</assigned_to>
          <cc>bugs-noreply</cc>
    
    <cc>mcatanzaro</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1921828</commentid>
    <comment_count>0</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2022-12-24 10:15:57 -0800</bug_when>
    <thetext>With WebKitGTK 2.39.3, open a PDF in PDF.js and try to save it using the download button. It will crash:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f999ad8cffd in WTF::RefCountedBase::ref (this=&lt;optimized out&gt;) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/_builddir/WTF/Headers/wtf/RefCounted.h:49
49	        ++m_refCount;
[Current thread is 1 (Thread 0x7f9991edc600 (LWP 2))]
(gdb) bt
#0  0x00007f999ad8cffd in WTF::RefCountedBase::ref() const (this=&lt;optimized out&gt;)
    at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/_builddir/WTF/Headers/wtf/RefCounted.h:49
#1  WTF::Ref&lt;WebCore::EventListener, WTF::RawPtrTraits&lt;WebCore::EventListener&gt; &gt;::Ref(WebCore::EventListener&amp;)
    (object=..., this=0x7ffc3fbffd00)
    at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/_builddir/WTF/Headers/wtf/Ref.h:67
#2  WTF::Ref&lt;WebCore::EventListener, WTF::RawPtrTraits&lt;WebCore::EventListener&gt; &gt;::copyRef() const &amp;
    (this=&lt;optimized out&gt;) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/_builddir/WTF/Headers/wtf/Ref.h:125
#3  WebCore::tryAddEventListener (listener=..., options=..., eventType=..., targetNode=0x7f97fe09fbc0)
    at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/dom/Node.cpp:2194
#4  WebCore::Node::addEventListener(WTF::AtomString const&amp;, WTF::Ref&lt;WebCore::EventListener, WTF::RawPtrTraits&lt;WebCore::EventListener&gt; &gt;&amp;&amp;, WebCore::AddEventListenerOptions const&amp;)
    (this=this@entry=0x7f97fe09fbc0, eventType=..., listener=..., options=...)
    at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/dom/Node.cpp:2229
#5  0x00007f999b009f4d in WebCore::PDFDocument::injectStyleAndContentScript() (this=0x7f9936121800)
    at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/html/PDFDocument.cpp:237
#6  0x00007f999ad60236 in WebCore::EventTarget::innerInvokeEventListeners(WebCore::Event&amp;, WTF::Vector&lt;WTF::RefPtr&lt;WebCore::RegisteredEventListener, WTF::RawPtrTraits&lt;WebCore::RegisteredEventListener&gt;, WTF::DefaultRefDerefTraits&lt;WebCore::RegisteredEventListener&gt; &gt;, 1ul, WTF::CrashOnOverflow, 2ul, WTF::FastMalloc&gt;, WebCore::EventTarget::EventInvokePhase)
    (this=this@entry=0x7f97fe002000, event=..., listeners=..., phase=phase@entry=WebCore::EventTarget::EventInvokePhase::Bubbling) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/dom/EventTarget.cpp:369
#7  0x00007f999ad609d8 in WebCore::EventTarget::fireEventListeners(WebCore::Event&amp;, WebCore::EventTarget::EventInvokePhase) (this=0x7f97fe002000, event=..., phase=WebCore::EventTarget::EventInvokePhase::Bubbling)
    at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/dom/EventTarget.cpp:301
#8  0x00007f999ad60e1f in WebCore::EventContext::handleLocalEvents(WebCore::Event&amp;, WebCore::EventTarget::EventInvokePhase) const (this=&lt;optimized out&gt;, event=&lt;optimized out&gt;, phase=&lt;optimized out&gt;)
    at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/dom/EventContext.cpp:96
#9  0x00007f999ad615af in WebCore::dispatchEventInDOM(WebCore::Event&amp;, WebCore::EventPath const&amp;)
    (event=..., path=...) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/dom/EventDispatcher.cpp:109
#10 0x00007f999ad653fd in WebCore::EventDispatcher::dispatchEvent(WebCore::Node&amp;, WebCore::Event&amp;)
     (node=..., event=...) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/dom/EventDispatcher.cpp:190
#11 0x00007f999b2aa63f in WebCore::FrameLoader::PolicyChecker::checkNavigationPolicy(WebCore::ResourceRequest&amp;&amp;, WebCore::ResourceResponse const&amp;, WebCore::DocumentLoader*, WTF::RefPtr&lt;WebCore::FormState, WTF::RawPtrTraits&lt;WebCore::FormState&gt;, WTF::DefaultRefDerefTraits&lt;WebCore::FormState&gt; &gt;&amp;&amp;, WTF::CompletionHandler&lt;void (WebCore::ResourceRequest&amp;&amp;, WTF::WeakPtr&lt;WebCore::FormState, WTF::DefaultWeakPtrImpl&gt;&amp;&amp;, WebCore::NavigationPolicyDecision)&gt;&amp;&amp;, WebCore::PolicyDecisionMode)
     (this=this@entry=0x7f998a2110c0, request=..., redirectResponse=..., loader=loader@entry=0x7f99362a5000, formState=..., function=..., policyDecisionMode=&lt;optimized out&gt;)
    at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/_builddir/WTF/Headers/wtf/RawPtrTraits.h:44
#12 0x00007f999b284379 in WebCore::FrameLoader::loadWithDocumentLoader(WebCore::DocumentLoader*, WebCore::FrameLoadType, WTF::RefPtr&lt;WebCore::FormState, WTF::RawPtrTraits&lt;WebCore::FormState&gt;, WTF::DefaultRefDerefTraits&lt;WebCore::FormState&gt; &gt;&amp;&amp;, WebCore::AllowNavigationToInvalidURL, WTF::CompletionHandler&lt;void ()&gt;&amp;&amp;)
    (this=0x7f998a029ba0, loader=0x7f99362a5000, type=&lt;optimized out&gt;, formState=&lt;optimized out&gt;, allowNavigationToInvalidURL=&lt;optimized out&gt;, completionHandler=&lt;optimized out&gt;)
    at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/loader/FrameLoader.cpp:1682
#13 0x00007f999b2849d8 in WebCore::FrameLoader::loadWithNavigationAction(WebCore::ResourceRequest const&amp;, WebCore::NavigationAction&amp;&amp;, WebCore::FrameLoadType, WTF::RefPtr&lt;WebCore::FormState, WTF::RawPtrTraits&lt;WebCore::FormState&gt;, WTF::DefaultRefDerefTraits&lt;WebCore::FormState&gt; &gt;&amp;&amp;, WebCore::AllowNavigationToInvalidURL, WebCore::ShouldTreatAsContinuingLoad, WTF::CompletionHandler&lt;void ()&gt;&amp;&amp;)
    (this=this@entry=0x7f998a029ba0, request=..., action=..., type=type@entry=WebCore::FrameLoadType::Standard, formState=..., allowNavigationToInvalidURL=allowNavigationToInvalidURL@entry=WebCore::AllowNavigationToInvalidURL::Yes, shouldTreatAsContinuingLoad=&lt;optimized out&gt;, completionHandler=&lt;optimized out&gt;)
    at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/_builddir/WTF/Headers/wtf/RawPtrTraits.h:44
#14 0x00007f999b2854a7 in WebCore::FrameLoader::loadURL(WebCore::FrameLoadRequest&amp;&amp;, WTF::String const&amp;, WebCore::FrameLoadType, WebCore::Event*, WTF::RefPtr&lt;WebCore::FormState, WTF::RawPtrTraits&lt;WebCore::FormState&gt;, WTF::DefaultRefDere--Type &lt;RET&gt; for more, q to quit, c to continue without paging--c
fTraits&lt;WebCore::FormState&gt; &gt;&amp;&amp;, std::optional&lt;WebCore::PrivateClickMeasurement&gt;&amp;&amp;, WTF::CompletionHandler&lt;void ()&gt;&amp;&amp;) (this=0x7f998a029ba0, frameLoadRequest=..., referrer=&lt;optimized out&gt;, newLoadType=&lt;optimized out&gt;, event=&lt;optimized out&gt;, formState=&lt;optimized out&gt;, privateClickMeasurement=&lt;optimized out&gt;, completionHandler=&lt;optimized out&gt;) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/loader/FrameLoader.cpp:1463
#15 0x00007f999b286cef in WebCore::FrameLoader::loadFrameRequest(WebCore::FrameLoadRequest&amp;&amp;, WebCore::Event*, WTF::RefPtr&lt;WebCore::FormState, WTF::RawPtrTraits&lt;WebCore::FormState&gt;, WTF::DefaultRefDerefTraits&lt;WebCore::FormState&gt; &gt;&amp;&amp;, std::optional&lt;WebCore::PrivateClickMeasurement&gt;&amp;&amp;) (this=0x7f998a029ba0, request=..., event=&lt;optimized out&gt;, formState=&lt;optimized out&gt;, privateClickMeasurement=&lt;optimized out&gt;) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/_builddir/WTF/Headers/wtf/ThreadAssertions.h:119
#16 0x00007f999b28713a in WebCore::FrameLoader::changeLocation(WebCore::FrameLoadRequest&amp;&amp;, WebCore::Event*, std::optional&lt;WebCore::PrivateClickMeasurement&gt;&amp;&amp;) (this=this@entry=0x7f998a029ba0, frameRequest=..., triggeringEvent=triggeringEvent@entry=0x7f97fe09f710, privateClickMeasurement=...) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/loader/FrameLoader.cpp:462
#17 0x00007f999b287545 in WebCore::FrameLoader::changeLocation(WTF::URL const&amp;, WTF::AtomString const&amp;, WebCore::Event*, WebCore::ReferrerPolicy const&amp;, WebCore::ShouldOpenExternalURLsPolicy, std::optional&lt;WebCore::NewFrameOpenerPolicy&gt;, WTF::AtomString const&amp;, WebCore::SystemPreviewInfo const&amp;, std::optional&lt;WebCore::PrivateClickMeasurement&gt;&amp;&amp;) (this=this@entry=0x7f998a029ba0, url=..., passedTarget=..., triggeringEvent=triggeringEvent@entry=0x7f97fe09f710, referrerPolicy=@0x7ffc3fc01faf: WebCore::ReferrerPolicy::EmptyString, shouldOpenExternalURLsPolicy=&lt;optimized out&gt;, openerPolicy=std::optional&lt;WebCore::NewFrameOpenerPolicy&gt; = {...}, downloadAttribute=&lt;optimized out&gt;, systemPreviewInfo=&lt;optimized out&gt;, privateClickMeasurement=&lt;optimized out&gt;) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/loader/FrameLoader.cpp:447
#18 0x00007f999af23914 in WebCore::HTMLAnchorElement::handleClick(WebCore::Event&amp;) (this=0x7f97fe04a9c0, event=...) at /usr/include/c++/12.1.0/bits/refwrap.h:346
#19 0x00007f999ad65236 in WebCore::callDefaultEventHandlersInBubblingOrder (path=..., event=...) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/dom/EventDispatcher.cpp:64
#20 WebCore::EventDispatcher::dispatchEvent(WebCore::Node&amp;, WebCore::Event&amp;) (node=..., event=...) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/dom/EventDispatcher.cpp:206
#21 0x00007f999adcce7b in WebCore::simulateMouseEvent(WTF::AtomString const&amp;, WebCore::Element&amp;, WebCore::Event*, WebCore::SimulatedClickSource) (eventType=..., element=..., underlyingEvent=0x0, source=WebCore::SimulatedClickSource::Bindings) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/_builddir/WTF/Headers/wtf/Ref.h:143
#22 0x00007f999add07ea in WebCore::simulateClick(WebCore::Element&amp;, WebCore::Event*, WebCore::SimulatedClickMouseEventOptions, WebCore::SimulatedClickVisualOptions, WebCore::SimulatedClickSource) (element=..., underlyingEvent=underlyingEvent@entry=0x0, mouseEventOptions=mouseEventOptions@entry=WebCore::SendNoEvents, visualOptions=visualOptions@entry=WebCore::DoNotShowPressedLook, creationOptions=creationOptions@entry=WebCore::SimulatedClickSource::Bindings) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/dom/SimulatedClick.cpp:105
#23 0x00007f999af3b872 in WebCore::HTMLElement::click() (this=&lt;optimized out&gt;) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/html/HTMLElement.cpp:689
#24 0x00007f999a13f5d9 in operator() (__closure=&lt;optimized out&gt;) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/_builddir/WebCore/DerivedSources/JSHTMLElement.cpp:4400
#25 WebCore::toJS&lt;WebCore::IDLUndefined, WebCore::jsHTMLElementPrototypeFunction_clickBody(JSC::JSGlobalObject*, JSC::CallFrame*, IDLOperation&lt;JSHTMLElement&gt;::ClassParameter)::&lt;lambda()&gt; &gt; (valueOrFunctor=&lt;optimized out&gt;, throwScope=&lt;synthetic pointer&gt;..., lexicalGlobalObject=&lt;optimized out&gt;) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/bindings/js/JSDOMConvertBase.h:165
#26 WebCore::jsHTMLElementPrototypeFunction_clickBody (castedThis=&lt;optimized out&gt;, callFrame=&lt;optimized out&gt;, lexicalGlobalObject=&lt;optimized out&gt;) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/_builddir/WebCore/DerivedSources/JSHTMLElement.cpp:4400
#27 WebCore::IDLOperation&lt;WebCore::JSHTMLElement&gt;::call&lt;WebCore::jsHTMLElementPrototypeFunction_clickBody&gt; (operationName=&lt;optimized out&gt;, callFrame=&lt;optimized out&gt;, lexicalGlobalObject=&lt;optimized out&gt;) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/WebCore/bindings/js/JSDOMOperation.h:63
#28 WebCore::jsHTMLElementPrototypeFunction_click(JSC::JSGlobalObject*, JSC::CallFrame*) (lexicalGlobalObject=&lt;optimized out&gt;, callFrame=&lt;optimized out&gt;) at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/_builddir/WebCore/DerivedSources/JSHTMLElement.cpp:4405
#29 0x00007f9938008038 in  ()
#30 0x00007ffc3fc02820 in  ()
#31 0x00007f99971bb76a in op_call_slow_return_location () at /usr/lib/debug/source/sdk/webkitgtk-6.0.bst/Source/JavaScriptCore/llint/LowLevelInterpreter.asm:1179
#32 0x0000000000000000 in  ()</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1925464</commentid>
    <comment_count>1</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2023-01-13 15:25:17 -0800</bug_when>
    <thetext>Can&apos;t reproduce anymore. Don&apos;t know what changed.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1931854</commentid>
    <comment_count>2</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2023-02-07 15:00:23 -0800</bug_when>
    <thetext>Reopening because this crash is reproducible for some websites but not others:

Crash: https://dor.mo.gov/forms/MO-1040%20Instructions_2022.pdf

No crash: https://www.irs.gov/pub/irs-pdf/i1040gi.pdf</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2027889</commentid>
    <comment_count>3</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2024-04-11 14:17:11 -0700</bug_when>
    <thetext>So the problem here is PDFDocument::injectStyleAndContentScript expects to be called only once per PDFDocument and releases the PDFDocument&apos;s reference to the PDFDocumentEventListener. But the PDFDocumentEventListener is still valid even though PDFDocument::m_listener is null, and it calls PDFDocument::injectStyleAndContentScript a second time for the download. This results in a crash because the function assumes m_listener is never null (because it will never be null the first time it is called).

I&apos;m still trying to figure out how to fix it.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2027901</commentid>
    <comment_count>4</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2024-04-11 14:52:26 -0700</bug_when>
    <thetext>Well I can at least make it not crash, but I&apos;m not sure how to make the download actually work. It&apos;s getting blocked by CSP:

[Error] Refused to load blob:webkit-pdfjs-viewer://pdfjs/39176f55-4f67-4515-9733-dc0f6d764506 because it does not appear in the frame-src directive of the Content Security Policy.

Not sure why this happens for https://dor.mo.gov/forms/MO-1040%20Instructions_2022.pdf but not for https://www.irs.gov/pub/irs-pdf/i1040gi.pdf</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2027928</commentid>
    <comment_count>5</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2024-04-11 17:27:37 -0700</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/27174</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2027933</commentid>
    <comment_count>6</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2024-04-11 17:37:05 -0700</bug_when>
    <thetext>The download failure might be related to bug #264355. Possibly the website&apos;s content security policy is blocking webkit-pdfjs-viewer://?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2028122</commentid>
    <comment_count>7</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2024-04-12 10:49:00 -0700</bug_when>
    <thetext>Committed 277434@main (96e3800242ca): &lt;https://commits.webkit.org/277434@main&gt;

Reviewed commits have been landed. Closing PR #27174 and removing active labels.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>