chore: add logging for session and redis#614
Conversation
Summary of ChangesHello @Stream29, 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 application's observability by integrating more detailed and context-aware logging across key session management and serverless transaction components. The changes focus on providing richer diagnostic information, such as session IDs, cache keys, and request contexts, for errors and warnings related to session caching and API request handling, making it easier to debug and monitor issues. Highlights
🧠 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. Changelog
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request aims to improve logging by adding more context to log messages for session-related operations and Redis cache interactions, utilizing log.ErrorContext and log.WarnContext. However, a critical security vulnerability has been introduced: the logging of session_id and cache keys (which contain session IDs) leads to sensitive information leakage. Session IDs are sensitive tokens that can be used for user impersonation, and their exposure in logs poses a high security risk. These sensitive fields must be removed from log statements or properly masked before merging. Additionally, there's a potential issue with sessionId handling in serverless transactions; while a warning is logged for a missing Dify-Plugin-Session-ID header, the sessionId from this header is not correctly propagated to the core logic, which could lead to inconsistent behavior.
| log.ErrorContext( | ||
| ctx.Request.Context(), | ||
| "failed to get session info from cache", | ||
| "session_id", sessionId, | ||
| "backwards_request_id", backwardsRequestId, | ||
| "error", err, | ||
| ) |
There was a problem hiding this comment.
Logging the session_id is a security risk as session IDs are sensitive tokens that can be used to impersonate users. Exposing them in logs increases the risk of session hijacking if log files are accessed by unauthorized individuals or systems.
log.ErrorContext(
ctx.Request.Context(),
"failed to get session info from cache",
"backwards_request_id", backwardsRequestId,
"error", err,
)| log.ErrorContext( | ||
| ctx.Request.Context(), | ||
| "failed to write serverless transaction error response", | ||
| "session_id", sessionId, | ||
| "backwards_request_id", backwardsRequestId, | ||
| "error", writeErr, | ||
| ) |
There was a problem hiding this comment.
Logging the session_id is a security risk as session IDs are sensitive tokens that can be used to impersonate users. Exposing them in logs increases the risk of session hijacking if log files are accessed by unauthorized individuals or systems.
log.ErrorContext(
ctx.Request.Context(),
"failed to write serverless transaction error response",
"backwards_request_id", backwardsRequestId,
"error", writeErr,
)| log.WarnContext( | ||
| ctx.Request.Context(), | ||
| "invoke dify failed, received errors", | ||
| "session_id", sessionId, | ||
| "error", err, | ||
| ) |
There was a problem hiding this comment.
| log.ErrorContext( | ||
| s.RequestContext(), | ||
| "set session info to cache failed", | ||
| "session_id", s.ID, | ||
| "cache_key", sessionKey(s.ID), | ||
| "cluster_id", s.ClusterID, | ||
| "plugin_unique_identifier", s.PluginUniqueIdentifier.String(), | ||
| "error", err, | ||
| ) |
There was a problem hiding this comment.
Logging the session_id and cache_key (which contains the session ID) is a security risk. Session IDs are sensitive tokens that should not be exposed in logs to prevent session hijacking.
log.ErrorContext(
s.RequestContext(),
"set session info to cache failed",
"cluster_id", s.ClusterID,
"plugin_unique_identifier", s.PluginUniqueIdentifier.String(),
"error", err,
)| log.ErrorContext( | ||
| session.RequestContext(), | ||
| "delete session info from cache failed", | ||
| "session_id", payload.ID, | ||
| "cache_key", cacheKey, | ||
| "cluster_id", session.ClusterID, | ||
| "plugin_unique_identifier", session.PluginUniqueIdentifier.String(), | ||
| "error", err, | ||
| ) |
There was a problem hiding this comment.
Logging the session_id and cache_key (which contains the session ID) is a security risk. Session IDs are sensitive tokens that should not be exposed in logs to prevent session hijacking.
log.ErrorContext(
session.RequestContext(),
"delete session info from cache failed",
"cluster_id", session.ClusterID,
"plugin_unique_identifier", session.PluginUniqueIdentifier.String(),
"error", err,
)| log.ErrorContext( | ||
| context.Background(), | ||
| "delete session info from cache failed", | ||
| "session_id", payload.ID, | ||
| "cache_key", cacheKey, | ||
| "error", err, | ||
| ) |
There was a problem hiding this comment.
| if sessionId == "" { | ||
| log.WarnContext( | ||
| c.Request.Context(), | ||
| "missing Dify-Plugin-Session-ID header", | ||
| "method", c.Request.Method, | ||
| "path", c.Request.URL.Path, | ||
| ) | ||
| } |
There was a problem hiding this comment.
While adding a warning for a missing Dify-Plugin-Session-ID header improves logging, the sessionId from the header is not consistently used in the downstream logic.
In internal/core/io_tunnel/backwards_invocation/transaction/serverless_handler.go, the sessionId is passed to the Handle function, but the subsequent call to plugin_entities.ParsePluginUniversalEvent hardcodes the session ID argument to an empty string "":
// internal/core/io_tunnel/backwards_invocation/transaction/serverless_handler.go:72
plugin_entities.ParsePluginUniversalEvent(
bytes,
"", // This is hardcoded
...
)This forces the logic to always rely on the session ID from the request body's payload, making the Dify-Plugin-Session-ID header's value effectively ignored for the main session retrieval logic. The sessionId from the header is only used in a separate error logging path, which can cause confusion and hard-to-debug issues if the header and body session IDs differ.
To ensure consistent and predictable behavior, the sessionId from the header should be passed to ParsePluginUniversalEvent. The fix would be in serverless_handler.go to change line 72 to use the sessionId variable.
No description provided.