@@ -4,6 +4,7 @@ import os from 'node:os'
4
4
import path from 'node:path'
5
5
import type { Readable , Writable } from 'node:stream'
6
6
import { exec , log , runCommand , runCommands } from '@stacksjs/cli'
7
+ import { glob } from '@stacksjs/storage'
7
8
import forge , { pki , tls } from 'node-forge'
8
9
import { resolveConfig } from './config'
9
10
import type { GenerateCertOptions } from './types'
@@ -287,7 +288,6 @@ export async function addCertToSystemTrustStoreAndSaveCerts(
287
288
CAcert : string ,
288
289
options ?: AddCertOptions ,
289
290
) {
290
- // console.log((await runCommand(`certutil -d sql:${os.homedir()}/.pki/nssdb -L -n ${DEFAULT_O}`)).isOk())
291
291
const certPath = storeCert ( cert , options )
292
292
const CAcertPath = storeCACert ( CAcert , options )
293
293
@@ -306,21 +306,56 @@ export async function addCertToSystemTrustStoreAndSaveCerts(
306
306
// Linux (This might vary based on the distro)
307
307
// for Ubuntu/Debian based systems
308
308
309
- // delete existing cert from system trust store
310
- console . warn = async ( ) => {
311
- // ignore error if no cert exists
312
- await runCommand ( `certutil -d sql:${ os . homedir ( ) } /.pki/nssdb -D -n ${ DEFAULT_O } ` )
313
- await runCommand (
314
- `certutil -d sql:${ os . homedir ( ) } /snap/firefox/common/.mozilla/firefox/3l148raz.default -D -n ${ DEFAULT_O } ` ,
315
- )
309
+ // return all directories that contain cert9.db file using fs.readdirSync
310
+
311
+ function findFoldersWithFile ( rootDir : string , fileName : string ) : string [ ] {
312
+ const result : string [ ] = [ ]
313
+
314
+ function search ( dir : string ) {
315
+ try {
316
+ const files = fs . readdirSync ( dir )
317
+
318
+ for ( const file of files ) {
319
+ const filePath = path . join ( dir , file )
320
+ const stats = fs . lstatSync ( filePath ) // Use fs.lstatSync instead
321
+
322
+ if ( stats . isDirectory ( ) ) {
323
+ search ( filePath )
324
+ } else if ( file === fileName ) {
325
+ result . push ( dir )
326
+ }
327
+ }
328
+ } catch ( error ) {
329
+ // Handle any errors (e.g., broken links, permission issues)
330
+ }
331
+ }
332
+
333
+ search ( rootDir )
334
+ return result
316
335
}
336
+ //
337
+ const rootDirectory = `${ os . homedir ( ) } `
338
+ const targetFileName = 'cert9.db'
339
+ const foldersWithFile = findFoldersWithFile ( rootDirectory , targetFileName )
340
+
341
+ foldersWithFile . map ( async ( folder ) => {
342
+ // delete existing cert from system trust store
343
+ console . warn = async ( ) => {
344
+ // ignore error if no cert exists
345
+ await runCommand ( `certutil -d sql:${ folder } -D -n ${ DEFAULT_O } ` )
346
+ }
347
+ } )
317
348
318
349
await runCommands ( [
319
350
`sudo cp ${ certPath } /usr/local/share/ca-certificates/` ,
320
351
321
352
// add new cert to system trust store
322
353
`certutil -d sql:${ os . homedir ( ) } /.pki/nssdb -A -t ${ args } -n ${ DEFAULT_O } -i ${ CAcertPath } ` ,
323
354
355
+ // add new cert to system trust store for Brave
356
+ `certutil -d sql:${ os . homedir ( ) } /snap/brave/411/.pki/nssdb -A -t ${ args } -n ${ DEFAULT_O } -i ${ CAcertPath } ` ,
357
+
358
+ // add new cert to system trust store for Firefox
324
359
`certutil -d sql:${ os . homedir ( ) } /snap/firefox/common/.mozilla/firefox/3l148raz.default -A -t ${ args } -n ${ DEFAULT_O } -i ${ CAcertPath } ` ,
325
360
326
361
// reload system trust store
0 commit comments