فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

fix(holoo): enforce least privilege before lab SQL counts

Davood Mehraban committed Nov 22, 2026 at 22:12 UTC 2f54acf60adcc71be9d3ad3df9758a2496466752
2 files changed +20 -6
scripts/holoo-lab-counts.ps1
+6
@@ -5,7 +5,13 @@ if(-not $QueryId){throw 'QueryId is required'}
5 $preflight=Join-Path $PSScriptRoot 'holoo-direct-sql-readiness.ps1'
6 $raw=& powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $preflight -Json
7 if($LASTEXITCODE -ne 0 -or -not ($raw|ConvertFrom-Json).readyForLabSql){throw 'Approved lab preflight failed'}
8 +$permissionScript=Join-Path $PSScriptRoot 'holoo-sql-permissions-audit.ps1'
9 +$permissionJson=& powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $permissionScript -Json
10 +if($LASTEXITCODE -ne 0){throw 'SQL least-privilege gate BLOCKED: count query not executed'}
11 +$permission=$permissionJson|ConvertFrom-Json
12 +if($permission.status -ne 'PASS' -or -not $permission.leastPrivilegeReadOnly){throw 'SQL least-privilege gate BLOCKED: count query not executed'}
13 $queries=@{invoiceCount='SELECT COUNT_BIG(*) FROM dbo.FACTURE';lineCount='SELECT COUNT_BIG(*) FROM dbo.FACTART';documentCount='SELECT COUNT_BIG(*) FROM dbo.SANAD'}
14 Add-Type -AssemblyName System.Data
15 $c=New-Object System.Data.SqlClient.SqlConnection('Server=ARAM\TNC;Database=HolooBackup_050808_LAB_20261110;Integrated Security=True;Connection Timeout=5;Application Name=FinoraHolooLabCounts;ApplicationIntent=ReadOnly')
16 try {$c.Open();$q=$c.CreateCommand();$q.CommandTimeout=15;$q.CommandText=$queries[$QueryId];$value=[long]$q.ExecuteScalar();[pscustomobject]@{database='HolooBackup_050808_LAB_20261110';queryId=$QueryId;count=$value;readOnly=$true}|ConvertTo-Json -Compress} finally {$c.Dispose()}
17 +
tests/holoo-lab-reader.test.mjs
+14 -6
@@ -1,17 +1,25 @@
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 +import { readFileSync } from 'node:fs';
4 +import { fileURLToPath } from 'node:url';
5 import { createRequire } from 'node:module';
6 const require = createRequire(import.meta.url);
7 const { readApprovedLabCount } = require('../src/holoo-lab-reader.cjs');
8 const database='HolooBackup_050808_LAB_20261110';
7 -test('real SQL lab counts match verified structural audit', {skip:process.platform!=='win32'||process.env.HOLOO_SQL_INTEGRATION!=='1'}, async()=>{
8 - const {loadApprovedHolooAudit}=require('../src/holoo-audit-loader.cjs');
9 - const audit=loadApprovedHolooAudit();
10 - for(const [queryId,field] of [['invoiceCount','invoiceHeaders'],['lineCount','invoiceLines'],['documentCount','accountingDocuments']]) {
11 - assert.equal(await readApprovedLabCount({database,queryId}),audit.integrity[field],queryId);
12 - }
9 +test('real SQL lab count requests are blocked while Windows login has write privileges', {skip:process.platform!=='win32'||process.env.HOLOO_SQL_INTEGRATION!=='1'}, async()=>{
10 + await assert.rejects(readApprovedLabCount({database,queryId:'invoiceCount'}),/Approved lab count query failed/);
11 });
12 test('rejects production database and arbitrary SQL without running queries',async()=>{
13 await assert.rejects(readApprovedLabCount({database:'Holoo1',queryId:'invoiceCount'}));
14 await assert.rejects(readApprovedLabCount({database,queryId:'DELETE FROM FACTURE'}));
15 });
16 +
17 +
18 +test('count SQL is gated by effective permission audit before connection',()=>{
19 + const source=readFileSync(fileURLToPath(new URL('../scripts/holoo-lab-counts.ps1',import.meta.url)),'utf8');
20 + const gate=source.indexOf('holoo-sql-permissions-audit.ps1');
21 + const connection=source.indexOf('New-Object System.Data.SqlClient.SqlConnection');
22 + assert.ok(gate>=0 && connection>gate);
23 + assert.match(source,/leastPrivilegeReadOnly/);
24 + assert.match(source,/SQL least-privilege gate BLOCKED/);
25 +});