@@ -249,6 +249,32 @@ async function startServer() {
249249 } ) ;
250250 app . use ( "/api/files/" , express . static ( path . join ( __dirname , '../../client/shlug-files/' ) ) ) ;
251251
252+
253+ let lastCertUpdateTime : Date | undefined = undefined
254+ async function updateRootCert ( ) {
255+ try {
256+ const url = process . env . READER_CERT_URL ;
257+ if ( url == undefined || url == "" ) {
258+ console . error ( "Can not update root cert. No download URL provided" ) ;
259+ return
260+ }
261+ const response = await fetch ( url ) ;
262+
263+ if ( ! response . ok ) {
264+ console . error ( `Could not download new root cert. HTTP error: ${ response . status } ` )
265+ return ;
266+ }
267+
268+ const certString = await response . text ( ) ;
269+ // normalize \r\n to \n
270+ setReaderCertCA ( certString . replace ( / \r / g, "" ) ) ;
271+ console . log ( "Successfully updated root cert. New cert ends in " , certString . substring ( certString . length - 50 , certString . length - 27 ) )
272+ lastCertUpdateTime = new Date ( )
273+ } catch ( error ) {
274+ console . error ( `Failed to update root cert: ${ error } ` )
275+ }
276+ }
277+
252278 app . get ( "/api/rootCA" , async function ( req , res ) {
253279 const SNHeader = 'shlug-sn' ;
254280 if ( ! req . headers [ SNHeader ] ) {
@@ -264,6 +290,12 @@ async function startServer() {
264290 return res . status ( 404 ) . send ( ) ;
265291 }
266292
293+ // only update cert if its been an hour since the last time a shlug said its out of date
294+ // prevent an easily ddos-able endpoint
295+ if ( lastCertUpdateTime == undefined || Math . abs ( Date . now ( ) - lastCertUpdateTime . getTime ( ) ) >= 1000 * 60 * 60 ) {
296+ updateRootCert ( ) ;
297+ }
298+
267299 const certca = ( await getReaderCertCA ( ) ) ?. value ;
268300 if ( certca == null ) {
269301 return res . status ( 404 ) . send ( ) ;
@@ -273,7 +305,7 @@ async function startServer() {
273305 const result = {
274306 cert : certca ,
275307 sha : sha ,
276- }
308+ }
277309 return res . json ( result ) ;
278310 } )
279311
@@ -562,27 +594,6 @@ async function startServer() {
562594 createUnassocaitedAuditLog ( `Trainings: Sent ${ numNotified } expiry notices, and purged ${ numPurged } expired trainings.` , "server" )
563595
564596 }
565- async function updateRootCert ( ) {
566- try {
567- const url = process . env . READER_CERT_URL ;
568- if ( url == undefined || url == "" ) {
569- console . error ( "Can not update root cert. No download URL provided" ) ;
570- return
571- }
572- const response = await fetch ( url ) ;
573-
574- if ( ! response . ok ) {
575- console . error ( `Could not download new root cert. HTTP error: ${ response . status } ` )
576- return ;
577- }
578-
579- const certString = await response . text ( ) ;
580- // normalize \r\n to \n
581- setReaderCertCA ( certString . replace ( / \r / g, "" ) ) ;
582- } catch ( error ) {
583- console . error ( `Failed to update root cert: ${ error } ` )
584- }
585- }
586597 /**
587598 Cron Format:
588599 * * * * * *
0 commit comments