Skip to content

Conversation

@adongfan
Copy link
Contributor

@adongfan adongfan commented Jan 7, 2026

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

This PR fixes critical security vulnerabilities, resolves workflow execution serialization issues, and optimizes the LogStore initialization process to improve system reliability.

Key Changes

1. Security: SQL Injection Prevention

  • Added SQL escape utilities to prevent SQL injection attacks and cross-tenant data access
  • Applied proper escaping for both SQL syntax and LogStore query syntax
  • Protects tenant_id, app_id, workflow_run_id and other identifiers from injection attacks

2. Serialization Fix

  • Fixed ArrayFileSegment type serialization issue in workflow execution
  • Now uses WorkflowRuntimeTypeConverter for proper serialization handling

3. Startup Optimization

  • Non-blocking Initialization: Perform a lightweight connectivity check to the configured logstore endpoint before establishing a full connection, to avoid blocking Dify's startup if the endpoint is unreachable.
  • Conditional Initialization: LogStore plugin initialization now only occurs when XXX_REPOSITORY is explicitly configured to use LogStore mode

4. Other Improvements

  • Connection Pool improvement : use SQLAlchemy connection pool for better connection recycling
  • Enhanced logging messages for better debugging
  • Improved some configuration comments

Screenshots

Before After
... ...

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint and make type-check (backend) and cd web && npx lint-staged (frontend) to appease the lint gods

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @adongfan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the security and reliability of the Aliyun SLS LogStore integration. It introduces robust SQL injection prevention mechanisms, migrates the PostgreSQL protocol connection management to SQLAlchemy for improved stability, and refines the logic for enabling the LogStore extension. These changes collectively aim to provide a more secure, performant, and maintainable logging solution.

Highlights

  • SQL Injection Prevention: Introduced a new sql_escape.py module with utility functions to safely escape SQL strings and identifiers, effectively preventing SQL injection vulnerabilities in LogStore queries, particularly for cross-tenant data access scenarios.
  • Aliyun SLS Connectivity Pre-check: Added a pre-check for Aliyun SLS endpoint connectivity using socket connections. This prevents indefinite hangs during LogStore initialization if the configured endpoint is unreachable, improving application startup robustness.
  • SQLAlchemy for PostgreSQL Protocol: Refactored the PostgreSQL protocol integration for Aliyun SLS (aliyun_logstore_pg.py) to utilize SQLAlchemy's engine and connection pooling. This upgrade brings enhanced connection management features like pool_recycle, pool_pre_ping, and TCP keepalive support, leading to more stable and efficient database interactions.
  • Refined LogStore Enablement Logic: Updated the is_enabled() function in ext_logstore.py to ensure LogStore is only activated if all necessary Aliyun SLS environment variables are configured AND at least one repository is explicitly set to use a LogStore implementation. This provides more precise control over LogStore activation.
  • Improved Logging and Error Handling: Enhanced logging messages across LogStore modules for better clarity during initialization, connection attempts, and fallback scenarios (e.g., when PG protocol fails). Detailed error messages now include configuration parameters, aiding in quicker debugging.
  • Workflow Runtime Type Conversion: Integrated WorkflowRuntimeTypeConverter for serializing complex workflow data types (like graph, inputs, and outputs) into JSON format for storage in LogStore, ensuring data integrity and compatibility.
  • Documentation of LogStore Repository Options: Updated the docker/.env.example file to clearly list the new LogStore repository options for workflow execution and node execution, making it easier for developers to configure and switch between repository implementations.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces significant improvements to the Aliyun LogStore integration, focusing on security, robustness, and maintainability. The key changes include:

  • Critical Security Fix: Addressing SQL injection vulnerabilities by consistently escaping user-provided values in dynamically constructed SQL queries. This is accompanied by a comprehensive new test suite for SQL injection.
  • Connection Pooling Refactor: The PostgreSQL connection handling for LogStore has been refactored to use SQLAlchemy's connection engine, which provides a more robust and feature-rich connection pool with features like recycling and keepalives.
  • Improved Initialization: The LogStore extension initialization is now more resilient, with pre-checks for endpoint connectivity and more detailed error logging to aid debugging.

The changes are well-implemented and greatly enhance the quality of the LogStore extension. I have a couple of suggestions for improvement: one to make the new endpoint connectivity check more robust, and another to correct the exception type check in the retry logic for PG connections. Overall, this is an excellent contribution.

@adongfan adongfan changed the title Fix sls 0105 fix(logstore): prevent SQL injection and optimize initialization process Jan 7, 2026
@adongfan adongfan changed the title fix(logstore): prevent SQL injection and optimize initialization process fix(logstore): prevent SQL injection, fix serialization issues, and optimize initialization Jan 7, 2026
@adongfan adongfan marked this pull request as ready for review January 8, 2026 01:24
@dosubot dosubot bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant