-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Create persistence.h #60
Conversation
#ifndef PERSISTENCE_H #define PERSISTENCE_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <time.h> #include <openssl/rsa.h> #include "io_socket.h" #include "pml_logic_loop.h" #include "memory_silo.h" #include "knowledge.h" #include "PMLL_ARLL_EFLL.h" // Constants #define MAX_KNOWLEDGE_NODES 4096 #define SESSION_TIMEOUT_MS 5000 #define ENCRYPTION_KEY_SIZE 2048 #define PACKAGE_TYPE_KNOWLEDGE_UPDATE 1 // Session context structure typedef struct { io_socket_t io_socket; PMLL_ARLL_EFLL_State state; Graph *knowledge_graph; RSA *encryption_key; memory_silo_t *persistent_silo; } SessionContext; // Function declarations SessionContext* create_session(const char *ip, int port, int max_retries, int feedback_threshold, const char *encryption_key_file); void process_chatlog(SessionContext *ctx, const char *input); void send_secure_graph_update(SessionContext *ctx); void save_session_state(SessionContext *ctx, const char* filename); void handle_network_error(SessionContext *ctx); void run_session_loop(SessionContext *ctx); // Utility function declarations (assumed to be implemented elsewhere) RSA* load_rsa_key(const char *filename); RSA* generate_rsa_key(int key_size); void save_rsa_key(const char *filename, RSA *key); char* sanitize_input(const char *input); EncryptedNode* encrypt_node_data(RSA *key, const char *data); int add_encrypted_node(Graph *graph, EncryptedNode *enode); void update_arll_weights(PMLL_ARLL_EFLL_State *state, int hash); unsigned char* serialize_graph(Graph *graph, size_t *size); size_t rsa_encrypt(RSA *key, const unsigned char *data, size_t data_size, unsigned char *encrypted, size_t encrypted_size); unsigned char* rsa_encrypt_buffer(RSA *key, const unsigned char *data, size_t data_size, size_t *encrypted_size); unsigned int calculate_checksum(const unsigned char *data, size_t size); void write_file_atomically(const char *filename, const unsigned char *data, size_t size); unsigned int calculate_graph_hash(Graph *graph); memory_silo_t* connect_memory_silo(int silo_type); void load_initial_knowledge(SessionContext *ctx, const char *filename); void perform_garbage_collection(Graph *graph); void optimize_memory_usage(memory_silo_t *silo); void initiate_graceful_shutdown(SessionContext *ctx); void validate_session_integrity(SessionContext *ctx); int initialize_subsystems(SessionContext *ctx); int process_queued_messages(SessionContext *ctx); void handle_runtime_error(SessionContext *ctx); int attempt_state_recovery(SessionContext *ctx); void destroy_session(SessionContext *ctx); #endif // PERSISTENCE_H Signed-off-by: J. K. Edwards <joed6834@colorado.edu>
WalkthroughThe pull request introduces a new header file Changes
Sequence DiagramsequenceDiagram
participant User
participant SessionContext
participant Graph
participant MemorySilo
participant NetworkSocket
User->>SessionContext: Initiate Session
SessionContext->>NetworkSocket: Establish Connection
SessionContext->>Graph: Initialize Knowledge Graph
SessionContext->>MemorySilo: Connect Storage
User->>SessionContext: Send Input
SessionContext->>SessionContext: Process & Sanitize Input
SessionContext->>Graph: Update Knowledge
SessionContext->>NetworkSocket: Send Secure Update
SessionContext->>MemorySilo: Save Session State
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🧰 Additional context used🪛 actionlint (1.7.4).github/workflows/build.yml69-69: could not parse as YAML: yaml: line 69: mapping values are not allowed in this context (syntax-check) ⏰ Context from checks skipped due to timeout of 90000ms (1)
🔇 Additional comments (1)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 5
🧹 Nitpick comments (1)
persistence.h (1)
4-14
: Consider organizing headers and documenting dependencies.The headers are logically organized, but consider:
- Grouping project-specific headers based on their dependencies
- Documenting the minimum required OpenSSL version for security compliance
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <time.h> +// Require OpenSSL >= 1.1.1 for security compliance #include <openssl/rsa.h> + +// Core system headers #include "io_socket.h" #include "pml_logic_loop.h" #include "memory_silo.h" + +// Knowledge processing headers #include "knowledge.h" #include "PMLL_ARLL_EFLL.h"
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
persistence.h
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: Summary
🔇 Additional comments (1)
persistence.h (1)
16-20
: Review constant values for scalability and security.Several concerns about the constant values:
MAX_KNOWLEDGE_NODES
(4096) might limit scalability for large knowledge graphsSESSION_TIMEOUT_MS
(5000ms) might be too short for complex graph operationsENCRYPTION_KEY_SIZE
(2048 bits) should be reviewed periodically for security complianceConsider making these configurable at runtime or compile-time based on deployment requirements. For the encryption key size, follow NIST guidelines for minimum RSA key lengths.
void write_file_atomically(const char *filename, const unsigned char *data, size_t size); | ||
unsigned int calculate_graph_hash(Graph *graph); | ||
memory_silo_t* connect_memory_silo(int silo_type); | ||
void load_initial_knowledge(SessionContext *ctx, const char *filename); | ||
void perform_garbage_collection(Graph *graph); | ||
void optimize_memory_usage(memory_silo_t *silo); | ||
void initiate_graceful_shutdown(SessionContext *ctx); | ||
void validate_session_integrity(SessionContext *ctx); | ||
int initialize_subsystems(SessionContext *ctx); | ||
int process_queued_messages(SessionContext *ctx); | ||
void handle_runtime_error(SessionContext *ctx); | ||
int attempt_state_recovery(SessionContext *ctx); | ||
void destroy_session(SessionContext *ctx); |
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.
🛠️ Refactor suggestion
Improve modularity and error handling in utility functions.
Consider:
- Splitting utilities into logical modules (e.g., crypto, persistence, memory)
- Adding error handling for all critical operations
- Documenting cleanup requirements
These functions should be moved to separate header files based on their functionality:
crypto.h
: RSA and encryption-related functionspersistence.h
: File and state managementmemory.h
: Memory silo and garbage collection
This would improve maintainability and make dependencies clearer.
// Session context structure | ||
typedef struct { | ||
io_socket_t io_socket; | ||
PMLL_ARLL_EFLL_State state; | ||
Graph *knowledge_graph; | ||
RSA *encryption_key; | ||
memory_silo_t *persistent_silo; | ||
} SessionContext; |
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.
🛠️ Refactor suggestion
Add documentation and clarify memory ownership.
The SessionContext
structure needs:
- Documentation for each field explaining its purpose and lifecycle
- Clear ownership semantics for pointer members
- Initialization requirements and cleanup responsibilities
// Session context structure
+/**
+ * @brief Encapsulates the state and resources for a session
+ *
+ * Memory ownership:
+ * - knowledge_graph: Owned by the session, must be freed in destroy_session
+ * - encryption_key: Owned by the session, must be freed using RSA_free
+ * - persistent_silo: External ownership, must not be freed by the session
+ */
typedef struct {
io_socket_t io_socket;
PMLL_ARLL_EFLL_State state;
Graph *knowledge_graph;
RSA *encryption_key;
memory_silo_t *persistent_silo;
} SessionContext;
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// Session context structure | |
typedef struct { | |
io_socket_t io_socket; | |
PMLL_ARLL_EFLL_State state; | |
Graph *knowledge_graph; | |
RSA *encryption_key; | |
memory_silo_t *persistent_silo; | |
} SessionContext; | |
/** | |
* @brief Encapsulates the state and resources for a session | |
* | |
* Memory ownership: | |
* - knowledge_graph: Owned by the session, must be freed in destroy_session | |
* - encryption_key: Owned by the session, must be freed using RSA_free | |
* - persistent_silo: External ownership, must not be freed by the session | |
*/ | |
typedef struct { | |
io_socket_t io_socket; | |
PMLL_ARLL_EFLL_State state; | |
Graph *knowledge_graph; | |
RSA *encryption_key; | |
memory_silo_t *persistent_silo; | |
} SessionContext; |
// Function declarations | ||
SessionContext* create_session(const char *ip, int port, | ||
int max_retries, int feedback_threshold, | ||
const char *encryption_key_file); | ||
void process_chatlog(SessionContext *ctx, const char *input); | ||
void send_secure_graph_update(SessionContext *ctx); | ||
void save_session_state(SessionContext *ctx, const char* filename); | ||
void handle_network_error(SessionContext *ctx); | ||
void run_session_loop(SessionContext *ctx); |
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.
🛠️ Refactor suggestion
Improve function signatures for error handling and thread safety.
Core functions need:
- Error return values for failure cases
- Const correctness for input parameters
- Documentation for thread safety guarantees
-SessionContext* create_session(const char *ip, int port,
- int max_retries, int feedback_threshold,
- const char *encryption_key_file);
-void process_chatlog(SessionContext *ctx, const char *input);
-void send_secure_graph_update(SessionContext *ctx);
-void save_session_state(SessionContext *ctx, const char* filename);
-void handle_network_error(SessionContext *ctx);
-void run_session_loop(SessionContext *ctx);
+/**
+ * @brief Creates a new session with the specified parameters
+ * @return NULL on failure with errno set
+ * @thread_safety Thread-unsafe
+ */
+SessionContext* create_session(const char *ip, int port,
+ int max_retries, int feedback_threshold,
+ const char *encryption_key_file);
+
+/**
+ * @brief Processes chat log input
+ * @return 0 on success, negative error code on failure
+ * @thread_safety Thread-safe
+ */
+int process_chatlog(SessionContext * restrict ctx, const char * restrict input);
Committable suggestion skipped: line range outside the PR's diff.
// Utility function declarations (assumed to be implemented elsewhere) | ||
RSA* load_rsa_key(const char *filename); | ||
RSA* generate_rsa_key(int key_size); | ||
void save_rsa_key(const char *filename, RSA *key); | ||
char* sanitize_input(const char *input); | ||
EncryptedNode* encrypt_node_data(RSA *key, const char *data); | ||
int add_encrypted_node(Graph *graph, EncryptedNode *enode); | ||
void update_arll_weights(PMLL_ARLL_EFLL_State *state, int hash); | ||
unsigned char* serialize_graph(Graph *graph, size_t *size); | ||
size_t rsa_encrypt(RSA *key, const unsigned char *data, size_t data_size, | ||
unsigned char *encrypted, size_t encrypted_size); | ||
unsigned char* rsa_encrypt_buffer(RSA *key, const unsigned char *data, size_t data_size, | ||
size_t *encrypted_size); | ||
unsigned int calculate_checksum(const unsigned char *data, size_t size); |
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.
Address security concerns in encryption functions.
The encryption-related functions need:
- Buffer size parameters to prevent overflows
- Error handling for encryption failures
- Secure memory handling for sensitive data
-unsigned char* rsa_encrypt_buffer(RSA *key, const unsigned char *data, size_t data_size,
- size_t *encrypted_size);
+/**
+ * @brief Encrypts data using RSA with secure memory handling
+ * @param key RSA key for encryption
+ * @param data Input data to encrypt
+ * @param data_size Size of input data
+ * @param max_output_size Maximum size of the output buffer
+ * @param encrypted_size [out] Actual size of encrypted data
+ * @return Encrypted data in secure memory (must be freed with OPENSSL_clear_free)
+ */
+unsigned char* rsa_encrypt_buffer(RSA *key,
+ const unsigned char *data,
+ size_t data_size,
+ size_t max_output_size,
+ size_t *encrypted_size);
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// Utility function declarations (assumed to be implemented elsewhere) | |
RSA* load_rsa_key(const char *filename); | |
RSA* generate_rsa_key(int key_size); | |
void save_rsa_key(const char *filename, RSA *key); | |
char* sanitize_input(const char *input); | |
EncryptedNode* encrypt_node_data(RSA *key, const char *data); | |
int add_encrypted_node(Graph *graph, EncryptedNode *enode); | |
void update_arll_weights(PMLL_ARLL_EFLL_State *state, int hash); | |
unsigned char* serialize_graph(Graph *graph, size_t *size); | |
size_t rsa_encrypt(RSA *key, const unsigned char *data, size_t data_size, | |
unsigned char *encrypted, size_t encrypted_size); | |
unsigned char* rsa_encrypt_buffer(RSA *key, const unsigned char *data, size_t data_size, | |
size_t *encrypted_size); | |
unsigned int calculate_checksum(const unsigned char *data, size_t size); | |
// Utility function declarations (assumed to be implemented elsewhere) | |
RSA* load_rsa_key(const char *filename); | |
RSA* generate_rsa_key(int key_size); | |
void save_rsa_key(const char *filename, RSA *key); | |
char* sanitize_input(const char *input); | |
EncryptedNode* encrypt_node_data(RSA *key, const char *data); | |
int add_encrypted_node(Graph *graph, EncryptedNode *enode); | |
void update_arll_weights(PMLL_ARLL_EFLL_State *state, int hash); | |
unsigned char* serialize_graph(Graph *graph, size_t *size); | |
size_t rsa_encrypt(RSA *key, const unsigned char *data, size_t data_size, | |
unsigned char *encrypted, size_t encrypted_size); | |
/** | |
* @brief Encrypts data using RSA with secure memory handling | |
* @param key RSA key for encryption | |
* @param data Input data to encrypt | |
* @param data_size Size of input data | |
* @param max_output_size Maximum size of the output buffer | |
* @param encrypted_size [out] Actual size of encrypted data | |
* @return Encrypted data in secure memory (must be freed with OPENSSL_clear_free) | |
*/ | |
unsigned char* rsa_encrypt_buffer(RSA *key, | |
const unsigned char *data, | |
size_t data_size, | |
size_t max_output_size, | |
size_t *encrypted_size); | |
unsigned int calculate_checksum(const unsigned char *data, size_t size); |
#ifndef PERSISTENCE_H | ||
#define PERSISTENCE_H | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <time.h> | ||
#include <openssl/rsa.h> | ||
#include "io_socket.h" | ||
#include "pml_logic_loop.h" | ||
#include "memory_silo.h" | ||
#include "knowledge.h" | ||
#include "PMLL_ARLL_EFLL.h" | ||
|
||
// Constants | ||
#define MAX_KNOWLEDGE_NODES 4096 | ||
#define SESSION_TIMEOUT_MS 5000 | ||
#define ENCRYPTION_KEY_SIZE 2048 | ||
#define PACKAGE_TYPE_KNOWLEDGE_UPDATE 1 | ||
|
||
// Session context structure | ||
typedef struct { | ||
io_socket_t io_socket; | ||
PMLL_ARLL_EFLL_State state; | ||
Graph *knowledge_graph; | ||
RSA *encryption_key; | ||
memory_silo_t *persistent_silo; | ||
} SessionContext; | ||
|
||
// Function declarations | ||
SessionContext* create_session(const char *ip, int port, | ||
int max_retries, int feedback_threshold, | ||
const char *encryption_key_file); | ||
void process_chatlog(SessionContext *ctx, const char *input); | ||
void send_secure_graph_update(SessionContext *ctx); | ||
void save_session_state(SessionContext *ctx, const char* filename); | ||
void handle_network_error(SessionContext *ctx); | ||
void run_session_loop(SessionContext *ctx); | ||
|
||
// Utility function declarations (assumed to be implemented elsewhere) | ||
RSA* load_rsa_key(const char *filename); | ||
RSA* generate_rsa_key(int key_size); | ||
void save_rsa_key(const char *filename, RSA *key); | ||
char* sanitize_input(const char *input); | ||
EncryptedNode* encrypt_node_data(RSA *key, const char *data); | ||
int add_encrypted_node(Graph *graph, EncryptedNode *enode); | ||
void update_arll_weights(PMLL_ARLL_EFLL_State *state, int hash); | ||
unsigned char* serialize_graph(Graph *graph, size_t *size); | ||
size_t rsa_encrypt(RSA *key, const unsigned char *data, size_t data_size, | ||
unsigned char *encrypted, size_t encrypted_size); | ||
unsigned char* rsa_encrypt_buffer(RSA *key, const unsigned char *data, size_t data_size, | ||
size_t *encrypted_size); | ||
unsigned int calculate_checksum(const unsigned char *data, size_t size); | ||
void write_file_atomically(const char *filename, const unsigned char *data, size_t size); | ||
unsigned int calculate_graph_hash(Graph *graph); | ||
memory_silo_t* connect_memory_silo(int silo_type); | ||
void load_initial_knowledge(SessionContext *ctx, const char *filename); | ||
void perform_garbage_collection(Graph *graph); | ||
void optimize_memory_usage(memory_silo_t *silo); | ||
void initiate_graceful_shutdown(SessionContext *ctx); | ||
void validate_session_integrity(SessionContext *ctx); | ||
int initialize_subsystems(SessionContext *ctx); | ||
int process_queued_messages(SessionContext *ctx); | ||
void handle_runtime_error(SessionContext *ctx); | ||
int attempt_state_recovery(SessionContext *ctx); | ||
void destroy_session(SessionContext *ctx); | ||
|
||
#endif // PERSISTENCE_H |
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.
💡 Codebase verification
Security concerns found in dependent components
The header dependencies are satisfied, but there are security concerns:
- Multiple instances of unsafe string functions (
strcpy
,strcat
,sprintf
,gets
,scanf
) found in the codebase - These functions are vulnerable to buffer overflows and should be replaced with safer alternatives:
- Use
strncpy
,strncat
,snprintf
instead - Replace
gets
withfgets
- Use
scanf
with field width limits or safer alternatives
- Use
🔗 Analysis chain
Verify header dependencies and security requirements.
Please ensure all required headers are available and security requirements are met.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify header dependencies and OpenSSL version
# Check for required headers
echo "Checking for required headers..."
for header in io_socket.h pml_logic_loop.h memory_silo.h knowledge.h PMLL_ARLL_EFLL.h; do
if ! fd -t f "$header"; then
echo "Missing required header: $header"
fi
done
# Check OpenSSL version and security requirements
echo "Checking OpenSSL version..."
if command -v pkg-config &> /dev/null; then
pkg-config --print-requires openssl
pkg-config --modversion openssl
fi
# Check for potential security issues in the codebase
echo "Checking for potential security issues..."
rg -i "strcpy|strcat|sprintf|gets|scanf" || echo "No unsafe string functions found"
Length of output: 3706
Signed-off-by: J. K. Edwards <joed6834@colorado.edu>
#ifndef PERSISTENCE_H
#define PERSISTENCE_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <openssl/rsa.h>
#include "io_socket.h"
#include "pml_logic_loop.h"
#include "memory_silo.h"
#include "knowledge.h"
#include "PMLL_ARLL_EFLL.h"
// Constants
#define MAX_KNOWLEDGE_NODES 4096
#define SESSION_TIMEOUT_MS 5000
#define ENCRYPTION_KEY_SIZE 2048
#define PACKAGE_TYPE_KNOWLEDGE_UPDATE 1
// Session context structure
typedef struct {
io_socket_t io_socket;
PMLL_ARLL_EFLL_State state;
Graph *knowledge_graph;
RSA *encryption_key;
memory_silo_t *persistent_silo;
} SessionContext;
// Function declarations
SessionContext* create_session(const char *ip, int port,
int max_retries, int feedback_threshold,
const char *encryption_key_file);
void process_chatlog(SessionContext *ctx, const char *input); void send_secure_graph_update(SessionContext *ctx); void save_session_state(SessionContext ctx, const char filename); void handle_network_error(SessionContext *ctx);
void run_session_loop(SessionContext *ctx);
// Utility function declarations (assumed to be implemented elsewhere) RSA* load_rsa_key(const char filename);
RSA generate_rsa_key(int key_size);
void save_rsa_key(const char *filename, RSA key); char sanitize_input(const char input);
EncryptedNode encrypt_node_data(RSA *key, const char *data); int add_encrypted_node(Graph *graph, EncryptedNode *enode); void update_arll_weights(PMLL_ARLL_EFLL_State state, int hash); unsigned char serialize_graph(Graph *graph, size_t *size); size_t rsa_encrypt(RSA *key, const unsigned char *data, size_t data_size,
unsigned char encrypted, size_t encrypted_size);
unsigned char rsa_encrypt_buffer(RSA *key, const unsigned char *data, size_t data_size,
size_t *encrypted_size);
unsigned int calculate_checksum(const unsigned char *data, size_t size); void write_file_atomically(const char *filename, const unsigned char *data, size_t size); unsigned int calculate_graph_hash(Graph graph);
memory_silo_t connect_memory_silo(int silo_type); void load_initial_knowledge(SessionContext *ctx, const char *filename); void perform_garbage_collection(Graph *graph);
void optimize_memory_usage(memory_silo_t *silo);
void initiate_graceful_shutdown(SessionContext *ctx); void validate_session_integrity(SessionContext *ctx); int initialize_subsystems(SessionContext *ctx);
int process_queued_messages(SessionContext *ctx);
void handle_runtime_error(SessionContext *ctx);
int attempt_state_recovery(SessionContext *ctx);
void destroy_session(SessionContext *ctx);
#endif // PERSISTENCE_H
Summary by CodeRabbit
New Features
Chores
Security Improvements