Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/neko/neko.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/spf13/cobra"
"io"
"iter"
"log"
"os"
"time"
)
Expand Down Expand Up @@ -98,6 +99,13 @@ func setNekoField(nekoEntry *internal.DbNeko, table, value string) {
}

func getAllMappings(table string) map[string]string {
switch table {
case internal.TableAnilist, internal.TableAnimePlanet, internal.TableBookWalker, internal.TableKitsu, internal.TableMyanimelist, internal.TableMangaupdates, internal.TableMangaupdatesNewId, internal.TableNovelUpdates:
// OK
default:
log.Fatalf("getAllMappings: invalid table name %s", table)
}
Comment on lines +102 to +107
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

The switch statement duplicates the list of tables from the mappingTables slice defined on line 23. This creates two sources of truth and can lead to maintainability issues. If a new table is added to mappingTables but forgotten here, the security check will be bypassed for that table, re-introducing the vulnerability.

To make this more robust and maintainable, you should validate the table parameter by checking for its presence in the mappingTables slice. This ensures there is a single source of truth for valid table names.

Suggested change
switch table {
case internal.TableAnilist, internal.TableAnimePlanet, internal.TableBookWalker, internal.TableKitsu, internal.TableMyanimelist, internal.TableMangaupdates, internal.TableMangaupdatesNewId, internal.TableNovelUpdates:
// OK
default:
log.Fatalf("getAllMappings: invalid table name %s", table)
}
isValid := false
for _, validTable := range mappingTables {
if table == validTable {
isValid = true
break
}
}
if !isValid {
log.Fatalf("getAllMappings: invalid table name %s", table)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

The switch statement duplicates the list of tables from the mappingTables slice defined on line 23. This creates two sources of truth and can lead to maintainability issues. If a new table is added to mappingTables but forgotten here, the security check will be bypassed for that table, re-introducing the vulnerability.

To make this more robust and maintainable, you should validate the table parameter by checking for its presence in the mappingTables slice. This ensures there is a single source of truth for valid table names.

@jules


rows, err := internal.DB.Query("SELECT UUID, ID FROM " + table)
internal.CheckErr(err)
defer rows.Close()
Expand Down
27 changes: 27 additions & 0 deletions cmd/neko/neko_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
package neko

import (
"bytes"
"database/sql"
"os"
"os/exec"
"slices"
"strings"
"testing"

_ "github.com/mattn/go-sqlite3"
"github.com/similar-manga/similar/internal"
)

func TestGetAllMappings(t *testing.T) {
t.Run("invalid table name", func(t *testing.T) {
if os.Getenv("BE_CRASHER") == "1" {
getAllMappings("INVALID_TABLE")
return
}
cmd := exec.Command(os.Args[0], "-test.run=^TestGetAllMappings/invalid_table_name$")
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()

if e, ok := err.(*exec.ExitError); ok && !e.Success() {
const expectedLog = "getAllMappings: invalid table name"
if !strings.Contains(stderr.String(), expectedLog) {
t.Errorf("expected stderr to contain %q, got %q", expectedLog, stderr.String())
}
return
}
t.Fatalf("process ran with err %v, want exit status 1. stderr: %s", err, stderr.String())
})
}

func TestProcessMangaList(t *testing.T) {
// Setup Output DB
outputDB, err := sql.Open("sqlite3", ":memory:")
Expand Down