codeant-ai-for-open-source[bot] commented on code in PR #41006:
URL: https://github.com/apache/superset/pull/41006#discussion_r3405752699


##########
superset-frontend/src/core/dataset/index.ts:
##########
@@ -0,0 +1,63 @@
+/**
+ * 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.
+ */
+
+/**
+ * Host-internal implementation of the `dataset` namespace.
+ *
+ * Dataset page components call `setCurrentDataset` to publish context as they
+ * load. Extensions consume the stable `DatasetContext` contract; they are
+ * isolated from the page's internal data-fetching implementation.
+ */
+
+import type { dataset as datasetApi } from '@apache-superset/core';
+import { createEmitter } from '../utils';
+
+type DatasetContext = datasetApi.DatasetContext;
+
+const emitter = createEmitter<DatasetContext | undefined>(undefined);
+
+/**
+ * Host-internal: called by the Dataset page when its entity loads or changes.
+ * Not part of the public `@apache-superset/core` API.
+ */
+export const setCurrentDataset = (ctx: DatasetContext | undefined): void => {
+  emitter.fire(ctx);
+};
+
+const getCurrentDataset: typeof datasetApi.getCurrentDataset = () => {
+  const current = emitter.getCurrent();
+  return current ? { ...current } : undefined;
+};
+
+const onDidChangeDataset: typeof datasetApi.onDidChangeDataset = (
+  listener: (ctx: DatasetContext) => void,
+  thisArgs?: unknown,
+) => {
+  const bound = thisArgs ? listener.bind(thisArgs) : listener;
+  // The public contract only emits a concrete context; skip `undefined` clears
+  // so subscribers are never handed an empty value.
+  return emitter.event(ctx => {
+    if (ctx) bound(ctx);
+  });

Review Comment:
   **Suggestion:** `onDidChangeDataset` forwards the exact emitter object 
reference to listeners, so any listener that mutates the received context can 
corrupt the shared in-memory current dataset and affect other listeners and 
future `getCurrentDataset()` reads. Pass a cloned context to listeners (for 
example a shallow copy) so extension callbacks cannot mutate host-owned state. 
[stale reference]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ dataset.getCurrentDataset returns context mutated by extensions.
   - ⚠️ One extension can corrupt dataset metadata for others.
   - ⚠️ Debugging dataset-aware extensions becomes significantly harder.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. In an extension using `@apache-superset/core`, register a dataset 
listener as shown in
   `packages/superset-core/src/dataset/index.ts:26-34` by calling
   `dataset.onDidChangeDataset(ds => { ... })`.
   
   2. Implement the listener to mutate the received context, for example:
   
      `dataset.onDidChangeDataset(ds => { (ds as any).datasetName = 
'mutated-name'; });`,
      matching the `DatasetContext` shape defined in
      `packages/superset-core/src/dataset/index.ts:1-6`.
   
   3. In the host app, when a Dataset page loads, call `setCurrentDataset(ctx)`
   (host-internal API defined in 
`superset-frontend/src/core/dataset/index.ts:39-41`), which
   forwards `ctx` into `emitter.fire(ctx)` using the `Emitter<T>` 
implementation from
   `superset-frontend/src/core/utils.ts:83-93`.
   
   4. The `Emitter` stores this exact object reference as `current` 
(`utils.ts:85-91`) and
   calls all listeners; because `onDidChangeDataset` just wraps 
`emitter.event(ctx => { if
   (ctx) bound(ctx); });` (`dataset/index.ts:48-57`), each subscriber receives 
the same
   mutable object reference and any mutation in one listener alters `current`, 
so subsequent
   `dataset.getCurrentDataset()` calls (`dataset/index.ts:43-45`) and other 
listeners observe
   the mutated DatasetContext.
   ```
   </details>
   
   [Fix in 
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=d5dd9bae75134066b2060dc16c17e24c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 | [Fix in VSCode 
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=d5dd9bae75134066b2060dc16c17e24c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset-frontend/src/core/dataset/index.ts
   **Line:** 55:57
   **Comment:**
        *Stale Reference: `onDidChangeDataset` forwards the exact emitter 
object reference to listeners, so any listener that mutates the received 
context can corrupt the shared in-memory current dataset and affect other 
listeners and future `getCurrentDataset()` reads. Pass a cloned context to 
listeners (for example a shallow copy) so extension callbacks cannot mutate 
host-owned state.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41006&comment_hash=1f8e709d555bbd003831a4e4936106c910e60ecb847e20f282e09e81c20ad718&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41006&comment_hash=1f8e709d555bbd003831a4e4936106c910e60ecb847e20f282e09e81c20ad718&reaction=dislike'>👎</a>



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to