-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add auth token query parameter to HTTP requests of webview assets
- Loading branch information
Showing
3 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
plugin-core/src/main/java/appland/webviews/webserver/WebviewAuthTokenRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package appland.webviews.webserver; | ||
|
||
import com.intellij.openapi.util.text.StringUtil; | ||
import com.intellij.util.Urls; | ||
import org.cef.browser.CefBrowser; | ||
import org.cef.browser.CefFrame; | ||
import org.cef.handler.CefRequestHandlerAdapter; | ||
import org.cef.handler.CefResourceRequestHandler; | ||
import org.cef.handler.CefResourceRequestHandlerAdapter; | ||
import org.cef.misc.BoolRef; | ||
import org.cef.network.CefRequest; | ||
import org.jetbrains.builtInWebServer.BuiltInWebServerKt; | ||
import org.jetbrains.ide.BuiltInServerManager; | ||
|
||
/** | ||
* Adds auth tokens to requests to webview resources to bypass any filtering of the built-in webserver. | ||
* Without auth tokens the IDE's built-in webserver only accepts URLs prefixed with a project name. | ||
*/ | ||
public final class WebviewAuthTokenRequestHandler extends CefRequestHandlerAdapter { | ||
@Override | ||
public CefResourceRequestHandler getResourceRequestHandler(CefBrowser browser, | ||
CefFrame frame, | ||
CefRequest request, | ||
boolean isNavigation, | ||
boolean isDownload, | ||
String requestInitiator, | ||
BoolRef disableDefaultHandling) { | ||
|
||
if (isUnsignedWebViewRequest(request)) { | ||
return new CefResourceRequestHandlerAdapter() { | ||
@Override | ||
public boolean onBeforeResourceLoad(CefBrowser browser, CefFrame frame, CefRequest request) { | ||
var url = Urls.parseEncoded(request.getURL()); | ||
if (url != null) { | ||
request.setURL(BuiltInServerManager.getInstance().addAuthToken(url).toExternalForm()); | ||
} | ||
|
||
// false to continue with the request | ||
return false; | ||
} | ||
}; | ||
} | ||
|
||
// fallback to default handling of JCEF | ||
return null; | ||
} | ||
|
||
/** | ||
* @return true if the request is for a webview asset, but not yet signed with an auth token | ||
*/ | ||
private static boolean isUnsignedWebViewRequest(CefRequest request) { | ||
var url = Urls.parseEncoded(request.getURL()); | ||
var params = StringUtil.defaultIfEmpty(url != null ? url.getParameters() : null, ""); | ||
return request.getURL().startsWith(AppMapWebview.getBaseUrlWithPath()) | ||
&& !params.contains("?" + BuiltInWebServerKt.TOKEN_PARAM_NAME + "=") | ||
&& !params.contains("&" + BuiltInWebServerKt.TOKEN_PARAM_NAME + "="); | ||
} | ||
} |