last Modules Update ...

This commit is contained in:
2026-11-06 21:00:06 +03:30
parent 1a9b9f15db
commit d5266d60ad
12 changed files with 8571 additions and 144 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,836 @@
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>گزارش بازبینی کد و پیشنهادهای بهبود | سامانه xAi</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
:root {
--primary: #1a365d;
--primary-light: #2c5282;
--accent: #c9a961;
--accent-dark: #a8893f;
--bg: #faf9f6;
--card-bg: #ffffff;
--text: #1a202c;
--text-muted: #4a5568;
--border: #e2e8f0;
--success: #38a169;
--danger: #e53e3e;
--warning: #dd6b20;
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Vazirmatn', Tahoma, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.9;
font-size: 15px;
padding: 30px 15px;
}
.document-container {
max-width: 1100px;
margin: 0 auto;
background: var(--card-bg);
box-shadow: var(--shadow);
border-radius: 12px;
overflow: hidden;
}
.document-header {
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
color: white;
padding: 50px 40px 40px;
position: relative;
border-bottom: 4px solid var(--accent);
}
.document-header::before {
content: '';
position: absolute;
top: 0; right: 0;
width: 200px; height: 200px;
background: radial-gradient(circle, rgba(201,169,97,0.15) 0%, transparent 70%);
border-radius: 50%;
transform: translate(50%, -50%);
}
.header-top {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
position: relative;
z-index: 1;
}
.company-badge { display: flex; align-items: center; gap: 12px; }
.company-logo {
width: 60px; height: 60px;
background: var(--accent);
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.doc-meta { text-align: left; font-size: 12px; opacity: 0.9; }
.doc-meta div { margin-bottom: 3px; }
.document-title {
text-align: center;
position: relative;
z-index: 1;
padding: 20px 0;
}
.document-title .label {
display: inline-block;
background: rgba(201,169,97,0.2);
color: var(--accent);
padding: 4px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-bottom: 15px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-title h1 { font-size: 28px; font-weight: 800; margin-bottom: 10px; letter-spacing: -0.5px; }
.document-title h2 { font-size: 16px; font-weight: 400; opacity: 0.9; }
.document-body { padding: 40px; }
.intro-text {
background: linear-gradient(to left, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin-bottom: 35px;
font-size: 14px;
color: var(--text-muted);
}
.section { margin-bottom: 40px; page-break-inside: avoid; }
.section-header {
display: flex;
align-items: center;
gap: 15px;
padding-bottom: 12px;
border-bottom: 2px solid var(--border);
margin-bottom: 20px;
}
.section-number {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 20px;
flex-shrink: 0;
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
}
.section-title { flex: 1; }
.section-title h3 { font-size: 19px; font-weight: 700; color: var(--primary); margin-bottom: 2px; }
.section-title .subtitle { font-size: 12px; color: var(--text-muted); }
.section-content { padding-right: 10px; }
.section-content p { margin-bottom: 12px; text-align: justify; text-indent: 20px; }
.section-content p.no-indent { text-indent: 0; }
.highlight-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #f6e05e;
border-right: 4px solid var(--accent);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.highlight-box strong { color: var(--accent-dark); }
.info-box {
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
border: 1px solid #90cdf4;
border-right: 4px solid var(--primary-light);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.success-box {
background: linear-gradient(to left, #f0fff4, #e6fffa);
border: 1px solid #9ae6b4;
border-right: 4px solid var(--success);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.warning-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #fbd38d;
border-right: 4px solid var(--warning);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.styled-table thead {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
}
.styled-table th {
padding: 12px 12px;
text-align: right;
font-weight: 600;
font-size: 12px;
}
.styled-table td {
padding: 11px 12px;
border-bottom: 1px solid var(--border);
font-size: 12px;
vertical-align: top;
}
.styled-table tbody tr:last-child td { border-bottom: none; }
.styled-table tbody tr:nth-child(even) { background: #f8fafc; }
.styled-table .highlight-row { background: #fffaf0 !important; font-weight: 600; }
.badge {
display: inline-block;
padding: 2px 9px;
border-radius: 12px;
font-size: 10px;
font-weight: 700;
color: #fff;
white-space: nowrap;
}
.badge.critical { background: #c53030; }
.badge.high { background: #dd6b20; }
.badge.medium { background: #d69e2e; }
.badge.low { background: #3182ce; }
.stats-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 15px;
margin: 25px 0;
}
.stat-card {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 18px;
border-radius: 10px;
text-align: center;
box-shadow: 0 4px 12px rgba(26,54,93,0.2);
}
.stat-card .stat-value {
font-size: 24px;
font-weight: 800;
margin-bottom: 5px;
color: var(--accent);
line-height: 1.3;
}
.stat-card .stat-label { font-size: 11px; opacity: 0.9; }
.reference-list { list-style: none; padding: 0; margin: 20px 0; }
.reference-list li {
background: #f8fafc;
border-right: 3px solid var(--primary-light);
padding: 15px 20px;
margin-bottom: 10px;
border-radius: 6px;
font-size: 13px;
}
.reference-list li strong { color: var(--primary); display: block; margin-bottom: 5px; }
.conclusion-box {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 30px;
border-radius: 12px;
margin: 30px 0;
text-align: center;
box-shadow: 0 8px 20px rgba(26,54,93,0.3);
}
.conclusion-box h3 { font-size: 22px; font-weight: 800; margin-bottom: 15px; color: var(--accent); }
.conclusion-box p { font-size: 14px; line-height: 1.8; text-indent: 0 !important; margin-bottom: 15px; }
.conclusion-box .highlight {
background: rgba(201,169,97,0.2);
padding: 15px 20px;
border-radius: 8px;
margin-top: 20px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-footer {
background: var(--primary);
color: white;
padding: 25px 40px;
text-align: center;
font-size: 12px;
margin-top: 40px;
}
.document-footer .footer-brand { font-weight: 700; color: var(--accent); margin-bottom: 5px; font-size: 14px; }
.footer-divider { width: 60px; height: 2px; background: var(--accent); margin: 10px auto; }
.page-info {
text-align: center;
padding: 15px;
font-size: 11px;
color: var(--text-muted);
border-top: 1px solid var(--border);
margin-top: 30px;
}
@media print {
body { background: white; padding: 0; }
.document-container { box-shadow: none; border-radius: 0; }
.section { page-break-inside: avoid; }
}
@media (max-width: 768px) {
.document-body { padding: 25px 20px; }
.document-header { padding: 30px 20px; }
.stats-grid { grid-template-columns: 1fr 1fr; }
.header-top { flex-direction: column; gap: 15px; }
.doc-meta { text-align: center; }
.styled-table { font-size: 11px; }
}
</style>
</head>
<body>
<div class="document-container">
<header class="document-header">
<div class="header-top">
<div class="company-badge">
<div class="company-logo"></div>
</div>
<div class="doc-meta">
<div>شماره مستند: REV-1405-001</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: آبان ۱۴۰۵</div>
<div>طبقه‌بندی: محرمانه</div>
</div>
</div>
<div class="document-title">
<span class="label">CODE REVIEW &amp; RECOMMENDATIONS</span>
<h1>گزارش بازبینی کد و پیشنهادهای بهبود</h1>
<h2>بررسی استاتیک ماژول‌های xAiApi، xAiMcpServer، xAiModels و xAiService</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>تیم توسعه محترم،</strong><br>
این مستند بر اساس بررسی استاتیک فایل‌های ارائه‌شده تهیه شده است. هدف، شناسایی خطاهای منطقی، ریسک‌های امنیتی، مشکلات پایداری و ارائه پیشنهادهای عملی برای ارتقای کیفیت سامانه xAi است. این گزارش جایگزین تست‌های اجرایی و امنیتی نیست؛ اما می‌تواند به‌عنوان چک‌لیست اصلاحات پیش از ادامه توسعه استفاده شود.
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>خلاصه اجرایی</h3>
<div class="subtitle">Executive Summary</div>
</div>
</div>
<div class="section-content">
<p>معماری کلی سامانه از انسجام خوبی برخوردار است و لایه‌بندی ماژول‌ها، استفاده از DI، الگوی Repository و پشتیبانی از RAG و MCP قابل تقدیر است. با این حال، چند ریسک امنیتی و خطای رفتاری مهم وجود دارد که در صورت عدم اصلاح، می‌تواند منجر به دسترسی غیرمجاز، ناپایداری اتصال MCP و از دست رفتن داده در فرایند Ingest شود.</p>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-value">۴</div>
<div class="stat-label">ماژول اصلی بررسی‌شده</div>
</div>
<div class="stat-card">
<div class="stat-value">۲۴</div>
<div class="stat-label">یافته ثبت‌شده</div>
</div>
<div class="stat-card">
<div class="stat-value">۶</div>
<div class="stat-label">مورد بحرانی / بالا</div>
</div>
<div class="stat-card">
<div class="stat-value">۲۰</div>
<div class="stat-label">پیشنهاد بهبود</div>
</div>
</div>
<div class="highlight-box">
<strong>نتیجه کلیدی:</strong> پیش از افزودن قابلیت‌های جدید، اصلاح کنترل دسترسی مبتنی بر مالکیت منابع، پایدارسازی اتصال MCP و ایمن‌سازی فرایند Ingest/RAG در اولویت قرار دارد.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>دامنه بازبینی و نمای کلی ماژول‌ها</h3>
<div class="subtitle">Review Scope &amp; Architecture Overview</div>
</div>
</div>
<div class="section-content">
<table class="styled-table">
<thead>
<tr>
<th>ماژول</th>
<th>مسئولیت اصلی</th>
<th>نکته کلی</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>xAiApi</strong></td>
<td>Web API، کنترلرهای AI، RAG، Proxy مدل، Extractors، Ingest</td>
<td>ساختار مناسب؛ اما ترتیب Middleware و رفتار Extractors نیازمند اصلاح است.</td>
</tr>
<tr>
<td><strong>xAiMcpServer</strong></td>
<td>MCP Server، ابزارهای سیستمی و کاربری، اسکن ابزارها</td>
<td>طراحی قابل توسعه؛ اما ریسک AllowAnonymous و خطاهای ورودی وجود دارد.</td>
</tr>
<tr>
<td><strong>xAiModels</strong></td>
<td>Domain، DTOها، DataProvider، Repositoryها، GraphQL</td>
<td>لایه‌بندی خوب؛ اما چند خطای EF، IDOR و ناسازگاری فرمت Embedding دیده می‌شود.</td>
</tr>
<tr>
<td><strong>xAiService</strong></td>
<td>MCP Client، Handler، Session Store، Transport، Scanner</td>
<td>پیاده‌سازی MCP قابل توجه؛ اما مدیریت Session و SSE ناقص است.</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>یافته‌های خطا و ریسک</h3>
<div class="subtitle">Findings &amp; Risk Register</div>
</div>
</div>
<div class="section-content">
<table class="styled-table">
<thead>
<tr>
<th style="width:55px;">کد</th>
<th style="width:70px;">شدت</th>
<th style="width:170px;">ماژول / فایل</th>
<th>شرح یافته</th>
<th>اثر</th>
<th>پیشنهاد اصلاح</th>
</tr>
</thead>
<tbody>
<tr>
<td>F-01</td>
<td><span class="badge critical">بحرانی</span></td>
<td>xAiService / XAiServiceControllerBase + xAiModels / XAiDataProvider</td>
<td>متدهای LoadConversations، LoadMessages و CreateConversation مالکیت پروژه/گفتگو را بررسی نمی‌کنند.</td>
<td>IDOR؛ خواندن گفتگوها و پیام‌های سایر کاربران و ایجاد گفتگو در پروژه دیگران.</td>
<td>بررسی OwnerId در لایه سرویس و DataProvider؛ افزودن تست‌های امنیتی برای هر متد.</td>
</tr>
<tr>
<td>F-02</td>
<td><span class="badge critical">بحرانی</span></td>
<td>xAiApi / Providers / XAiMcpServerProvider.UpdateMcpServer</td>
<td>بررسی مالکیت بر اساس item.OwnerId انجام می‌شود، نه رکورد موجود در دیتابیس.</td>
<td>امکان ویرایش سرور MCP متعلق به کاربر دیگر با ارسال OwnerId جعلی.</td>
<td>ابتدا رکورد موجود را واکشی و OwnerId آن را با کاربر مقایسه کنید؛ سپس Update.</td>
</tr>
<tr>
<td>F-03</td>
<td><span class="badge high">بالا</span></td>
<td>xAiService / XAiMcpClient + XAiMcpHttpTransport</td>
<td>هدر Mcp-Session-Id از پاسخ سرور خوانده و در XAiMcpSessionStore ذخیره نمی‌شود؛ متد Set هرگز فراخوانی نمی‌شود.</td>
<td>سرورهای MCP مبتنی بر Session کار نمی‌کنند یا در درخواست‌های بعدی خطا می‌دهند.</td>
<td>در Transport هدر پاسخ را استخراج و در SessionStore.Set ذخیره کنید؛ در Initialize و ListTools استفاده شود.</td>
</tr>
<tr>
<td>F-04</td>
<td><span class="badge high">بالا</span></td>
<td>xAiService / Controllers / XAiMcpControllerBase</td>
<td>کلاس پایه با [AllowAnonymous] علامت‌گذاری شده و McpController با [Authorize] ارث‌بری می‌کند.</td>
<td>احتمال بی‌اثر شدن Authorize و دسترسی ناخواسته به HandleMcp.</td>
<td>AllowAnonymous را از کلاس حذف و فقط روی SseEndpoint اعمال کنید؛ تست دسترسی ناشناس.</td>
</tr>
<tr>
<td>F-05</td>
<td><span class="badge high">بالا</span></td>
<td>xAiApi / Extractors / XFileContentExtractor + XImage/XAudio/XVision</td>
<td>همه Extractorها به‌صورت eager ساخته می‌شوند و برخی در Constructor برای OCR/Transcript/Vision اعتبارسنجی سخت‌گیرانه دارند.</td>
<td>نبود کانفیگ OCR یا Transcript باعث شکست کل استخراج فایل، حتی برای فایل‌های متنی می‌شود.</td>
<td>اعتبارسنجی را Lazy و Optional کنید؛ Extractorهای غیرفعال نباید در ساخت Composite خطا بدهند.</td>
</tr>
<tr>
<td>F-06</td>
<td><span class="badge high">بالا</span></td>
<td>xAiApi / Extractors / XFileContentExtractor + XAiRAGService.ExtractText</td>
<td>اولین Extractor منطبق انتخاب می‌شود؛ برای تصویر، XImage قبل از XVision قرار دارد و Text خالی برمی‌گرداند. برای PDF اسکن‌شده نیز Images نادیده گرفته می‌شود.</td>
<td>تصاویر و PDFهای اسکن‌شده وارد RAG نمی‌شوند.</td>
<td>زنجیره Fallback تعریف کنید؛ اگر Text خالی بود از Vision/OCR استفاده شود و Images نیز به مدل Vision داده شود.</td>
</tr>
<tr>
<td>F-07</td>
<td><span class="badge high">بالا</span></td>
<td>xAiApi / Startup.Configure</td>
<td>Middlewareهای Push، Database، String، Tag و File بعد از UseEndpoints ثبت شده‌اند.</td>
<td>برای درخواست‌های Controller که Endpoint آن‌ها Match می‌شود، این Middlewareها اجرا نمی‌شوند.</td>
<td>این Middlewareها را قبل از UseRouting/UseEndpoints منتقل کنید و ترتیب را مستند کنید.</td>
</tr>
<tr>
<td>F-08</td>
<td><span class="badge medium">متوسط</span></td>
<td>xAiApi / Providers / XAiRAGService.IngestAsync</td>
<td>وکتورهای قبلی قبل از Upsert جدید حذف می‌شوند.</td>
<td>در صورت خطای Upsert، داده قبلی از دست می‌رود.</td>
<td>از تراکنش استفاده کنید یا ابتدا Upsert سپس حذف موارد Stale با شناسه‌های جدید.</td>
</tr>
<tr>
<td>F-09</td>
<td><span class="badge medium">متوسط</span></td>
<td>xAiApi / Providers / XAiMcpServerProvider.FindOne/FindMany</td>
<td>داخل Expression از predicate.Compile()(x) استفاده شده است.</td>
<td>EF Core نمی‌تواند آن را ترجمه کند؛ خطا یا افت شدید عملکرد.</td>
<td>Expressionها را ترکیب کنید یا فیلتر مالکیت را بعد از واکشی اعمال کنید.</td>
</tr>
<tr>
<td>F-10</td>
<td><span class="badge medium">متوسط</span></td>
<td>xAiApi / Providers / XAiMcpServerProvider.GetMcpServer</td>
<td>قبل از بررسی مالکیت، null بودن result بررسی نمی‌شود.</td>
<td>NullReferenceException در صورت عدم وجود سرور.</td>
<td>اگر result null بود، NotFound پرتاب کنید.</td>
</tr>
<tr>
<td>F-11</td>
<td><span class="badge medium">متوسط</span></td>
<td>xAiService / Services / XAiMcpHandler.HandleToolCallAsync</td>
<td>toolName ممکن است null باشد و ContainsKey(null) خطا می‌دهد.</td>
<td>خطای 500 برای درخواست ناقص.</td>
<td>وجود params.name را اعتبارسنجی و خطای JSON-RPC مناسب برگردانید.</td>
</tr>
<tr>
<td>F-12</td>
<td><span class="badge medium">متوسط</span></td>
<td>xAiService / Services / XAiMcpHttpTransport.SendAsync</td>
<td>در حالت SSE، داده event: endpoint مانند "/mcp" به‌عنوان JSON-RPC دیسریالایز می‌شود.</td>
<td>خطای JsonException در اتصال SSE.</td>
<td>نوع Event را بررسی کنید و فقط داده‌های JSON-RPC را Parse کنید.</td>
</tr>
<tr>
<td>F-13</td>
<td><span class="badge medium">متوسط</span></td>
<td>xAiApi / Providers / XAiEmbeddingServiceBase</td>
<td>برای OpenAI در هر فراخوانی HttpClient جدید ساخته می‌شود.</td>
<td>خطر Socket Exhaustion و افت عملکرد.</td>
<td>از IHttpClientFactory یا یک HttpClient مشترک استفاده کنید.</td>
</tr>
<tr>
<td>F-14</td>
<td><span class="badge medium">متوسط</span></td>
<td>xAiModels / Providers / XAiDataProvider.SearchVectorsAsync</td>
<td>همه وکتورهای کاندید بارگذاری و Cosine در حافظه محاسبه می‌شود.</td>
<td>مقیاس‌پذیری پایین و مصرف بالای حافظه/CPU.</td>
<td>از Vector Index یا دیتابیس برداری استفاده کنید؛ حداقل صفحه‌بندی و فیلتر دقیق‌تر.</td>
</tr>
<tr>
<td>F-15</td>
<td><span class="badge medium">متوسط</span></td>
<td>xAiModels / Providers / XAiDataProvider.UpsertVectorAsync</td>
<td>در Upsert تکی از embedding.ToJSON() و در Batch از ToEmbedding() استفاده می‌شود.</td>
<td>احتمال فرمت ناسازگار Embedding و خطای خواندن.</td>
<td>از یک متد واحد مانند ToEmbedding استفاده کنید.</td>
</tr>
<tr>
<td>F-16</td>
<td><span class="badge low">پایین</span></td>
<td>xAiModels / Constants / XAiModelsConstants</td>
<td>مقدار XAiProjectCollectionName برابر "AiProjectsa" است.</td>
<td>نام‌گذاری GraphQL نادرست و گیج‌کننده.</td>
<td>به "aiProjects" اصلاح شود.</td>
</tr>
<tr>
<td>F-17</td>
<td><span class="badge low">پایین</span></td>
<td>xAiModels / Models / Entities / XAiMessage</td>
<td>ویژگی [StringLength(255)] روی Guid ConversationId اعمال شده است.</td>
<td>اعتبارسنجی و Migration گیج‌کننده.</td>
<td>StringLength را حذف کنید؛ در صورت نیاز از نوع مناسب استفاده شود.</td>
</tr>
<tr>
<td>F-18</td>
<td><span class="badge low">پایین</span></td>
<td>xAiModels / Extensions / XAiModelsExtensions</td>
<td>HasDefaultProject/HasDefaultConversation از ثابت‌های Resource متفاوت با GetDefault* استفاده می‌کنند.</td>
<td>در صورت استفاده، Default Project/Conversation شناسایی نمی‌شود.</td>
<td>ثابت‌های Resource را یکسان‌سازی و تست Add/Get Default اضافه کنید.</td>
</tr>
<tr>
<td>F-19</td>
<td><span class="badge low">پایین</span></td>
<td>xAiApi / Controllers / ChatWithDocumentsController.EmbedChunks</td>
<td>EmbeddingBatchSize بدون Guard استفاده می‌شود.</td>
<td>در صورت مقدار 0، حلقه بی‌نهایت رخ می‌دهد.</td>
<td>از Math.Max(1, batchSize) استفاده کنید.</td>
</tr>
<tr>
<td>F-20</td>
<td><span class="badge low">پایین</span></td>
<td>xAiApi / Middlewares / XAiModelProxyMiddleware</td>
<td>کل Body درخواست به‌صورت UTF-8 بافر و بازسازی می‌شود.</td>
<td>مصرف حافظه بالا و خرابی درخواست‌های Binary/Multipart.</td>
<td>برای JSON محدود کنید یا Streaming Proxy پیاده‌سازی کنید.</td>
</tr>
<tr>
<td>F-21</td>
<td><span class="badge low">پایین</span></td>
<td>xAiApi / Providers / XAiRAGService.IngestAsync</td>
<td>نام تکراری فایل در یک درخواست باعث Overwrite فایل قبلی می‌شود.</td>
<td>از دست رفتن فایل اول و شمارش نادرست.</td>
<td>نام‌های تکراری را Reject یا با Suffix یکتا کنید.</td>
</tr>
<tr>
<td>F-22</td>
<td><span class="badge low">پایین</span></td>
<td>xAiService / Services / XAiMcpClient</td>
<td>ListToolsAsync و CallToolAsync به userInfo.UserId دسترسی دارند بدون Validation.</td>
<td>NullReference در فراخوانی بدون Context.</td>
<td>در ابتدای متدها userInfo را Validate کنید.</td>
</tr>
<tr>
<td>F-23</td>
<td><span class="badge low">پایین</span></td>
<td>xAiApi / Providers / XAiMcpServerProvider.ConnectAndValidateAsync</td>
<td>خطای اتصال فقط Log می‌شود و به فراخواننده منتقل نمی‌شود.</td>
<td>Add/Update موفق به نظر می‌رسد اما سرور عملاً غیرقابل استفاده است.</td>
<td>وضعیت اتصال را در DTO برگردانید یا هشدار عملیاتی ثبت کنید.</td>
</tr>
<tr>
<td>F-24</td>
<td><span class="badge low">پایین</span></td>
<td>xAiModels / Providers / XAiDataProvider.CreateProject/CreateConversation</td>
<td>در صورت نبود TitleResource، Title خالی ذخیره می‌شود.</td>
<td>داده ناقص و تجربه کاربری نامناسب.</td>
<td>اعتبارسنجی Title/TitleResource یا تولید مقدار پیش‌فرض.</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>تحلیل امنیتی و کنترل دسترسی</h3>
<div class="subtitle">Security &amp; Access Control Deep Dive</div>
</div>
</div>
<div class="section-content">
<p>مهم‌ترین ریسک‌های امنیتی شناسایی‌شده مربوط به کنترل دسترسی در سطح Resource هستند. وجود Policyهای عمومی کافی نیست؛ هر متد باید مالکیت منبع را نیز بررسی کند.</p>
<div class="warning-box">
<strong>اقدام فوری:</strong>
<ul style="padding-right: 20px; margin-top: 10px;">
<li>افزودن بررسی مالکیت در LoadConversations، LoadMessages و CreateConversation.</li>
<li>اصلاح UpdateMcpServer برای واکشی رکورد موجود و بررسی OwnerId واقعی.</li>
<li>حذف [AllowAnonymous] از کلاس پایه MCP و اعمال آن فقط روی Endpointهای عمومی.</li>
<li>افزودن تست‌های IDOR برای تمام Endpointهای دارای ProjectId/ConversationId/McpServerId.</li>
</ul>
</div>
<div class="info-box">
<strong>الگوی پیشنهادی:</strong> یک سرویس <code>IAuthorizationService</code> داخلی یا <code>IResourceOwnershipGuard</code> بسازید که برای هر منبع، مالکیت را بررسی کند. سپس در لایه سرویس (نه فقط Controller) از آن استفاده شود تا حتی فراخوانی‌های داخلی نیز ایمن باشند.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>پیشنهادهای بهبود معماری و کیفیت</h3>
<div class="subtitle">Architecture &amp; Quality Recommendations</div>
</div>
</div>
<div class="section-content">
<ul class="reference-list">
<li>
<strong>۱. کنترل دسترسی متمرکز</strong>
بررسی مالکیت منابع در یک سرویس مشترک پیاده‌سازی شود. Controllerها نباید تنها نقطه اعمال Authorization باشند.
</li>
<li>
<strong>۲. بازطراحی Extractor Pipeline</strong>
Extractorها به‌صورت زنجیره‌ای با قابلیت Fallback طراحی شوند. اگر Extractor اول Text معتبر نداد، Extractor بعدی امتحان شود. برای تصاویر و PDF اسکن‌شده، Vision/OCR مسیر مشخص داشته باشد.
</li>
<li>
<strong>۳. اعتبارسنجی Lazy برای سرویس‌های Optional</strong>
OCR، Transcript و Vision نباید در Constructor سخت‌گیرانه کل Composite را از کار بیندازند. تنظیمات آن‌ها فقط هنگام استفاده همان Extractor بررسی شود.
</li>
<li>
<strong>۴. پایدارسازی MCP Client</strong>
ذخیره Mcp-Session-Id، مدیریت Retry/Timeout، پشتیبانی صحیح SSE و ثبت وضعیت اتصال سرورها ضروری است.
</li>
<li>
<strong>۵. ایمن‌سازی RAG Ingest</strong>
فرایند Ingest باید Idempotent و تا حد امکان تراکنشی باشد. حذف داده قبلی قبل از Upsert جدید انجام نشود. نام فایل‌های تکراری مدیریت شود.
</li>
<li>
<strong>۶. استفاده از Vector Store مناسب</strong>
برای حجم داده واقعی، محاسبه Cosine در حافظه کافی نیست. استفاده از SQLite Vector Extension، PostgreSQL + pgvector یا یک Vector DB توصیه می‌شود.
</li>
<li>
<strong>۷. حذف HttpClientهای موقت</strong>
برای Embedding و Model Proxy از IHttpClientFactory استفاده شود تا Socket Exhaustion رخ ندهد.
</li>
<li>
<strong>۸. اعتبارسنجی Configuration در Startup</strong>
علاوه بر اعتبارسنجی کلی، وابستگی‌های Optional نیز مشخص شوند. نبود OCR/Transcript نباید مانع استخراج فایل‌های متنی شود.
</li>
<li>
<strong>۹. تست‌های امنیتی و Integration</strong>
تست IDOR، تست MCP Session، تست SSE، تست Ingest مجدد و تست استخراج تصویر/PDF اسکن‌شده اضافه شود.
</li>
<li>
<strong>۱۰. مشاهدپذیری و پایش</strong>
برای MCP، RAG و Ingest لاگ ساختاریافته، CorrelationId، متریک latency/error rate و Health Check اضافه شود.
</li>
</ul>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>نقشه راه اصلاحات پیشنهادی</h3>
<div class="subtitle">Recommended Remediation Roadmap</div>
</div>
</div>
<div class="section-content">
<table class="styled-table">
<thead>
<tr>
<th>فاز</th>
<th>بازه پیشنهادی</th>
<th>تمرکز</th>
<th>یافته‌های مرتبط</th>
</tr>
</thead>
<tbody>
<tr class="highlight-row">
<td><strong>فاز ۱ — امنیت</strong></td>
<td>۰ تا ۲ هفته</td>
<td>رفع IDOR، اصلاح مالکیت MCP، حذف AllowAnonymous خطرناک، Null Checkهای امنیتی</td>
<td>F-01, F-02, F-04, F-10, F-11</td>
</tr>
<tr>
<td><strong>فاز ۲ — پایداری MCP و RAG</strong></td>
<td>۲ تا ۴ هفته</td>
<td>مدیریت Session، اصلاح SSE، Fallback Extractorها، ترتیب Middleware، ایمن‌سازی Ingest</td>
<td>F-03, F-05, F-06, F-07, F-08, F-09, F-12</td>
</tr>
<tr>
<td><strong>فاز ۳ — عملکرد و مقیاس‌پذیری</strong></td>
<td>۴ تا ۸ هفته</td>
<td>Vector Store مناسب، HttpClientFactory، Streaming Proxy، یکسان‌سازی Embedding</td>
<td>F-13, F-14, F-15, F-20</td>
</tr>
<tr>
<td><strong>فاز ۴ — کیفیت و پایش</strong></td>
<td>مستمر</td>
<td>تست‌های امنیتی، تست Integration، Health Check، لاگ ساختاریافته، مستندسازی</td>
<td>سایر یافته‌ها و پیشنهادها</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۷</div>
<div class="section-title">
<h3>جمع‌بندی و توصیه نهایی</h3>
<div class="subtitle">Final Summary &amp; Recommendation</div>
</div>
</div>
<div class="section-content">
<div class="conclusion-box">
<h3>🎯 توصیه نهایی</h3>
<p>سامانه xAi پایه معماری خوبی دارد؛ اما بدهی امنیتی و پایداری آن باید پیش از توسعه قابلیت‌های جدید پرداخت شود.</p>
<p>مهم‌ترین اولویت‌ها عبارت‌اند از: اصلاح کنترل دسترسی مبتنی بر مالکیت، پایدارسازی MCP Session، بهبود Extractors برای تصویر/PDF و ایمن‌سازی فرایند Ingest.</p>
<div class="highlight">
<strong>💎 ارزش ایجادشده:</strong><br>
کاهش ریسک دسترسی غیرمجاز + پایداری اتصال MCP + جلوگیری از از دست رفتن داده + مقیاس‌پذیری بهتر RAG
</div>
</div>
<div class="info-box">
<strong>📌 گام بعدی پیشنهادی:</strong> یک Sprint اصلاحی کوتاه برای فاز ۱ تعریف شود و پس از آن، تست‌های امنیتی و Integration مربوط به MCP/RAG به Pipeline CI اضافه گردد.
</div>
</div>
</div>
<div class="page-info">
این مستند محرمانه بوده و صرفاً جهت استفاده تیم توسعه تهیه شده است. بررسی انجام‌شده از نوع استاتیک بوده و جایگزین تست نفوذ یا تست اجرایی نیست.
</div>
</div>
<footer class="document-footer">
<div class="footer-brand">گزارش بازبینی کد سامانه xAi</div>
<div class="footer-divider"></div>
<div>تاریخ تهیه: آبان ۱۴۰۵ &nbsp;|&nbsp; نسخه: ۱.۰ &nbsp;|&nbsp; طبقه‌بندی: محرمانه</div>
</footer>
</div>
</body>
</html>
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff