Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions airflow-core/src/airflow/ui/src/pages/ReactPlugin.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*!
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { afterEach, describe, expect, it } from "vitest";

import type { ReactAppResponse } from "openapi/requests/types.gen";

import { loadPlugin } from "./ReactPlugin";

const pluginA = { bundle_url: "http://localhost/a.js", name: "PluginA" } as ReactAppResponse;
const pluginB = { bundle_url: "http://localhost/b.js", name: "PluginB" } as ReactAppResponse;
const componentA = () => null;

describe("loadPlugin", () => {
afterEach(() => {
for (const key of ["AirflowPlugin", pluginA.name, pluginB.name]) {
(globalThis as Record<string, unknown>)[key] = undefined;
}
});

it("does not let a malformed bundle inherit a previously-loaded plugin's component", async () => {
// A well-formed bundle sets globalThis.AirflowPlugin on import; a malformed one sets nothing.
await loadPlugin(pluginA, () => {
(globalThis as Record<string, unknown>).AirflowPlugin = componentA;

return Promise.resolve();
});
const { default: component } = await loadPlugin(pluginB, () => Promise.resolve());

expect(component).not.toBe(componentA);
});
});
10 changes: 8 additions & 2 deletions airflow-core/src/airflow/ui/src/pages/ReactPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ export type PluginProps = {

type PluginComponentType = FC<PluginProps>;

const loadPlugin = (reactApp: ReactAppResponse): Promise<{ default: PluginComponentType }> =>
export const loadPlugin = (
reactApp: ReactAppResponse,
importBundle: (url: string) => Promise<unknown> = (url) => import(/* @vite-ignore */ url),
): Promise<{ default: PluginComponentType }> => {
(globalThis as Record<string, unknown>).AirflowPlugin = undefined;

// We are assuming the plugin manager is trusted and the bundle_url is safe
import(/* @vite-ignore */ new URL(reactApp.bundle_url, document.baseURI).href)
return importBundle(new URL(reactApp.bundle_url, document.baseURI).href)
.then(() => {
// Store components in globalThis[reactApp.name] to avoid conflicts with the shared globalThis.AirflowPlugin
// global variable.
Expand All @@ -60,6 +65,7 @@ const loadPlugin = (reactApp: ReactAppResponse): Promise<{ default: PluginCompon

return { default: ErrorPage };
});
};

export const ReactPlugin = ({ reactApp }: { readonly reactApp: ReactAppResponse }) => {
const { dagId, mapIndex, runId, taskId } = useParams();
Expand Down
Loading