Skip to content

Commit db45e7a

Browse files
authored
Merge pull request #11 from massix/feat/validate-alias-name
Validate aliases names
2 parents 609243d + a801e10 commit db45e7a

File tree

6 files changed

+79
-10
lines changed

6 files changed

+79
-10
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ The configuration file is loaded when Gleeter starts. Gleeter looks for the conf
9999
* `max_cols`: Specifies the maximum number of columns to use for displaying the comic.
100100
* `max_lines`: Specifies the maximum number of lines to use for displaying the comic.
101101

102-
* **`[[alias]]`**: This section defines aliases for accessing comics. You can define multiple aliases.
103-
* `name`: The name of the alias.
102+
* `[[alias]]`**: This section defines aliases for accessing comics. You can define multiple aliases.
103+
* `name`: The name of the alias. Alias names **must not** be empty and **must not** contain any of the following characters: `!`, `;`, `$`, `:`, `\`, `"`, `'`, `(`, `)`, space, tab, newline, or carriage return. Leading and trailing whitespaces will be automatically removed.
104104
* `type`: The type of alias. Valid values are `"id"`, `"random"`, and `"latest"`.
105105
* `id`: (Only required for `"id"` aliases) The comic ID to associate with the alias.
106106

107-
The `src/gleeter/config.gleam` file defines the structure of the configuration data and how it is parsed from the TOML file.
107+
The [config.gleam](src/gleeter/config.gleam) file defines the structure of the configuration data and how it is parsed from the TOML file.
108108

109109
To modify Gleeter's behavior, simply edit the configuration file and restart the application.
110110

flake.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pkgs = import nixpkgs { inherit system; };
1111
inherit (pkgs) mkShell;
1212
gleamPackagesHash = "sha256-DIY9OA3ZigaVC2gxvwgCrF8rjNIraSy7mVqudp62x4M=";
13-
version = "1.3.2";
13+
version = "1.3.3";
1414
pname = "gleeter";
1515
src = ./.;
1616
gleam-helper = pkgs.callPackage ./nix/gleam-helper.nix { };
@@ -24,7 +24,7 @@
2424
sqlite
2525
];
2626
};
27-
overlays = _: _: { gleeter = self.packages.${system}.gleeter; };
27+
overlays = _: _: { inherit (self.packages.${system}) gleeter; };
2828
packages = {
2929
gleeter = gleam-helper.buildGleamPackage {
3030
inherit pname version src gleamPackagesHash;

src/gleeter/config.gleam

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import gleam/dict
33
import gleam/list
44
import gleam/option
55
import gleam/result
6+
import gleam/string
67
import simplifile
78
import tom
89

@@ -63,19 +64,60 @@ fn or_empty(
6364
}
6465
}
6566

67+
type NameString {
68+
Invalid
69+
Valid(String)
70+
}
71+
72+
const forbidden_chars: List(String) = [
73+
"!", ";", "$", ":", "\\", "\"", "'", "(", ")", " ", "\t", "\n", "\r",
74+
]
75+
76+
fn validate_name(in: Result(String, a)) -> NameString {
77+
let with_valid = fn(in: NameString, next: fn(String) -> NameString) -> NameString {
78+
case in {
79+
Invalid -> Invalid
80+
Valid(s) -> next(s)
81+
}
82+
}
83+
84+
let check_trim = fn(in: NameString) -> NameString {
85+
use s <- with_valid(in)
86+
case string.trim(s) {
87+
"" -> Invalid
88+
s -> Valid(s)
89+
}
90+
}
91+
92+
let check_special = fn(in: NameString) -> NameString {
93+
use s <- with_valid(in)
94+
case string.to_graphemes(s) |> list.any(list.contains(forbidden_chars, _)) {
95+
True -> Invalid
96+
False -> Valid(s)
97+
}
98+
}
99+
100+
case in {
101+
Ok(s) -> {
102+
Valid(s) |> check_trim |> check_special
103+
}
104+
Error(_) -> Invalid
105+
}
106+
}
107+
66108
fn parse_alias(in: dict.Dict(String, tom.Toml)) -> option.Option(Alias) {
67-
let name = tom.get_string(in, ["name"]) |> option.from_result
109+
let name = tom.get_string(in, ["name"]) |> validate_name
68110
let alias_type = tom.get_string(in, ["type"]) |> option.from_result
69111
let comic_id = tom.get_int(in, ["id"]) |> option.from_result
70112

71113
case name, alias_type, comic_id {
72-
option.Some(name), option.Some("id"), option.Some(comic_id) -> {
114+
Valid(name), option.Some("id"), option.Some(comic_id) -> {
73115
IdAlias(name, comic_id) |> option.Some
74116
}
75-
option.Some(name), option.Some("latest"), _ -> {
117+
Valid(name), option.Some("latest"), _ -> {
76118
LatestAlias(name) |> option.Some
77119
}
78-
option.Some(name), option.Some("random"), _ -> {
120+
Valid(name), option.Some("random"), _ -> {
79121
RandomAlias(name) |> option.Some
80122
}
81123
_, _, _ -> option.None

src/gleeter/version.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub const gleeter_version: String = "1.3.2"
1+
pub const gleeter_version: String = "1.3.3"
22

33
pub const github_url: String = "https://github.com/massix/gleeter"
44

test/gleeter/config_test.gleam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn config_tests() -> test_tree.TestTree {
2929
|> expect.to_equal(
3030
Configuration(Some(38), Some(80), Some(23), [
3131
IdAlias("bobbytables", 987),
32+
IdAlias("trimmed_alias", 987),
3233
RandomAlias("rnd"),
3334
LatestAlias("l"),
3435
]),

test_data/config.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,32 @@ name = "bobbytables"
99
type = "id"
1010
id = 987
1111

12+
[[alias]]
13+
name = "" # Empty name
14+
type = "id"
15+
id = 123
16+
17+
[[alias]]
18+
name = " trimmed_alias "
19+
type = "id"
20+
id = 987
21+
22+
[[alias]]
23+
name = " with spaces "
24+
type = "random"
25+
26+
[[alias]]
27+
name = "with!special"
28+
type = "latest"
29+
30+
[[alias]]
31+
name = "with;special"
32+
type = "latest"
33+
34+
[[alias]]
35+
name = "with(special"
36+
type = "random"
37+
1238
[[alias]]
1339
name = "rnd"
1440
type = "random"

0 commit comments

Comments
 (0)