-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: enhance plugin page internationalization #7998
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 all commits
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 |
|---|---|---|
|
|
@@ -189,7 +189,13 @@ async def get_plugin_page_bridge_sdk(self): | |
| return await self._plugin_page_error_response( | ||
| 404, "Plugin Page bridge SDK not found" | ||
| ) | ||
| bridge_js = await self._read_plugin_page_binary(_PLUGIN_PAGE_BRIDGE_FILE) | ||
| bridge_js = await self._read_plugin_page_text(_PLUGIN_PAGE_BRIDGE_FILE) | ||
| initial_context = self._get_plugin_page_initial_context() | ||
| if initial_context: | ||
| context_json = json.dumps(initial_context, ensure_ascii=False) | ||
| bridge_js += ( | ||
|
Comment on lines
+192
to
+196
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. 🚨 issue (security): Inline injection of JSON context into bridge JS can break the script or introduce XSS edge cases. Here To harden this:
Since plugin authors control this content, this is a realistic XSS vector and should be addressed before shipping. |
||
| f"\n;window.AstrBotPluginPage?.__setInitialContext({context_json});\n" | ||
| ) | ||
| response = cast( | ||
| QuartResponse, | ||
| await make_response( | ||
|
|
@@ -204,6 +210,82 @@ def _get_plugin_metadata_by_name(self, plugin_name: str) -> StarMetadata | None: | |
| return plugin | ||
| return None | ||
|
|
||
| @staticmethod | ||
| def _get_by_path(source: dict | None, key: str): | ||
| if not isinstance(source, dict) or not key: | ||
| return None | ||
| current = source | ||
| for part in key.split("."): | ||
| if not isinstance(current, dict) or part not in current: | ||
| return None | ||
| current = current[part] | ||
| return current | ||
|
Comment on lines
+214
to
+222
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. |
||
|
|
||
| @staticmethod | ||
| def _get_request_locale(default: str = "zh-CN") -> str: | ||
| raw_locale = request.headers.get("Accept-Language", "").strip() | ||
| locale = raw_locale.split(",", 1)[0].split(";", 1)[0].strip() | ||
| if not locale or len(locale) > 32: | ||
| return default | ||
| return locale | ||
|
|
||
| def _get_plugin_page_initial_context(self) -> dict | None: | ||
| asset_token = request.args.get("asset_token", "").strip() | ||
| if not asset_token: | ||
| return None | ||
| jwt_secret = self.config.get("dashboard", {}).get("jwt_secret") | ||
| if not isinstance(jwt_secret, str) or not jwt_secret.strip(): | ||
| return None | ||
|
|
||
| try: | ||
| payload = jwt.decode(asset_token, jwt_secret, algorithms=["HS256"]) | ||
| except jwt.InvalidTokenError: | ||
| return None | ||
| if payload.get("token_type") != _PLUGIN_PAGE_ASSET_TOKEN_TYPE: | ||
| return None | ||
|
|
||
| plugin_name = payload.get("plugin_name") | ||
| page_name = payload.get("page_name") | ||
| if not isinstance(plugin_name, str) or not isinstance(page_name, str): | ||
| return None | ||
|
|
||
| plugin = self._get_plugin_metadata_by_name(plugin_name) | ||
| if not plugin: | ||
| return None | ||
|
|
||
| locale = ( | ||
| payload.get("locale") | ||
| if isinstance(payload.get("locale"), str) | ||
| else self._get_request_locale() | ||
| ) | ||
| plugin_i18n = plugin.i18n or {} | ||
| try: | ||
| plugin_root = self._get_plugin_root_dir(plugin) | ||
| fresh_i18n = PluginManager._load_plugin_i18n(str(plugin_root)) | ||
| if fresh_i18n: | ||
| plugin_i18n = fresh_i18n | ||
| except (OSError, ValueError): | ||
| pass | ||
|
|
||
| locale_data = plugin_i18n.get(locale) | ||
| display_name = ( | ||
| self._get_by_path(locale_data, "metadata.display_name") | ||
| or plugin.display_name | ||
| or plugin.name | ||
| ) | ||
| page_title = ( | ||
| self._get_by_path(locale_data, f"pages.{page_name}.title") or page_name | ||
| ) | ||
|
|
||
| return { | ||
| "pluginName": plugin.name, | ||
| "displayName": display_name, | ||
| "pageName": page_name, | ||
| "pageTitle": page_title, | ||
| "locale": locale, | ||
| "i18n": plugin_i18n, | ||
| } | ||
|
|
||
| @staticmethod | ||
| def _normalize_plugin_page_path( | ||
| raw_path: str, | ||
|
|
@@ -634,6 +716,7 @@ async def _serialize_plugin_page( | |
| page_data = { | ||
| "name": page.name, | ||
| "title": page.title, | ||
| "i18n_key": f"pages.{page.name}", | ||
| } | ||
| if include_content_path: | ||
| asset_token = ( | ||
|
|
@@ -675,6 +758,7 @@ def _issue_plugin_page_asset_token( | |
| "token_type": _PLUGIN_PAGE_ASSET_TOKEN_TYPE, | ||
| "plugin_name": plugin_name, | ||
| "page_name": page_name, | ||
| "locale": self._get_request_locale(), | ||
| "iat": now, | ||
| "exp": now + timedelta(seconds=_PLUGIN_PAGE_ASSET_TOKEN_TTL_SECONDS), | ||
| } | ||
|
|
@@ -1285,6 +1369,7 @@ async def get_plugin_page_components(self, plugin) -> list[dict]: | |
| "name": page["title"], | ||
| "title": page["title"], | ||
| "page_name": page["name"], | ||
| "i18n_key": page["i18n_key"], | ||
| "description": "Plugin Page entry", | ||
| "plugin_name": plugin.name, | ||
| } | ||
|
|
||
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.
The notifyContextHandlers function uses a try-catch block inside a forEach loop. While this prevents one failing handler from stopping others, it is generally better to use a for...of loop for better readability and to avoid potential issues with forEach and async/await if handlers were to become asynchronous in the future.