QtWebEngine: "Not allowed to load local resource" for iframe, how to disable web security?

I am migrating my application from WebKit to WebEngine (it seems that it is better for rendering angular-basad html). I ran into a problem that I cannot enable QtWebEngine to load a local iframe, despite the fact that I set all the possible settings that I found:

Code from mainwindow.cpp

view->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
view->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
view->page()->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);

view->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
view->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
view->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);

The simplest example is to take a FancyBrowser based on WebEngine (\ Examples \ Qt-5.4 \ webenginewidgets \ fancybrowser) and try to load the local html file into it as follows:

Index.html:

<html>
<head>
    <title>Hi there</title>
</head>
<body>
    This is a page
    a simple page
    <iframe id="some_idrame" width="0" height="0" style="border: none" src="some_iframe.html" name="target" sandbox="allow-scripts"></iframe>
</body>
</html>

some_iframe.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>La-la-la</title>
</head>
<body>
    Lalala 
</body>
</html>

If you configured env var QTWEBENGINE_REMOTE_DEBUGGING on some port, you can open 127.0.0.1:port and see this error in the console:

"Not allowed to load local resource".

, ... - WebEngine - "--disable-web-security"...

!

+4
2

- . Qt, QWebEngineView :

void WebEngineView::setLocalHtml(const QString &html)
{
    if(html.isEmpty())
    {
        setHtml(QString());
        return;
    }

    // Save html to a local file
    QString filePath;
    {
        QTemporaryFile tempFile(QDir::toNativeSeparators(QDir::tempPath() + "/ehr_temp.XXXXXX.html"));
        tempFile.setAutoRemove(false);
        tempFile.open();
        QTextStream out(&tempFile);
        out << html;

        filePath = tempFile.fileName();
    }

    // delete the file after it has been loaded
    QMetaObject::Connection * const conn = new QMetaObject::Connection;
    *conn = connect(this, &WebEngineView::loadFinished, [filePath, conn](){
        disconnect(*conn);
        delete conn;

        QFile::remove(filePath);
    });

    load(QUrl::fromLocalFile(filePath));
}

- , CORS.

0

Source: https://habr.com/ru/post/1616021/


All Articles