rusackas commented on code in PR #40629: URL: https://github.com/apache/superset/pull/40629#discussion_r3342891260
########## superset-frontend/src/embedded/originValidation.ts: ########## @@ -0,0 +1,68 @@ +/** + * 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. + */ + +// The message type used for the embedded comms handshake. Must stay in sync +// with the parent SDK's handshake messages. +export const MESSAGE_TYPE = '__embedded_comms__'; + +/** + * Validates the origin of an incoming postMessage event against the dashboard's + * configured allowed domains. + * + * Enforcement is opt-in by configuration: if the allowed-domains list is empty + * or undefined, any origin is accepted (no restriction), which preserves the + * historical behavior for embeds that did not configure domains. When the list + * is non-empty, only origins present in the list are accepted. + */ +export function isMessageOriginAllowed( + origin: string, + allowedDomains?: string[], +): boolean { + if (!allowedDomains || allowedDomains.length === 0) { + return true; + } + if (allowedDomains.includes(origin)) { + return true; + } + // eslint-disable-next-line no-console + console.warn( + `[superset] ignoring embedded message from origin "${origin}" which is not in the list of allowed domains`, + ); + return false; +} + +/** + * Validates that an incoming message is intended for embedded comms and that it + * originates from an allowed domain. Returns `true` when the message should be + * processed, `false` otherwise. + */ +export function validateMessageEvent( + event: MessageEvent, + allowedDomains?: string[], +): boolean { + if (!isMessageOriginAllowed(event.origin, allowedDomains)) { + return false; + } + + if (typeof event.data !== 'object' || event.data.type !== MESSAGE_TYPE) { Review Comment: Good catch — 'typeof null === "object"' so a null payload would have slipped past that check and thrown on '.type'. Added an explicit 'event.data === null' guard and a regression test. ########## superset-frontend/src/embedded/originValidation.ts: ########## @@ -0,0 +1,68 @@ +/** + * 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. + */ + +// The message type used for the embedded comms handshake. Must stay in sync +// with the parent SDK's handshake messages. +export const MESSAGE_TYPE = '__embedded_comms__'; + +/** + * Validates the origin of an incoming postMessage event against the dashboard's + * configured allowed domains. + * + * Enforcement is opt-in by configuration: if the allowed-domains list is empty + * or undefined, any origin is accepted (no restriction), which preserves the + * historical behavior for embeds that did not configure domains. When the list + * is non-empty, only origins present in the list are accepted. + */ +export function isMessageOriginAllowed( + origin: string, + allowedDomains?: string[], +): boolean { + if (!allowedDomains || allowedDomains.length === 0) { + return true; + } + if (allowedDomains.includes(origin)) { + return true; + } + // eslint-disable-next-line no-console + console.warn( + `[superset] ignoring embedded message from origin "${origin}" which is not in the list of allowed domains`, + ); + return false; Review Comment: Agreed, this is a real mismatch with the backend's same_origin check. I now normalize each configured allowed domain to its origin via new URL(domain).origin (guarded against unparseable values) before comparing, so entries with a trailing slash or path no longer get silently rejected on the frontend. Added tests for both cases. -- 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]
