detached 62cb26b

fix(review): harden CEF init trace log setup

commit
62cb26b869a6eb1b6e75b3b67e100e72d3b1fa9c
author
brice · · 2026-04-19 10:07 UTC
parent
2b89483
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
2 files · +41 -1
M internal/infrastructure/cef/engine_init.go +6 -1
@@ -486,7 +486,12 @@ func prepareCEFInitTraceFile(defaultLogFile, logFile string) (string, error) {
486486 if err := os.MkdirAll(filepath.Dir(bootstrapLogFile), dirPerm); err != nil {
487487 return "", fmt.Errorf("mkdir %s: %w", filepath.Dir(bootstrapLogFile), err)
488488 }
489- if err := os.WriteFile(bootstrapLogFile, nil, filePerm); err != nil {
489+ file, err := openRegularLogFile(bootstrapLogFile)
490+ if err != nil {
491+ return "", fmt.Errorf("reset %s: %w", bootstrapLogFile, err)
492+ }
493+ defer func() { _ = file.Close() }()
494+ if err := file.Truncate(0); err != nil {
490495 return "", fmt.Errorf("reset %s: %w", bootstrapLogFile, err)
491496 }
492497 return bootstrapLogFile, nil
@@ -486,7 +486,12 @@ func prepareCEFInitTraceFile(defaultLogFile, logFile string) (string, error) {
486 if err := os.MkdirAll(filepath.Dir(bootstrapLogFile), dirPerm); err != nil {486 if err := os.MkdirAll(filepath.Dir(bootstrapLogFile), dirPerm); err != nil {
487 return "", fmt.Errorf("mkdir %s: %w", filepath.Dir(bootstrapLogFile), err)487 return "", fmt.Errorf("mkdir %s: %w", filepath.Dir(bootstrapLogFile), err)
488 }488 }
489- if err := os.WriteFile(bootstrapLogFile, nil, filePerm); err != nil {489+ file, err := openRegularLogFile(bootstrapLogFile)
490+ if err != nil {
491+ return "", fmt.Errorf("reset %s: %w", bootstrapLogFile, err)
492+ }
493+ defer func() { _ = file.Close() }()
494+ if err := file.Truncate(0); err != nil {
490 return "", fmt.Errorf("reset %s: %w", bootstrapLogFile, err)495 return "", fmt.Errorf("reset %s: %w", bootstrapLogFile, err)
491 }496 }
492 return bootstrapLogFile, nil497 return bootstrapLogFile, nil
M internal/infrastructure/cef/engine_init_test.go +35 -0
@@ -142,6 +142,41 @@ func TestPrepareCEFInitTraceFile_EnabledViaExplicitLogFile(t *testing.T) {
142142 require.Equal(t, os.FileMode(dirPerm), dirInfo.Mode().Perm())
143143 }
144144
145+func TestPrepareCEFInitTraceFile_TruncatesExistingFile(t *testing.T) {
146+ t.Setenv(puregoCEFInitTraceEnvVar, "1")
147+ logFile := filepath.Join(t.TempDir(), "custom", "cef_runtime.log")
148+ bootstrapLogFile := filepath.Join(filepath.Dir(logFile), "cef_runtime.bootstrap.log")
149+ require.NoError(t, os.MkdirAll(filepath.Dir(bootstrapLogFile), dirPerm))
150+ require.NoError(t, os.WriteFile(bootstrapLogFile, []byte("existing"), filePerm))
151+
152+ path, err := prepareCEFInitTraceFile("", logFile)
153+ require.NoError(t, err)
154+ require.Equal(t, bootstrapLogFile, path)
155+
156+ content, readErr := os.ReadFile(path)
157+ require.NoError(t, readErr)
158+ require.Empty(t, content)
159+}
160+
161+func TestPrepareCEFInitTraceFile_RejectsSymlink(t *testing.T) {
162+ t.Setenv(puregoCEFInitTraceEnvVar, "1")
163+ target := filepath.Join(t.TempDir(), "target.bootstrap.log")
164+ require.NoError(t, os.WriteFile(target, []byte("existing"), 0o644))
165+
166+ logFile := filepath.Join(t.TempDir(), "custom", "cef_runtime.log")
167+ bootstrapLogFile := filepath.Join(filepath.Dir(logFile), "cef_runtime.bootstrap.log")
168+ require.NoError(t, os.MkdirAll(filepath.Dir(bootstrapLogFile), dirPerm))
169+ require.NoError(t, os.Symlink(target, bootstrapLogFile))
170+
171+ path, err := prepareCEFInitTraceFile("", logFile)
172+ require.ErrorContains(t, err, "must not be a symlink")
173+ require.Empty(t, path)
174+
175+ targetInfo, statErr := os.Stat(target)
176+ require.NoError(t, statErr)
177+ require.Equal(t, os.FileMode(0o644), targetInfo.Mode().Perm())
178+}
179+
145180 func TestPrepareCEFInitTraceFile_UsesResolvedProfileLogDirByDefault(t *testing.T) {
146181 t.Setenv(puregoCEFInitTraceEnvVar, "1")
147182 profile := testCEFDevProfile(t)
@@ -142,6 +142,41 @@ func TestPrepareCEFInitTraceFile_EnabledViaExplicitLogFile(t *testing.T) {
142 require.Equal(t, os.FileMode(dirPerm), dirInfo.Mode().Perm())142 require.Equal(t, os.FileMode(dirPerm), dirInfo.Mode().Perm())
143 }143 }
144 144
145+func TestPrepareCEFInitTraceFile_TruncatesExistingFile(t *testing.T) {
146+ t.Setenv(puregoCEFInitTraceEnvVar, "1")
147+ logFile := filepath.Join(t.TempDir(), "custom", "cef_runtime.log")
148+ bootstrapLogFile := filepath.Join(filepath.Dir(logFile), "cef_runtime.bootstrap.log")
149+ require.NoError(t, os.MkdirAll(filepath.Dir(bootstrapLogFile), dirPerm))
150+ require.NoError(t, os.WriteFile(bootstrapLogFile, []byte("existing"), filePerm))
151+
152+ path, err := prepareCEFInitTraceFile("", logFile)
153+ require.NoError(t, err)
154+ require.Equal(t, bootstrapLogFile, path)
155+
156+ content, readErr := os.ReadFile(path)
157+ require.NoError(t, readErr)
158+ require.Empty(t, content)
159+}
160+
161+func TestPrepareCEFInitTraceFile_RejectsSymlink(t *testing.T) {
162+ t.Setenv(puregoCEFInitTraceEnvVar, "1")
163+ target := filepath.Join(t.TempDir(), "target.bootstrap.log")
164+ require.NoError(t, os.WriteFile(target, []byte("existing"), 0o644))
165+
166+ logFile := filepath.Join(t.TempDir(), "custom", "cef_runtime.log")
167+ bootstrapLogFile := filepath.Join(filepath.Dir(logFile), "cef_runtime.bootstrap.log")
168+ require.NoError(t, os.MkdirAll(filepath.Dir(bootstrapLogFile), dirPerm))
169+ require.NoError(t, os.Symlink(target, bootstrapLogFile))
170+
171+ path, err := prepareCEFInitTraceFile("", logFile)
172+ require.ErrorContains(t, err, "must not be a symlink")
173+ require.Empty(t, path)
174+
175+ targetInfo, statErr := os.Stat(target)
176+ require.NoError(t, statErr)
177+ require.Equal(t, os.FileMode(0o644), targetInfo.Mode().Perm())
178+}
179+
145 func TestPrepareCEFInitTraceFile_UsesResolvedProfileLogDirByDefault(t *testing.T) {180 func TestPrepareCEFInitTraceFile_UsesResolvedProfileLogDirByDefault(t *testing.T) {
146 t.Setenv(puregoCEFInitTraceEnvVar, "1")181 t.Setenv(puregoCEFInitTraceEnvVar, "1")
147 profile := testCEFDevProfile(t)182 profile := testCEFDevProfile(t)

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close