codeant-ai-for-open-source[bot] commented on code in PR #40815: URL: https://github.com/apache/superset/pull/40815#discussion_r3797421952
########## superset-frontend/packages/superset-ui-core/src/chart/components/Matrixify/useMatrixifyAllowedValues.ts: ########## @@ -0,0 +1,182 @@ +/** + * 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 { useEffect, useMemo, useState } from 'react'; +import { SupersetClient } from '../../..'; +import { getMatrixifyConfig, MatrixifyFormData } from '../../types/matrixify'; + +export type MatrixifyAllowedValuesStatus = 'success' | 'loading' | 'error'; + +export interface MatrixifyAllowedValuesState { + status: MatrixifyAllowedValuesStatus; + /** + * Map of dimension column name -> set of string-normalized values the + * current viewer is allowed to see (RLS applied by the backend). + */ + allowedByColumn: Record<string, Set<string>>; +} + +export interface MatrixifyAllowedValuesFormData extends MatrixifyFormData { + /** ``${id}__${type}`` identifier the column-values endpoint is keyed by */ + datasource?: string; +} + +/** + * A resolution tagged with the request it belongs to, so a result fetched for a + * previous datasource/axis is never mistaken for the current one. + */ +interface ResolvedAllowedValues extends MatrixifyAllowedValuesState { + key: string; +} + +const NO_ALLOWED_VALUES: Record<string, Set<string>> = {}; +const NO_DIMENSIONS_KEY = '[]'; + +/** + * Collect the distinct dimension columns referenced by the matrixify axes. + */ +function getDimensionColumns(formData: MatrixifyFormData): string[] { + const config = getMatrixifyConfig(formData); + if (!config) { + return []; + } + const columns = new Set<string>(); + [config.rows, config.columns].forEach(axis => { + if (axis.mode === 'dimensions' && axis.dimension?.dimension) { + columns.add(axis.dimension.dimension); + } + }); + return Array.from(columns); +} + +/** + * Fetch the distinct values a viewer is permitted to see for a column. This + * reuses the datasource ``/column/<col>/values/`` endpoint, which applies the + * requesting user's row-level security filters server-side. + */ +async function fetchAllowedValues( + datasource: string, + column: string, + signal: AbortSignal, +): Promise<unknown[]> { + const [id, type] = String(datasource).split('__'); + const endpoint = `/api/v1/datasource/${type}/${id}/column/${encodeURIComponent( + column, + )}/values/`; + const { json } = await SupersetClient.get({ endpoint, signal }); + return json?.result || []; +} + +/** + * Resolve, per render, which dimension values the current viewer is allowed to + * see. Matrixify axis values are frozen into ``formData`` at design time, so + * without this the grid would be built from the chart author's RLS context and + * leak values (as subplot headers + empty cells) to restricted viewers. The + * renderer intersects the stored values against the returned allow-list. + * + * Fails closed: while loading the grid must not render, and on error the + * allow-list is treated as empty rather than falling back to the unfiltered + * (leaking) list. + */ +export function useMatrixifyAllowedValues( + formData: MatrixifyAllowedValuesFormData, +): MatrixifyAllowedValuesState { + const { datasource } = formData; + // Serialize the dimension columns to a primitive key. ``formData`` is a fresh + // object on most renders, so the effect must depend on this stable string + // rather than the (always-new) array, otherwise it would refetch in a loop. + const columnsKey = useMemo( + () => JSON.stringify([...getDimensionColumns(formData)].sort()), + [formData], + ); + // Identifies the resolution this render requires; state tagged with any other + // key describes a datasource/axis the viewer is no longer looking at. + const fetchKey = `${datasource ?? ''}|${columnsKey}`; + + const [resolved, setResolved] = useState<ResolvedAllowedValues | null>(null); + + useEffect(() => { + const dimensionColumns: string[] = JSON.parse(columnsKey); + + // Metrics-only matrixify has no dimension axes: nothing to resolve. + if (dimensionColumns.length === 0) { Review Comment: **Suggestion:** When the axes temporarily become metrics-only, this effect returns without clearing `resolved`. If the chart switches back to the same dimension axis, `fetchKey` matches the old resolution, so the hook reports `success` and the renderer builds one frame using the stale allow-list before the new request completes. This can expose values that are no longer permitted after an axis or authorization-context change; invalidate the previous resolution whenever the dimension set becomes empty or otherwise force a loading state for the new request. [stale reference] <details> <summary><b>Severity Level:</b> Minor ๐งน</summary> ```mdx - โ ๏ธ Axis changes can reuse an outdated RLS allow-list. - โ ๏ธ Previously allowed dimension labels may briefly reappear. - โ ๏ธ Fresh authorization results arrive only after rendering. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=4f5a5e8ed57a4dfabbf102ac275c2e9c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=4f5a5e8ed57a4dfabbf102ac275c2e9c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) <details> <summary><b>Prompt for AI Agent ๐ค </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/packages/superset-ui-core/src/chart/components/Matrixify/useMatrixifyAllowedValues.ts **Line:** 117:118 **Comment:** *Stale Reference: When the axes temporarily become metrics-only, this effect returns without clearing `resolved`. If the chart switches back to the same dimension axis, `fetchKey` matches the old resolution, so the hook reports `success` and the renderer builds one frame using the stale allow-list before the new request completes. This can expose values that are no longer permitted after an axis or authorization-context change; invalidate the previous resolution whenever the dimension set becomes empty or otherwise force a loading state for the new request. 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%2F40815&comment_hash=c5179e629c21cdef609ee81ba53ef9621031433f9b416d0909454490f96efae7&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40815&comment_hash=c5179e629c21cdef609ee81ba53ef9621031433f9b416d0909454490f96efae7&reaction=dislike'>๐</a> ########## superset-frontend/packages/superset-ui-core/src/chart/components/Matrixify/MatrixifyGridRenderer.tsx: ########## @@ -121,10 +142,48 @@ function MatrixifyGridRenderer({ height, hooks, }: MatrixifyGridRendererProps) { - // Generate grid structure from form data + // Resolve which dimension values the current viewer is allowed to see, with + // row-level security applied server-side. Matrixify axis values are frozen + // into formData at design time, so without this the grid would be built from + // the chart author's RLS context and leak values to restricted viewers. + const { status: allowedStatus, allowedByColumn } = + useMatrixifyAllowedValues(formData); Review Comment: **Suggestion:** The renderer receives the resolved datasource object through its `datasource` prop, but passes only `formData` to the allow-list hook. In render paths where raw form data does not include the serialized `id__type` datasource value, the hook reports an error and the chart permanently fails closed despite having a valid datasource. Pass the resolved datasource identifier to the hook or derive the endpoint from the supplied datasource prop. [api mismatch] <details> <summary><b>Severity Level:</b> Major โ ๏ธ</summary> ```mdx - โ Matrixify charts fail closed when raw form data omits datasource. - โ ๏ธ Valid resolved datasource metadata is ignored. - โ Explore, dashboard, or embedded matrix rendering can be replaced by an access-error message. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=de1577cd5f8d47788962db8ca8b5eef9&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=de1577cd5f8d47788962db8ca8b5eef9&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) <details> <summary><b>Prompt for AI Agent ๐ค </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/packages/superset-ui-core/src/chart/components/Matrixify/MatrixifyGridRenderer.tsx **Line:** 150:150 **Comment:** *Api Mismatch: The renderer receives the resolved datasource object through its `datasource` prop, but passes only `formData` to the allow-list hook. In render paths where raw form data does not include the serialized `id__type` datasource value, the hook reports an error and the chart permanently fails closed despite having a valid datasource. Pass the resolved datasource identifier to the hook or derive the endpoint from the supplied datasource prop. 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%2F40815&comment_hash=1bf4d9e9a75f83460f8e65455bf16c9bd37d6471f77f04fcc682f67972508a84&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40815&comment_hash=1bf4d9e9a75f83460f8e65455bf16c9bd37d6471f77f04fcc682f67972508a84&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]
