|
1 | 1 | import os from "node:os";
|
2 |
| -import {stat, readFile, writeFile, mkdir} from "node:fs/promises"; |
| 2 | +import {stat, readFile, writeFile, mkdir, chmod, constants} from "node:fs/promises"; |
3 | 3 | import path from "node:path";
|
4 | 4 | import {getLogger} from "@ui5/logger";
|
5 | 5 |
|
@@ -27,18 +27,36 @@ export function getSslCertificate(
|
27 | 27 | ) {
|
28 | 28 | // checks the certificates if they are present
|
29 | 29 | return Promise.all([
|
30 |
| - fileExists(keyPath).then((bExists) => { |
31 |
| - if (!bExists) { |
| 30 | + fileExists(keyPath).then(async (statsOrFalse) => { |
| 31 | + if (!statsOrFalse) { |
32 | 32 | log.verbose(`No SSL private key found at ${keyPath}`);
|
33 | 33 | return false;
|
34 | 34 | }
|
| 35 | + if (statsOrFalse.mode & constants.S_IWUSR || statsOrFalse.mode & constants.S_IROTH) { |
| 36 | + // Note: According to the Node.js docs, "On Windows, only S_IRUSR and S_IWUSR are available" |
| 37 | + // Therefore we first check for "writable by owner" (S_IWUSR), even though we are more interested in |
| 38 | + // "readable by others", which we still check on platforms where it's supported |
| 39 | + log.verbose(`Detected outdated file permissions for private key file at ${keyPath}. ` + |
| 40 | + `Fixing permissions...`); |
| 41 | + await chmod(keyPath, 0o400).catch((err) => { |
| 42 | + log.error(`Failed to update permissions of private key file at ${keyPath}: ${err}`); |
| 43 | + }); |
| 44 | + } |
35 | 45 | return readFile(keyPath);
|
36 | 46 | }),
|
37 |
| - fileExists(certPath).then((bExists) => { |
38 |
| - if (!bExists) { |
| 47 | + fileExists(certPath).then(async (statsOrFalse) => { |
| 48 | + if (!statsOrFalse) { |
39 | 49 | log.verbose(`No SSL certificate found at ${certPath}`);
|
40 | 50 | return false;
|
41 | 51 | }
|
| 52 | + |
| 53 | + if (statsOrFalse.mode & constants.S_IWUSR || statsOrFalse.mode & constants.S_IROTH) { |
| 54 | + log.verbose(`Detected outdated file permissions for certificate file at ${keyPath}. ` + |
| 55 | + `Fixing permissions...`); |
| 56 | + await chmod(certPath, 0o400).catch((err) => { |
| 57 | + log.error(`Failed to update permissions of certificate file at ${certPath}: ${err}`); |
| 58 | + }); |
| 59 | + } |
42 | 60 | return readFile(certPath);
|
43 | 61 | })
|
44 | 62 | ]).then(function([key, cert]) {
|
@@ -84,14 +102,14 @@ async function createAndInstallCertificate(keyPath, certPath) {
|
84 | 102 | await Promise.all([
|
85 | 103 | // Write certificates to the ui5 certificate folder
|
86 | 104 | // such that they are used by default upon next startup
|
87 |
| - mkdir(path.dirname(keyPath), {recursive: true}).then(() => writeFile(keyPath, key)), |
88 |
| - mkdir(path.dirname(certPath), {recursive: true}).then(() => writeFile(certPath, cert)) |
| 105 | + mkdir(path.dirname(keyPath), {recursive: true}).then(() => writeFile(keyPath, key, {mode: 0o400})), |
| 106 | + mkdir(path.dirname(certPath), {recursive: true}).then(() => writeFile(certPath, cert, {mode: 0o400})) |
89 | 107 | ]);
|
90 | 108 | return {key, cert};
|
91 | 109 | }
|
92 | 110 |
|
93 | 111 | function fileExists(filePath) {
|
94 |
| - return stat(filePath).then(() => true, (err) => { |
| 112 | + return stat(filePath).then((s) => s, (err) => { |
95 | 113 | if (err.code === "ENOENT") { // "File or directory does not exist"
|
96 | 114 | return false;
|
97 | 115 | } else {
|
|
0 commit comments