From kde-devel Thu Dec 09 14:31:40 2004 From: Luc Saillard Date: Thu, 09 Dec 2004 14:31:40 +0000 To: kde-devel Subject: [PATCH] Allow/Deny some urls to be fetch in KHTMLPart Message-Id: <200412091531.40684.luc () saillard ! org> X-MARC-Message: https://marc.info/?l=kde-devel&m=110260822210168 MIME-Version: 1 Content-Type: multipart/mixed; boundary="--Boundary-00=_MHGuBJm1/+jpbip" --Boundary-00=_MHGuBJm1/+jpbip Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, I want to be able to block some urls in Konqueror (or any application that use KHTML) like adblock do for Mozilla suite. I look into the source code of KHTML and try to see where i can put a hook to allow, or deny loading data from the web. I use the DocLoader class to deny accessing the data. I see that a security control is already in place. For this first patch, i don't want to be so intrusive. Then i declare 3 methods to add, remove, and test urls in KHTMLPart class. I want to put the urls list in the KHTMLPartPrivate class, but this class isn't initialised when plugins is loaded by Konqueror (or the KHTML class). This list is put in the private area of KHTMLPart. I want to know if this patch is correct, and can be included in the kdelibs for the next kde release. I've already a small plugin to use this new API, to show the result. Example: to block an url just do: if( !parent() || !parent()->inherits("KHTMLPart")) return; KHTMLPart *part = static_cast( parent() ); part->insertBlacklistUrl(QString("http://a.as-eu.falkag.net/dat/dlv"),false); Luc --Boundary-00=_MHGuBJm1/+jpbip Content-Type: text/x-diff; charset="us-ascii"; name="kdelibs-khtml-addfilter.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="kdelibs-khtml-addfilter.patch" Index: khtml_part.cpp =================================================================== RCS file: /home/kde/kdelibs/khtml/khtml_part.cpp,v retrieving revision 1.1058 diff -u -r1.1058 khtml_part.cpp --- khtml_part.cpp 8 Dec 2004 01:32:45 -0000 1.1058 +++ khtml_part.cpp 9 Dec 2004 13:40:21 -0000 @@ -6888,6 +6888,44 @@ d->m_bJScriptDebugEnabled = enable; } +void KHTMLPart::insertBlacklistUrl( const QString &url , bool is_regexp) +{ + QRegExp rx(url); + if (!rx.isValid()) + return; + if (!is_regexp) + rx.setWildcard(true); + kdDebug( 6050 ) << "adding this URL «" << rx.pattern() << "» to the blacklist " << endl; + m_blacklistedURLs.append(rx); +} + +void KHTMLPart::removeBlacklistUrl( const QString &url ) +{ + for ( QValueList::Iterator it = m_blacklistedURLs.begin(); it != m_blacklistedURLs.end(); ++it ) + { + if ((*it).pattern() == url) + { + m_blacklistedURLs.remove(*it); + break; + } + } +} + +bool KHTMLPart::isUrlAllowed( const KURL &url ) +{ + /* TODO: Build a cache for frequent result, to speedup lookup like a hash table ? */ + for ( QValueList::Iterator it = m_blacklistedURLs.begin(); it != m_blacklistedURLs.end(); ++it ) + { + int pos = (*it).search(url.url()); + if (pos>=0) + { + kdDebug( 6050 ) << "this URL «" << url.url() << "» is denied" << endl; + return false; + } + } + return true; +} + using namespace KParts; #include "khtml_part.moc" #include "khtmlpart_p.moc" Index: khtml_part.h =================================================================== RCS file: /home/kde/kdelibs/khtml/khtml_part.h,v retrieving revision 1.269 diff -u -r1.269 khtml_part.h --- khtml_part.h 15 Nov 2004 17:57:56 -0000 1.269 +++ khtml_part.h 9 Dec 2004 13:40:24 -0000 @@ -992,6 +992,25 @@ */ bool isModified() const; + + /** + * Insert this Url into the list of url which is denied to be fetch + * + * @param url string to be denied + * @param is_regexp @c true if this is real regexp, @c false if this is a wildcard + * @since 3.4 + */ + void insertBlacklistUrl( const QString &url , bool is_regexp); + void removeBlacklistUrl( const QString &url ); + + /** + * Checks whether this Url is autorized to be fetch by the cache loader + * + * @return @c true if the url is autorized + * @since 3.4 + */ + bool isUrlAllowed( const KURL &url ); + signals: /** * Emitted if the cursor is moved over an URL. @@ -1582,6 +1601,11 @@ void setDebugScript( bool enable ); + /** @internal + * put here because KHTMLPartPrivate, is not initialiazed when plugins is called + */ + QValueList m_blacklistedURLs; + KHTMLPartPrivate *d; friend class KHTMLPartPrivate; }; Index: misc/loader.cpp =================================================================== RCS file: /home/kde/kdelibs/khtml/misc/loader.cpp,v retrieving revision 1.178 diff -u -r1.178 loader.cpp --- misc/loader.cpp 12 Nov 2004 00:02:21 -0000 1.178 +++ misc/loader.cpp 9 Dec 2004 13:40:27 -0000 @@ -950,6 +950,8 @@ CachedImage *DocLoader::requestImage( const DOM::DOMString &url) { DOCLOADER_SECCHECK(true); + if (m_part && !m_part->isUrlAllowed(fullURL)) + return 0L; CachedImage* i = Cache::requestObject( this, fullURL, 0); @@ -963,6 +965,8 @@ const char *accept, bool userSheet ) { DOCLOADER_SECCHECK(!userSheet); + if (m_part && !m_part->isUrlAllowed(fullURL)) + return 0L; CachedCSSStyleSheet* s = Cache::requestObject( this, fullURL, accept ); if ( s ) { @@ -974,6 +978,8 @@ CachedScript *DocLoader::requestScript( const DOM::DOMString &url, const QString& charset) { DOCLOADER_SECCHECK(true); + if (m_part && !m_part->isUrlAllowed(fullURL)) + return 0L; CachedScript* s = Cache::requestObject( this, fullURL, 0 ); if ( s ) --Boundary-00=_MHGuBJm1/+jpbip Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline >> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe << --Boundary-00=_MHGuBJm1/+jpbip--