Frontend Monitoring: Only write to custom Faro endpoint if origins are the same in iframe (#95381)

* Only enable Faro if origins are the same in iframe

* Handle non-iframe case

* Remove console logs

* Use try catch to identify cross origin iframe

* Only disable transport
This commit is contained in:
Tobias Skarhed 2024-10-28 11:48:44 +01:00 committed by GitHub
parent 712974b26c
commit 187111368a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -17,6 +17,14 @@ import { EchoBackend, EchoEvent, EchoEventType } from '@grafana/runtime';
import { EchoSrvTransport } from './EchoSrvTransport';
import { GrafanaJavascriptAgentEchoEvent, User } from './types';
function isCrossOriginIframe() {
try {
return document.location.hostname !== window.parent.location.hostname;
} catch (e) {
return true;
}
}
export interface GrafanaJavascriptAgentBackendOptions extends BrowserConfig {
buildInfo: BuildInfo;
customEndpoint: string;
@ -40,7 +48,8 @@ export class GrafanaJavascriptAgentBackend
const transports: BaseTransport[] = [new EchoSrvTransport()];
if (options.customEndpoint) {
// If in cross origin iframe, default to writing to instance logging endpoint
if (options.customEndpoint && !isCrossOriginIframe()) {
transports.push(new FetchTransport({ url: options.customEndpoint, apiKey: options.apiKey }));
}