Skip to content

Commit 529818f

Browse files
committed
feat: add check for origin invalid URI
Signed-off-by: Guillaume Hivert <hivert.is.coming@gmail.com>
1 parent b7e94b2 commit 529818f

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
*.ez
33
/build
44
erl_crash.dump
5+
.DS_Store
6+
Thumbs.db

src/cors_builder.gleam

+11
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import gleam/pair
6262
import gleam/result
6363
import gleam/set.{type Set}
6464
import gleam/string
65+
import gleam/uri
6566
import mist
6667
import wisp
6768

@@ -110,6 +111,15 @@ pub fn allow_all_origins(cors: Cors) {
110111
Cors(..cors, allow_origin: allow_origin)
111112
}
112113

114+
fn invalid_uri(origin: String) {
115+
uri.parse(origin)
116+
|> result.is_ok()
117+
|> function.tap(fn(value) {
118+
use <- bool.guard(when: value, return: Nil)
119+
io.println("Your provided origin: \"" <> origin <> "\" is not a valid URI.")
120+
})
121+
}
122+
113123
/// Allow a specific domain to access your server.
114124
/// You can specify multiple domains to access your server. In this case, call
115125
/// the function multiple times on `Cors` data.
@@ -120,6 +130,7 @@ pub fn allow_all_origins(cors: Cors) {
120130
/// |> cors.allow_origin("domain2")
121131
/// }
122132
pub fn allow_origin(cors: Cors, origin: String) {
133+
use <- bool.guard(when: invalid_uri(origin), return: cors)
123134
let allow_origin = case cors.allow_origin {
124135
Some(Wildcard) -> Some(Wildcard)
125136
Some(Origin(content)) -> Some(Origin(set.insert(content, origin)))

0 commit comments

Comments
 (0)