-
Notifications
You must be signed in to change notification settings - Fork 0
Develop #4
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
Develop #4
Changes from all commits
376d091
f598d1a
01e1872
ef807f6
841f15f
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 |
|---|---|---|
|
|
@@ -174,4 +174,34 @@ export const listScansForOrg = async (userId: string, organizationId: string) => | |
| }); | ||
|
|
||
| return scans; | ||
| }; | ||
|
|
||
| /** | ||
| * يلغي فحصاً جارياً أو قيد الانتظار. | ||
| * @param userId - معرف المستخدم. | ||
| * @param scanId - معرف الفحص. | ||
| */ | ||
| export const cancelScan = async (userId: string, scanId: string) => { | ||
| // 1. التأكد من وجود الفحص وصلاحيات المستخدم | ||
| const scan = await getScanById(userId, scanId); | ||
|
|
||
| // 2. تحديث الحالة فقط إذا كانت RUNNING أو QUEUED أو PENDING | ||
| const activeStatuses = ['RUNNING', 'QUEUED', 'PENDING']; | ||
| if (activeStatuses.includes(scan.status)) { | ||
| await prisma.scan.updateMany({ | ||
| where: { | ||
| id: scanId, | ||
| status: { in: activeStatuses as ScanStatus[] }, | ||
| }, | ||
| data: { | ||
| status: ScanStatus.CANCELED, | ||
| completedAt: new Date() // نعتبره مكتملاً (متوقفاً) لتسجيل الوقت | ||
| } | ||
| }); | ||
| // ملاحظة: إيقاف الـ Job الفعلي من BullMQ يتطلب معرف JobId. | ||
| // في هذه المرحلة، نكتفي بتحديث حالة قاعدة البيانات. | ||
| // الـ Worker يجب أن يتحقق من حالة الفحص في قاعدة البيانات قبل الاستمرار. | ||
| } | ||
|
|
||
| return true; | ||
| }; | ||
|
Comment on lines
+184
to
207
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. Cancellation needs to be atomic and job-aware. The current read-then-update flow leaves a TOCTOU window, and the BullMQ job is never removed or stopped. A scan can still finish after cancellation and be written back as Please make the state transition conditional in one step and coordinate queue removal or an early worker exit before returning success. 🤖 Prompt for AI Agents |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,11 @@ export async function runSqliScan(job: Job, prisma: PrismaClient): Promise<void> | |
| if (await executeAuthBypassAttack(job, prisma)) totalVulnerabilities++; | ||
|
|
||
| await job.updateProgress(10); // تحديث التقدم المبدئي | ||
| // 🆕 حفظ التقدم الأولي | ||
| try { | ||
| // @ts-ignore: Prisma Client types might be stale | ||
| await prisma.scan.update({ where: { id: scanId }, data: { progress: 10 } }); | ||
| } catch (e) { /* Ignore stale client error */ } | ||
|
|
||
| // --- المرحلة الثانية: الفحص الدقيق لكل بارامتر (Waves 2-6) --- | ||
| // نحتفظ بنسخة من البيانات الأصلية لتجنب تلوث البيانات أثناء التكرار | ||
|
|
@@ -73,6 +78,18 @@ export async function runSqliScan(job: Job, prisma: PrismaClient): Promise<void> | |
| // المعادلة: نوزع 80% من التقدم على هذه المرحلة | ||
| const progress = 10 + Math.floor(((i + 1) / totalParams) * 80); | ||
| await job.updateProgress(progress); | ||
|
|
||
| // 🆕 تحديث التقدم في قاعدة البيانات للعرض في الواجهة (مع حماية من الأخطاء) | ||
| try { | ||
| // @ts-ignore: Prisma Client types might be stale | ||
| await prisma.scan.update({ | ||
| where: { id: scanId }, | ||
| data: { progress: progress } | ||
| }); | ||
| } catch (e) { | ||
| // قد يفشل إذا لم يتم تحديث Prisma Client بعد، نتجاهل الخطأ لكي لا يتوقف الفحص | ||
| console.warn('[Orchestrator] Failed to sync progress to DB (Non-fatal)'); | ||
| } | ||
| } | ||
|
|
||
| // استعادة البيانات الأصلية للمرحلة الأخيرة | ||
|
|
@@ -84,12 +101,15 @@ export async function runSqliScan(job: Job, prisma: PrismaClient): Promise<void> | |
| if (await executeSecondOrderAttack(job, prisma)) totalVulnerabilities++; | ||
|
|
||
| await job.updateProgress(100); | ||
| // لا نحتاج لتحديث التقدم هنا لأن التحديث الأخير سيضع الحالة COMPLETED والتقدم 100 | ||
|
|
||
| // --- إتمام المهمة --- | ||
| // @ts-ignore: Prisma Client types might be stale | ||
| await prisma.scan.update({ | ||
| where: { id: scanId }, | ||
| data: { | ||
| status: 'COMPLETED', | ||
| progress: 100, // 🆕 تأكيد الوصول لـ 100% | ||
|
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. P2: The final completion write unconditionally sets Prompt for AI agents |
||
| completedAt: new Date(), | ||
| // يمكن إضافة حقل لعدد النتائج إذا كان مدعوماً في قاعدة البيانات | ||
| // findingsCount: totalVulnerabilities | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,9 @@ | ||||||||||||||||||||||||||||||
| export type ScanStatus = 'PENDING' | 'RUNNING' | 'COMPLETED' | 'FAILED' | 'STOPPED'; | ||||||||||||||||||||||||||||||
| export type ScanStatus = 'PENDING' | 'RUNNING' | 'COMPLETED' | 'FAILED' | 'CANCELED'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export interface Scan { | ||||||||||||||||||||||||||||||
| id: string; | ||||||||||||||||||||||||||||||
| status: ScanStatus; | ||||||||||||||||||||||||||||||
| progress?: number; // 🆕 نسبة التقدم (0-100) | ||||||||||||||||||||||||||||||
| startedAt: string | null; | ||||||||||||||||||||||||||||||
| completedAt: string | null; | ||||||||||||||||||||||||||||||
| createdAt: string; | ||||||||||||||||||||||||||||||
|
Comment on lines
3
to
9
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. Make The backend now guarantees a numeric progress value for every scan, so keeping this optional only forces consumers into ♻️ Suggested tweak export interface Scan {
id: string;
status: ScanStatus;
- progress?: number; // 🆕 نسبة التقدم (0-100)
+ progress: number; // 0-100
startedAt: string | null;
completedAt: string | null;
createdAt: string;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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.
P2: The BullMQ job is never removed or signaled to stop after marking the scan as
CANCELED. The worker will continue processing and could overwrite the status back toCOMPLETEDwhen it finishes, making the cancel appear successful while work continues. Consider storing the BullMQ job ID on the scan record and callingjob.remove()or using theAbortSignal-based cancellation pattern here, or at minimum ensure the worker checks the DB status before writing terminal states.Prompt for AI agents
Tip: Review your code locally with the cubic CLI to iterate faster.