Repository navigation
Improve footer sync and show Creem webhook URL #3236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
18d06c0
d7d1900
96af2a0
b2bda46
e01ed91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,26 +17,59 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. | |||||||||||||||||
| For commercial licensing, please contact support@quantumnous.com | ||||||||||||||||||
| */ | ||||||||||||||||||
|
|
||||||||||||||||||
| import React, { useEffect, useState, useMemo, useContext } from 'react'; | ||||||||||||||||||
| import React, { | ||||||||||||||||||
| useCallback, | ||||||||||||||||||
| useContext, | ||||||||||||||||||
| useMemo, | ||||||||||||||||||
| useRef, | ||||||||||||||||||
| useState, | ||||||||||||||||||
| } from 'react'; | ||||||||||||||||||
| import { useTranslation } from 'react-i18next'; | ||||||||||||||||||
| import { Typography } from '@douyinfe/semi-ui'; | ||||||||||||||||||
| import { getFooterHTML, getLogo, getSystemName } from '../../helpers'; | ||||||||||||||||||
| import { | ||||||||||||||||||
| getFooterRenderMode, | ||||||||||||||||||
| normalizeFooterValue, | ||||||||||||||||||
| } from '../../helpers/footer'; | ||||||||||||||||||
| import { StatusContext } from '../../context/Status'; | ||||||||||||||||||
|
|
||||||||||||||||||
| const FooterBar = () => { | ||||||||||||||||||
| const { t } = useTranslation(); | ||||||||||||||||||
| const [footer, setFooter] = useState(getFooterHTML()); | ||||||||||||||||||
| const iframeRef = useRef(null); | ||||||||||||||||||
| const [iframeHeight, setIframeHeight] = useState(240); | ||||||||||||||||||
| const [statusState] = useContext(StatusContext); | ||||||||||||||||||
| const systemName = getSystemName(); | ||||||||||||||||||
| const logo = getLogo(); | ||||||||||||||||||
| const [statusState] = useContext(StatusContext); | ||||||||||||||||||
| const isDemoSiteMode = statusState?.status?.demo_site_enabled || false; | ||||||||||||||||||
| const footerValue = useMemo( | ||||||||||||||||||
| () => | ||||||||||||||||||
| normalizeFooterValue(statusState?.status?.footer_html ?? getFooterHTML()), | ||||||||||||||||||
| [statusState?.status?.footer_html], | ||||||||||||||||||
| ); | ||||||||||||||||||
| const footerRenderMode = useMemo( | ||||||||||||||||||
| () => getFooterRenderMode(footerValue), | ||||||||||||||||||
| [footerValue], | ||||||||||||||||||
| ); | ||||||||||||||||||
|
|
||||||||||||||||||
| const handleFooterFrameLoad = useCallback(() => { | ||||||||||||||||||
| try { | ||||||||||||||||||
| const iframeDocument = iframeRef.current?.contentDocument; | ||||||||||||||||||
| if (!iframeDocument) { | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| const loadFooter = () => { | ||||||||||||||||||
| let footer_html = localStorage.getItem('footer_html'); | ||||||||||||||||||
| if (footer_html) { | ||||||||||||||||||
| setFooter(footer_html); | ||||||||||||||||||
| const nextHeight = Math.max( | ||||||||||||||||||
| iframeDocument.documentElement?.scrollHeight || 0, | ||||||||||||||||||
| iframeDocument.body?.scrollHeight || 0, | ||||||||||||||||||
| ); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (nextHeight > 0) { | ||||||||||||||||||
| setIframeHeight(nextHeight); | ||||||||||||||||||
| } | ||||||||||||||||||
| } catch { | ||||||||||||||||||
| // Cross-origin iframe documents cannot be measured, so keep the fallback height. | ||||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
| }, []); | ||||||||||||||||||
|
|
||||||||||||||||||
| const currentYear = new Date().getFullYear(); | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -214,18 +247,25 @@ const FooterBar = () => { | |||||||||||||||||
| [logo, systemName, t, currentYear, isDemoSiteMode], | ||||||||||||||||||
| ); | ||||||||||||||||||
|
|
||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||
| loadFooter(); | ||||||||||||||||||
| }, []); | ||||||||||||||||||
|
|
||||||||||||||||||
| return ( | ||||||||||||||||||
| <div className='w-full'> | ||||||||||||||||||
| {footer ? ( | ||||||||||||||||||
| <div className='relative'> | ||||||||||||||||||
| <div | ||||||||||||||||||
| className='custom-footer' | ||||||||||||||||||
| dangerouslySetInnerHTML={{ __html: footer }} | ||||||||||||||||||
| ></div> | ||||||||||||||||||
| {footerRenderMode !== 'default' ? ( | ||||||||||||||||||
| <div className='relative pb-8'> | ||||||||||||||||||
| {footerRenderMode === 'iframe' ? ( | ||||||||||||||||||
| <iframe | ||||||||||||||||||
| ref={iframeRef} | ||||||||||||||||||
| title={t('页脚')} | ||||||||||||||||||
| src={footerValue} | ||||||||||||||||||
| className='custom-footer-frame' | ||||||||||||||||||
| style={{ height: `${iframeHeight}px` }} | ||||||||||||||||||
| onLoad={handleFooterFrameLoad} | ||||||||||||||||||
| /> | ||||||||||||||||||
| ) : ( | ||||||||||||||||||
| <div | ||||||||||||||||||
| className='custom-footer' | ||||||||||||||||||
| dangerouslySetInnerHTML={{ __html: footerValue }} | ||||||||||||||||||
| ></div> | ||||||||||||||||||
|
Comment on lines
+272
to
+275
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify whether footer content is currently sanitized before reaching dangerouslySetInnerHTML.
rg -n "dangerouslySetInnerHTML|normalizeFooterValue|sanitize|DOMPurify" web/src/components/layout/Footer.jsx web/src/helpers/footer.jsRepository: QuantumNous/new-api Length of output: 514 🏁 Script executed: cat -n web/src/helpers/footer.jsRepository: QuantumNous/new-api Length of output: 1711 🏁 Script executed: sed -n '45,60p' web/src/components/layout/Footer.jsxRepository: QuantumNous/new-api Length of output: 529 🏁 Script executed: rg -l "dompurify|DOMPurify" web/Repository: QuantumNous/new-api Length of output: 75 🏁 Script executed: fd package.json web/ | xargs grep -l "dompurify" 2>/dev/null || echo "Not found in direct grep"Repository: QuantumNous/new-api Length of output: 87 🏁 Script executed: cat web/package.json | grep -A 5 -B 5 "dompurify" || echo "dompurify not in package.json"Repository: QuantumNous/new-api Length of output: 92 🏁 Script executed: cat web/package.json | head -50Repository: QuantumNous/new-api Length of output: 1555 🏁 Script executed: grep -A 10 "dompurify" web/bun.lock | head -20Repository: QuantumNous/new-api Length of output: 3988 🏁 Script executed: rg -n "footer_html|getFooterHTML" web/src/ -A 2 -B 2Repository: QuantumNous/new-api Length of output: 2576 Sanitize footer HTML before rendering to avoid XSS.
Use DOMPurify (already available as a transitive dependency of mermaid) to sanitize the HTML before rendering: Proposed fix import React, {
useCallback,
useContext,
useMemo,
useRef,
useState,
} from 'react';
+import DOMPurify from 'dompurify';
@@
const footerRenderMode = useMemo(
() => getFooterRenderMode(footerValue),
[footerValue],
);
+ const sanitizedFooterHtml = useMemo(
+ () => DOMPurify.sanitize(footerValue),
+ [footerValue],
+ );
@@
) : (
<div
className='custom-footer'
- dangerouslySetInnerHTML={{ __html: footerValue }}
+ dangerouslySetInnerHTML={{ __html: sanitizedFooterHtml }}
></div>
)}📝 Committable suggestion
Suggested change
🧰 Tools🪛 Biome (2.4.6)[error] 274-274: Avoid passing content using the dangerouslySetInnerHTML prop. (lint/security/noDangerouslySetInnerHtml) 🤖 Prompt for AI Agents |
||||||||||||||||||
| )} | ||||||||||||||||||
| <div className='absolute bottom-2 right-4 text-xs !text-semi-color-text-2 opacity-70'> | ||||||||||||||||||
| <span>{t('设计与开发由')} </span> | ||||||||||||||||||
| <a | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| Copyright (C) 2025 QuantumNous | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU Affero General Public License as | ||
| published by the Free Software Foundation, either version 3 of the | ||
| License, or (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU Affero General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Affero General Public License | ||
| along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| For commercial licensing, please contact support@quantumnous.com | ||
| */ | ||
|
|
||
| /** | ||
| * 规范化页脚配置值,统一将非字符串输入视为空值。 | ||
| * @param {unknown} footer - 原始页脚配置值 | ||
| * @returns {string} 去除首尾空白后的页脚内容,或空字符串 | ||
| */ | ||
| export function normalizeFooterValue(footer) { | ||
| return typeof footer === 'string' ? footer.trim() : ''; | ||
| } | ||
|
|
||
| /** | ||
| * 根据页脚内容判断前端应使用的渲染模式。 | ||
| * @param {unknown} footer - 原始页脚配置值 | ||
| * @returns {'default' | 'iframe' | 'html'} 页脚渲染模式 | ||
| */ | ||
| export function getFooterRenderMode(footer) { | ||
| const normalizedFooter = normalizeFooterValue(footer); | ||
|
|
||
| if (!normalizedFooter) { | ||
| return 'default'; | ||
| } | ||
|
|
||
| if (/^https?:\/\//i.test(normalizedFooter)) { | ||
| return 'iframe'; | ||
| } | ||
|
|
||
| return 'html'; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import test from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
|
|
||
| import { getFooterRenderMode } from './footer.js'; | ||
|
|
||
| test('treats http and https footer values as iframe embeds', () => { | ||
| assert.equal( | ||
| getFooterRenderMode('https://veriai.chat/landing/footer.html'), | ||
| 'iframe', | ||
| ); | ||
| assert.equal(getFooterRenderMode('http://example.com/footer.html'), 'iframe'); | ||
| }); | ||
|
|
||
| test('treats HTML fragments as inline footer content', () => { | ||
| assert.equal(getFooterRenderMode('<div>Footer</div>'), 'html'); | ||
| }); | ||
|
|
||
| test('falls back to the built-in footer for blank values', () => { | ||
| assert.equal(getFooterRenderMode(''), 'default'); | ||
| assert.equal(getFooterRenderMode(' '), 'default'); | ||
| assert.equal(getFooterRenderMode(null), 'default'); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| Copyright (C) 2025 QuantumNous | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU Affero General Public License as | ||
| published by the Free Software Foundation, either version 3 of the | ||
| License, or (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU Affero General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Affero General Public License | ||
| along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| For commercial licensing, please contact support@quantumnous.com | ||
| */ | ||
|
|
||
| /** | ||
| * 构建支付渠道的 webhook 回调地址。 | ||
| * @param {string | null | undefined} serverAddress - 后台配置的站点地址 | ||
| * @param {string} provider - 支付渠道标识 | ||
| * @param {string} [fallbackBaseLabel=''] - 站点地址缺失时使用的展示占位 | ||
| * @returns {string} 标准化后的 webhook 地址 | ||
| */ | ||
| export function getPaymentWebhookUrl( | ||
| serverAddress, | ||
| provider, | ||
| fallbackBaseLabel = '', | ||
| ) { | ||
| const normalizedServerAddress = String(serverAddress || '') | ||
| .trim() | ||
| .replace(/\/+$/, ''); | ||
| const baseUrl = normalizedServerAddress || fallbackBaseLabel; | ||
| return `${baseUrl}/api/${provider}/webhook`; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import test from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
|
|
||
| import { getPaymentWebhookUrl } from './paymentWebhook.js'; | ||
|
|
||
| test('builds creem webhook url from server address', () => { | ||
| assert.equal( | ||
| getPaymentWebhookUrl('https://veriai.chat/', 'creem'), | ||
| 'https://veriai.chat/api/creem/webhook', | ||
| ); | ||
| }); | ||
|
|
||
| test('uses caller-provided fallback when server address is missing', () => { | ||
| assert.equal( | ||
| getPaymentWebhookUrl('', 'creem', 'Website Address'), | ||
| 'Website Address/api/creem/webhook', | ||
| ); | ||
| }); | ||
|
|
||
| test('returns a relative webhook path when no fallback is provided', () => { | ||
| assert.equal(getPaymentWebhookUrl('', 'creem'), '/api/creem/webhook'); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| Copyright (C) 2025 QuantumNous | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU Affero General Public License as | ||
| published by the Free Software Foundation, either version 3 of the | ||
| License, or (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU Affero General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Affero General Public License | ||
| along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| For commercial licensing, please contact support@quantumnous.com | ||
| */ | ||
|
|
||
| const STATUS_OPTION_FIELD_MAP = { | ||
| SystemName: 'system_name', | ||
| Logo: 'logo', | ||
| Footer: 'footer_html', | ||
| }; | ||
|
|
||
| /** | ||
| * 将设置项更新转换为需要同步到状态上下文的补丁对象。 | ||
| * @param {string} key - 设置项键名 | ||
| * @param {unknown} value - 设置项值 | ||
| * @returns {{statusKey: string, storageKey: string, value: string} | null} 可同步的状态补丁 | ||
| */ | ||
| export function getStatusOptionPatch(key, value) { | ||
| const statusKey = STATUS_OPTION_FIELD_MAP[key]; | ||
|
|
||
| if (!statusKey) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| statusKey, | ||
| storageKey: statusKey, | ||
| value: typeof value === 'string' ? value : '', | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import test from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
|
|
||
| import { getStatusOptionPatch } from './statusOption.js'; | ||
|
|
||
| test('maps footer option updates to footer_html status data', () => { | ||
| assert.deepEqual( | ||
| getStatusOptionPatch('Footer', 'https://veriai.chat/landing/footer.html'), | ||
| { | ||
| statusKey: 'footer_html', | ||
| storageKey: 'footer_html', | ||
| value: 'https://veriai.chat/landing/footer.html', | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| test('returns null for options that are not mirrored into status', () => { | ||
| assert.equal(getStatusOptionPatch('About', '<div>About</div>'), null); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset iframe height when source/mode changes to keep fallback behavior correct.
If a previous same-origin footer set a large measured height, then a new cross-origin iframe can fail measurement and keep the old large height instead of the intended fallback.
🧩 Minimal fix
import React, { useCallback, useContext, + useEffect, useMemo, useRef, useState, } from 'react'; @@ const handleFooterFrameLoad = useCallback(() => { @@ }, []); + + useEffect(() => { + if (footerRenderMode === 'iframe') { + setIframeHeight(240); + } + }, [footerRenderMode, footerValue]);📝 Committable suggestion
🤖 Prompt for AI Agents