@nx-ddd/logging 20.9.2 → 20.20.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,9 +1,24 @@
|
|
|
1
1
|
import * as fs from 'fs';
|
|
2
2
|
import * as os from 'os';
|
|
3
|
-
import { join } from 'path';
|
|
3
|
+
import { relative, isAbsolute, join } from 'path';
|
|
4
4
|
import { BaseLogger, LogLevel, LOGGER } from '@nx-ddd/logging/base';
|
|
5
5
|
import { formatJson, buildLogRecord } from '@nx-ddd/logging/format';
|
|
6
6
|
|
|
7
|
+
function isRootWritingToForeignHome(path) {
|
|
8
|
+
if (typeof process.getuid !== 'function' || process.getuid() !== 0)
|
|
9
|
+
return false;
|
|
10
|
+
const home = os.homedir();
|
|
11
|
+
const rel = relative(home, path);
|
|
12
|
+
if (rel.startsWith('..') || isAbsolute(rel))
|
|
13
|
+
return false;
|
|
14
|
+
try {
|
|
15
|
+
return fs.statSync(home).uid !== 0;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
7
22
|
class FileLogger extends BaseLogger {
|
|
8
23
|
writeStream = null;
|
|
9
24
|
minLevel;
|
|
@@ -22,6 +37,10 @@ class FileLogger extends BaseLogger {
|
|
|
22
37
|
const date = new Date().toISOString().split('T')[0];
|
|
23
38
|
const component = options?.component || 'app';
|
|
24
39
|
const logFile = options?.filePath || join(logDir, `${component}-${date}.log`);
|
|
40
|
+
if (isRootWritingToForeignHome(logFile)) {
|
|
41
|
+
console.error(`[FileLogger] file output disabled: running as root with non-root-owned home (${logFile})`);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
25
44
|
try {
|
|
26
45
|
const dir = options?.filePath ? join(logFile, '..') : logDir;
|
|
27
46
|
fs.mkdirSync(dir, { recursive: true });
|
|
@@ -30,7 +49,14 @@ class FileLogger extends BaseLogger {
|
|
|
30
49
|
console.error(`[FileLogger] Failed to create log directory: ${error}`);
|
|
31
50
|
}
|
|
32
51
|
try {
|
|
33
|
-
|
|
52
|
+
const stream = fs.createWriteStream(logFile, { flags: 'a' });
|
|
53
|
+
stream.on('error', (error) => {
|
|
54
|
+
console.error(`[FileLogger] file output disabled: ${error.code ?? 'ERR'} ${logFile}: ${error.message}`);
|
|
55
|
+
stream.destroy();
|
|
56
|
+
if (this.writeStream === stream)
|
|
57
|
+
this.writeStream = null;
|
|
58
|
+
});
|
|
59
|
+
this.writeStream = stream;
|
|
34
60
|
}
|
|
35
61
|
catch (error) {
|
|
36
62
|
console.error(`[FileLogger] Failed to open log file: ${error}`);
|