-
Notifications
You must be signed in to change notification settings - Fork 0
fix: validate file extension in SaveAutoImage #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
| ) | ||
|
|
@@ -115,3 +116,54 @@ func TestSaveBatchImage_Validation(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| // SaveAutoImage Validation | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| func TestSaveAutoImage_Validation(t *testing.T) { | ||
| app := &App{ | ||
| handler: newTestHandler(), | ||
| } | ||
|
|
||
| exportDir := t.TempDir() | ||
|
|
||
| settingsMu.Lock() | ||
| oldSettings := currentSettings | ||
| currentSettings.ExportFolder = exportDir | ||
| settingsMu.Unlock() | ||
| defer func() { | ||
| settingsMu.Lock() | ||
| currentSettings = oldSettings | ||
| settingsMu.Unlock() | ||
| }() | ||
|
|
||
| type testCase struct { | ||
| name string | ||
| savePath string | ||
| isPng bool | ||
| wantError string | ||
| } | ||
|
Comment on lines
+140
to
+145
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
テスト用 handler に Also applies to: 160-165 🤖 Prompt for AI Agents |
||
|
|
||
| tests := []testCase{ | ||
| {"valid path", filepath.Join(exportDir, "image.jpg"), false, ""}, | ||
| {"valid path png", filepath.Join(exportDir, "photo.png"), true, ""}, | ||
| {"no extension jpeg", filepath.Join(exportDir, "image"), false, ""}, | ||
| {"no extension png", filepath.Join(exportDir, "photo"), true, ""}, | ||
| {"valid path but wrong ext png", filepath.Join(exportDir, "image.jpg"), true, "Invalid extension. Please save as .png"}, | ||
| {"valid path but wrong ext jpeg", filepath.Join(exportDir, "photo.png"), false, "Invalid extension. Please save as .jpg or .jpeg"}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| res := app.SaveAutoImage(tt.isPng, tt.savePath) | ||
|
|
||
| if res.Error != tt.wantError { | ||
| t.Errorf("expected error %q, got: %q", tt.wantError, res.Error) | ||
| } | ||
|
|
||
| if tt.wantError == "" && res.SaveToken == "" { | ||
| t.Errorf("expected valid SaveToken, got empty string") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: amemya/ExifFrame
Length of output: 154
🏁 Script executed:
Repository: amemya/ExifFrame
Length of output: 26768
🏁 Script executed:
Repository: amemya/ExifFrame
Length of output: 8297
最終保存パスを検証してから
prepareSaveに渡してください。ensureValidExtensionは実体パス検証の後でsavePathを変更します。savePathがExportFolder/link/で、linkが外部を指すシンボリックリンクの場合、検証対象はExportFolderです。その後、ExportFolder/link/.jpgがprepareSaveに渡されます。handleSaveは保存時にこのパスをos.Renameまたはos.Createに渡すため、許可フォルダ外へ保存できます。filepath.Clean(savePath)に対して拡張子を追加してください。拡張子追加後のパスを実体パス検証とprepareSaveの両方で使用してください。🤖 Prompt for AI Agents