crypto-inventory 0.4.0: Swift and Kotlin detectors (CryptoKit, CommonCrypto, Security.framework, Android Keystore, Tink, OkHttp, the libsignal Swift and Android APIs), Apple and Android manifests; remediation recipes for Apache and HAProxy measured on real servers, HAProxy precondition fixed
This commit is contained in:
1 parent
e453c0c42d
commit
b6ed4d2b71
37 files changed
+1623
-82
No files matched your search
@@ -16,10 +16,199 @@ export function curata(text, limbaj) {
|
||||
case 'go': return curataC(text, { raw: true });
|
||||
case 'python': return curataPython(text);
|
||||
case 'rust': return curataRust(text);
|
||||
case 'swift': return curataSwift(text);
|
||||
case 'kotlin': return curataKotlin(text);
|
||||
default: return { code: text, siruri: [] };
|
||||
}
|
||||
}
|
||||
|
||||
// Sfarsitul unui comentariu bloc IMBRICAT (/* a /* b */ c */) care incepe la i; n daca nu se inchide. Comun pentru Rust, Swift si
|
||||
// Kotlin: toate trei imbrica; un lexer C ar fi inchis comentariul la primul */ si ar fi lasat restul drept cod.
|
||||
function sfarsitComentariuImbricat(text, i, n) {
|
||||
let j = i + 2;
|
||||
let ad = 1;
|
||||
while (j < n && ad > 0) {
|
||||
if (text[j] === '/' && text[j + 1] === '*') { ad++; j += 2; continue; }
|
||||
if (text[j] === '*' && text[j + 1] === '/') { ad--; j += 2; continue; }
|
||||
j++;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
// Swift (0.4.0, 2026-10-18): comentariile bloc se imbrica; sirurile sunt "..." (un singur rand), """...""" (multi-rand), si formele
|
||||
// brute #"..."# / #"""..."""# cu oricate diezuri; INTERPOLAREA \( expr ) (in sirurile brute \#( expr )) poate contine la randul ei
|
||||
// siruri si paranteze, deci sfarsitul sirului se cauta cu o stiva de cadre, nu cu "pana la urmatorul ghilimea". Nu exista literale
|
||||
// char. Scanarea e ITERATIVA (stiva explicita): un fisier facut anume din `"\(` repetat ar fi 85.000 de niveluri de recursie.
|
||||
// Continutul sirului (interpolare inclusa) ramane un singur interval: un nume de API scris in interpolare nu e o folosire masurata.
|
||||
function sfarsitSirSwift(text, start, n) {
|
||||
// cadre: { fel: 'sir', diezi, multi } sau { fel: 'interp', ad } (ad = paranteze deschise in interpolare)
|
||||
const stiva = [];
|
||||
const deschide = (j) => {
|
||||
let d = 0;
|
||||
while (text[j] === '#') { d++; j++; }
|
||||
if (text[j] !== '"') return -1;
|
||||
const multi = text.startsWith('"""', j);
|
||||
stiva.push({ fel: 'sir', diezi: d, multi });
|
||||
return j + (multi ? 3 : 1);
|
||||
};
|
||||
let j = deschide(start);
|
||||
if (j < 0) return -1;
|
||||
while (j < n && stiva.length) {
|
||||
const cadru = stiva[stiva.length - 1];
|
||||
const c = text[j];
|
||||
if (cadru.fel === 'sir') {
|
||||
if (c === '\\') {
|
||||
// escape-ul unui sir brut e \ urmat de exact `diezi` diezi; un \ singur in sirul brut e un caracter obisnuit
|
||||
let k = j + 1;
|
||||
let d = 0;
|
||||
while (d < cadru.diezi && text[k] === '#') { d++; k++; }
|
||||
if (d < cadru.diezi) { j++; continue; }
|
||||
if (text[k] === '(') { stiva.push({ fel: 'interp', ad: 1 }); j = k + 1; continue; }
|
||||
j = k + 1; continue;
|
||||
}
|
||||
if (c === '"') {
|
||||
const inchidere = (cadru.multi ? '"""' : '"') + '#'.repeat(cadru.diezi);
|
||||
if (text.startsWith(inchidere, j)) { stiva.pop(); j += inchidere.length; continue; }
|
||||
j++; continue;
|
||||
}
|
||||
if (c === '\n' && !cadru.multi) { stiva.pop(); j++; continue; } // sir neterminat pe rand: se inchide la capatul randului
|
||||
j++; continue;
|
||||
}
|
||||
// in interpolare: paranteze echilibrate, siruri imbricate
|
||||
if (c === '(') { cadru.ad++; j++; continue; }
|
||||
if (c === ')') { if (--cadru.ad === 0) stiva.pop(); j++; continue; }
|
||||
if (c === '"' || (c === '#' && /^#+"/.test(text.slice(j, j + 8)))) {
|
||||
const k = deschide(j);
|
||||
if (k >= 0) { j = k; continue; }
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return Math.min(j, n);
|
||||
}
|
||||
|
||||
function curataSwift(text) {
|
||||
const n = text.length;
|
||||
const out = text.split('');
|
||||
const goleste = golitor(out);
|
||||
const siruri = [];
|
||||
let i = 0;
|
||||
while (i < n) {
|
||||
const c = text[i];
|
||||
const d = text[i + 1];
|
||||
if (c === '/' && d === c) { // (scris altfel decat in lexerul C si in cel Rust: controlul negativ isi ancoreaza cioturile pe acelea)
|
||||
let j = i;
|
||||
while (j < n && text[j] !== '\n') j++;
|
||||
goleste(i, j); i = j; continue;
|
||||
}
|
||||
if (c === '/' && d === '*') {
|
||||
const jSw = sfarsitComentariuImbricat(text, i, n);
|
||||
goleste(i, jSw); i = jSw; continue;
|
||||
}
|
||||
if (c === '"' || (c === '#' && (d === '#' || d === '"'))) {
|
||||
const sf = sfarsitSirSwift(text, i, n);
|
||||
if (sf > i) { siruri.push([i, sf]); i = sf; continue; }
|
||||
}
|
||||
if (/[A-Za-z_]/.test(c)) {
|
||||
let j = i + 1;
|
||||
while (j < n && /\w/.test(text[j])) j++;
|
||||
i = j; continue;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return { code: out.join(''), siruri };
|
||||
}
|
||||
|
||||
// Kotlin (0.4.0, 2026-10-18): comentariile bloc se imbrica; sirurile sunt "..." (un singur rand, cu escape-uri) si """...""" (brute,
|
||||
// fara escape-uri); amandoua au sabloane $nume si ${ expr }, iar expr poate contine siruri si acolade, deci sfarsitul se cauta cu o
|
||||
// stiva de cadre, iterativ. 'c' e un literal char ('a', '\n', 'A', '\''), nu deschide un sir. Continutul sirului ramane un singur
|
||||
// interval: un nume de API scris intr-un sablon nu e o folosire masurata.
|
||||
function sfarsitSirKotlin(text, start, n) {
|
||||
const stiva = []; // { fel: 'sir', brut } | { fel: 'sablon', ad }
|
||||
const deschide = (j) => {
|
||||
if (text[j] !== '"') return -1;
|
||||
const brut = text.startsWith('"""', j);
|
||||
stiva.push({ fel: 'sir', brut });
|
||||
return j + (brut ? 3 : 1);
|
||||
};
|
||||
let j = deschide(start);
|
||||
if (j < 0) return -1;
|
||||
while (j < n && stiva.length) {
|
||||
const cadru = stiva[stiva.length - 1];
|
||||
const c = text[j];
|
||||
if (cadru.fel === 'sir') {
|
||||
if (c === '\\' && !cadru.brut) { j += 2; continue; }
|
||||
if (c === '$' && text[j + 1] === '{') { stiva.push({ fel: 'sablon', ad: 1 }); j += 2; continue; }
|
||||
if (c === '"') {
|
||||
if (cadru.brut) {
|
||||
if (text.startsWith('"""', j)) { j += 3; while (text[j] === '"') j++; stiva.pop(); continue; } // """"" inchide si pastreaza ghilimelele in plus
|
||||
j++; continue;
|
||||
}
|
||||
stiva.pop(); j++; continue;
|
||||
}
|
||||
if (c === '\n' && !cadru.brut) { stiva.pop(); j++; continue; }
|
||||
j++; continue;
|
||||
}
|
||||
if (c === '{') { cadru.ad++; j++; continue; }
|
||||
if (c === '}') { if (--cadru.ad === 0) stiva.pop(); j++; continue; }
|
||||
if (c === '"') { const k = deschide(j); if (k >= 0) { j = k; continue; } }
|
||||
if (c === "'") { const k = sfarsitCharKotlin(text, j, n); if (k > 0) { j = k; continue; } }
|
||||
j++;
|
||||
}
|
||||
return Math.min(j, n);
|
||||
}
|
||||
|
||||
// literal char Kotlin la i: 'x', '\n', '\'', 'A', un caracter astral; -1 daca la i nu incepe un literal char
|
||||
function sfarsitCharKotlin(text, i, n) {
|
||||
const d = text[i + 1];
|
||||
if (d === undefined || d === '\n' || d === "'") return -1;
|
||||
if (d === '\\') {
|
||||
let j = i + 2;
|
||||
while (j < n && text[j] !== "'" && text[j] !== '\n' && j - i < 8) j++;
|
||||
return text[j] === "'" ? j + 1 : -1;
|
||||
}
|
||||
if (text[i + 2] === "'") return i + 3;
|
||||
const cod = d.charCodeAt(0);
|
||||
if (cod >= 0xd800 && cod <= 0xdbff && text[i + 3] === "'") return i + 4;
|
||||
return -1;
|
||||
}
|
||||
|
||||
function curataKotlin(text) {
|
||||
const n = text.length;
|
||||
const out = text.split('');
|
||||
const goleste = golitor(out);
|
||||
const siruri = [];
|
||||
let i = 0;
|
||||
while (i < n) {
|
||||
const c = text[i];
|
||||
const d = text[i + 1];
|
||||
if (c === d && d === '/') { // (scris altfel decat in lexerele C, Rust si Swift: fiecare ciot al controlului negativ are ancora lui)
|
||||
let j = i;
|
||||
while (j < n && text[j] !== '\n') j++;
|
||||
goleste(i, j); i = j; continue;
|
||||
}
|
||||
if (c === '/' && d === '*') {
|
||||
const jKt = sfarsitComentariuImbricat(text, i, n);
|
||||
goleste(i, jKt); i = jKt; continue;
|
||||
}
|
||||
if (c === '"') {
|
||||
const sf = sfarsitSirKotlin(text, i, n);
|
||||
if (sf > i) { siruri.push([i, sf]); i = sf; continue; }
|
||||
}
|
||||
if (c === "'") {
|
||||
const sf = sfarsitCharKotlin(text, i, n);
|
||||
if (sf > 0) { siruri.push([i, sf]); i = sf; continue; }
|
||||
i++; continue;
|
||||
}
|
||||
if (/[A-Za-z_]/.test(c)) {
|
||||
let j = i + 1;
|
||||
while (j < n && /\w/.test(text[j])) j++;
|
||||
i = j; continue;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return { code: out.join(''), siruri };
|
||||
}
|
||||
|
||||
function golitor(out) {
|
||||
return (a, b) => {
|
||||
for (let k = a; k < b; k++) if (out[k] !== '\n' && out[k] !== '\r') out[k] = ' ';
|
||||
@@ -218,13 +407,7 @@ function curataRust(text) {
|
||||
goleste(i, j); i = j; continue;
|
||||
}
|
||||
if (c === '/' && d === '*') {
|
||||
let j = i + 2;
|
||||
let ad = 1;
|
||||
while (j < n && ad > 0) {
|
||||
if (text[j] === '/' && text[j + 1] === '*') { ad++; j += 2; continue; }
|
||||
if (text[j] === '*' && text[j + 1] === '/') { ad--; j += 2; continue; }
|
||||
j++;
|
||||
}
|
||||
const j = sfarsitComentariuImbricat(text, i, n); // (0.4.0: aceeasi functie pentru Rust, Swift si Kotlin)
|
||||
goleste(i, j); i = j; continue;
|
||||
}
|
||||
const dupaCuvant = i > 0 && /\w/.test(text[i - 1]);
|
||||
|
||||
Reference in new issue
Block a user