3434import org .slf4j .Logger ;
3535import org .slf4j .LoggerFactory ;
3636
37+ import com .vaadin .flow .internal .MessageDigestUtil ;
3738import com .vaadin .flow .internal .Pair ;
3839import com .vaadin .flow .server .frontend .FrontendUtils ;
3940import com .vaadin .flow .server .frontend .FrontendVersion ;
@@ -55,6 +56,8 @@ public class NodeInstaller {
5556
5657 public static final String UNOFFICIAL_NODEJS_DOWNLOAD_ROOT = "https://unofficial-builds.nodejs.org/download/release/" ;
5758
59+ public static final String SHA_SUMS_FILE = "SHASUMS256.txt" ;
60+
5861 private static final String NODE_WINDOWS = INSTALL_PATH .replaceAll ("/" ,
5962 "\\ \\ " ) + "\\ node.exe" ;
6063 private static final String NODE_DEFAULT = INSTALL_PATH + "/node" ;
@@ -64,6 +67,7 @@ public class NodeInstaller {
6467 private static final int MAX_DOWNLOAD_ATTEMPS = 5 ;
6568
6669 private static final int DOWNLOAD_ATTEMPT_DELAY = 5 ;
70+ public static final String ACCEPT_MISSING_SHA = "vaadin.node.download.acceptMissingSHA" ;
6771
6872 private final Object lock = new Object ();
6973
@@ -524,6 +528,8 @@ private void downloadFileIfMissing(URI downloadUrl, File destination,
524528 try {
525529 fileDownloader .download (downloadUrl , destination , userName ,
526530 password , null );
531+
532+ verifyArchive (destination );
527533 return ;
528534 } catch (DownloadException e ) {
529535 if (i == MAX_DOWNLOAD_ATTEMPS - 1 ) {
@@ -538,10 +544,85 @@ private void downloadFileIfMissing(URI downloadUrl, File destination,
538544 try {
539545 Thread .sleep (DOWNLOAD_ATTEMPT_DELAY * 1000 );
540546 } catch (InterruptedException e1 ) {
547+ Thread .currentThread ().interrupt ();
548+ }
549+ } catch (VerificationException ve ) {
550+ getLogger ().warn (
551+ "SHA256 verification of downloaded node archive failed." );
552+ if (i == MAX_DOWNLOAD_ATTEMPS - 1 ) {
553+ removeArchiveFile (destination );
554+ throw new DownloadException (
555+ "Failed to download node matching SHA256." );
541556 }
542557 }
558+ }
559+ } else {
560+ try {
561+ verifyArchive (destination );
562+ } catch (VerificationException de ) {
563+ removeArchiveFile (destination );
564+ downloadFileIfMissing (downloadUrl , destination , userName ,
565+ password );
566+ }
567+ }
568+ }
543569
570+ private void verifyArchive (File archive )
571+ throws DownloadException , VerificationException {
572+ try {
573+ URI shaSumsURL = nodeDownloadRoot
574+ .resolve (nodeVersion + "/" + SHA_SUMS_FILE );
575+ if ("file" .equalsIgnoreCase (shaSumsURL .getScheme ())) {
576+ // The file is local so it can't be expected to have a SHA file
577+ return ;
544578 }
579+
580+ File shaSums = new File (installDirectory , "node-" + SHA_SUMS_FILE );
581+
582+ getLogger ().debug ("Downloading {} to {}" , shaSumsURL , shaSums );
583+
584+ try {
585+ fileDownloader .download (shaSumsURL , shaSums , userName , password ,
586+ null );
587+ } catch (DownloadException e ) {
588+ if (Boolean .getBoolean (ACCEPT_MISSING_SHA )) {
589+ getLogger ().warn (
590+ "Could not verify SHA256 sum of downloaded node in {}. Accepting missing checksum verification as set in '{}' system property." ,
591+ archive , ACCEPT_MISSING_SHA );
592+ return ;
593+ } else {
594+ getLogger ().info (
595+ "Download of {} failed. If failure persists, use system property '{}' to skip verification or download node manually." ,
596+ SHA_SUMS_FILE , ACCEPT_MISSING_SHA );
597+ throw e ;
598+ }
599+ }
600+
601+ String archiveSHA256 = MessageDigestUtil
602+ .sha256Hex (Files .readAllBytes (archive .toPath ()));
603+
604+ List <String > sha256sums = Files .readAllLines (shaSums .toPath ());
605+ String archiveTargetSHA256 = sha256sums .stream ()
606+ .filter (sum -> sum
607+ .endsWith (archive .getName ()))
608+ .map (sum -> sum
609+ .substring (0 ,
610+ sum .length () - archive .getName ().length ())
611+ .trim ())
612+ .findFirst ().orElse ("-1" );
613+
614+ shaSums .delete ();
615+
616+ if (!archiveSHA256 .equals (archiveTargetSHA256 )) {
617+ getLogger ().error (
618+ "Expected SHA256 [{}] for downloaded node archive, got [{}]" ,
619+ archiveTargetSHA256 , archiveSHA256 );
620+ throw new VerificationException (
621+ "SHA256 sums did not match for downloaded node" );
622+ }
623+ } catch (IOException e ) {
624+ throw new VerificationException ("Failed to validate archive hash." ,
625+ e );
545626 }
546627 }
547628
0 commit comments