Skip to content

[Code scan] Handle missing or invalid custom template files #564

Description

@njzjz

This issue was found during a Codex global code scan of the repository.

Baseline commit: e3c5b38

Problem

The custom template import path assumes a file is selected and that its contents are valid JSON. Both reader.readAsText(this.file[0]) and JSON.parse(e.target.result) are unguarded.

Code references:

add: function () {
const name = this.name;
const that = this;
const reader = new FileReader();
reader.onload = function (e) {
const hash = require("crypto")
.createHash("sha256")
.update(e.target.result)
.digest("hex");
const obj = JSON.parse(e.target.result);
const curr = that.$storage.getStorageSync("CustomTemplate") || {};
curr[hash] = {
name: name,
obj: obj,
};
that.$storage.setStorageSync("CustomTemplate", curr);
that.$router.push("/input");
that.$root.$app.update_navi();
};
reader.readAsText(this.file[0]);

Relevant snippet:

const obj = JSON.parse(e.target.result);
reader.readAsText(this.file[0]);

Impact

Clicking Add without selecting a file, or selecting malformed JSON, throws an uncaught browser error instead of giving a recoverable UI error.

Suggested fix

Disable Add until a file is selected, wrap JSON parsing in try/catch, handle FileReader.onerror, and show a validation message instead of navigating away.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions