Skip to content

Commit

Permalink
encoding improvement for my frontend.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvorhauer committed Feb 22, 2024
1 parent 729004a commit 5c17306
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 20 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ dependencies {

implementation("io.hypersistence:hypersistence-tsid:2.1.1")
implementation("io.netty:netty-all:4.1.106.Final")
implementation("org.owasp.encoder:encoder:1.2.3")
implementation("io.github.microutils:kotlin-logging-jvm:3.0.5")
implementation("ch.qos.logback:logback-classic:1.4.14")
implementation("io.sentry:sentry:7.2.0")
Expand Down
2 changes: 1 addition & 1 deletion deploy/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ spec:
spec:
containers:
- name: konomas
image: ghcr.io/jvorhauer/konomas:1.0.12
image: ghcr.io/jvorhauer/konomas:1.0.13
env:
- name: ASTRA_USERNAME
valueFrom:
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/blog/model/Note.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import akka.actor.typed.ActorRef
import akka.actor.typed.Scheduler
import akka.actor.typed.javadsl.AskPattern
import akka.pattern.StatusReply
import org.owasp.encoder.Encode
import io.hypersistence.tsid.TSID
import io.ktor.http.*
import io.ktor.server.application.*
Expand All @@ -19,11 +18,11 @@ import kotlinx.coroutines.future.await
import blog.read.Reader

data class CreateNoteRequest(val title: String, val body: String) {
fun toCommand(user: String, replyTo: ActorRef<StatusReply<NoteResponse>>) = CreateNote(user, Encode.forHtml(title), Encode.forHtml(body), replyTo)
fun toCommand(user: String, replyTo: ActorRef<StatusReply<NoteResponse>>) = CreateNote(user, title.encode(), body.encode(), replyTo)
}

data class UpdateNoteRequest(val id: String, val title: String?, val body: String?) {
fun toCommand(user: String, rt: ActorRef<StatusReply<NoteResponse>>) = UpdateNote(user, id, title.encode(), body.encode(), rt)
fun toCommand(user: String, rt: ActorRef<StatusReply<NoteResponse>>) = UpdateNote(user, id, title.mencode(), body.mencode(), rt)
}

data class CreateNote(
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/blog/model/Task.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import akka.actor.typed.ActorRef
import akka.actor.typed.Scheduler
import akka.actor.typed.javadsl.AskPattern.ask
import akka.pattern.StatusReply
import org.owasp.encoder.Encode
import io.hypersistence.tsid.TSID
import io.ktor.http.*
import io.ktor.server.application.*
Expand Down Expand Up @@ -49,11 +48,11 @@ data class Task(
}

data class CreateTaskRequest(val title: String, val body: String, val due: LocalDateTime): Request {
fun toCommand(user: String, replyTo: ActorRef<StatusReply<TaskResponse>>) = CreateTask(user, Encode.forHtml(title), Encode.forHtml(body), due, replyTo)
fun toCommand(user: String, replyTo: ActorRef<StatusReply<TaskResponse>>) = CreateTask(user, title.encode(), body.encode(), due, replyTo)
}

data class UpdateTaskRequest(val id: String, val title: String?, val body: String?, val due: LocalDateTime?, val status: TaskStatus?): Request {
fun toCommand(user: String, replyTo: ActorRef<StatusReply<TaskResponse>>) = UpdateTask(user, id, title.encode(), body.encode(), due, status, replyTo)
fun toCommand(user: String, replyTo: ActorRef<StatusReply<TaskResponse>>) = UpdateTask(user, id, title.mencode(), body.mencode(), due, status, replyTo)
}


Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/blog/model/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import akka.actor.typed.javadsl.AskPattern.ask
import akka.pattern.StatusReply
import com.auth0.jwt.JWT
import com.auth0.jwt.algorithms.Algorithm
import org.owasp.encoder.Encode
import io.hypersistence.tsid.TSID
import io.ktor.http.*
import io.ktor.http.HttpStatusCode.Companion.BadRequest
Expand Down Expand Up @@ -44,7 +43,7 @@ data class CreateUser(
) : Command {
constructor(rur: RegisterUserRequest, replyTo: ActorRef<StatusReply<User>>) : this(rur.email, rur.name, rur.password, replyTo)

fun toEvent() = UserCreated(id, Encode.forHtml(email), Encode.forHtml(name), password.hashed())
fun toEvent() = UserCreated(id, email, name.encode(), password.hashed())
}

// Events
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/blog/model/model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import java.time.Duration
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Locale
import org.owasp.encoder.Encode
import io.hypersistence.tsid.TSID
import io.ktor.server.application.*
import io.ktor.server.auth.*
Expand Down Expand Up @@ -44,7 +43,10 @@ fun nextId(): String = idFactory.generate().toString()
fun slugify(s: String): String = s.trim().replace(" ", " ").lowercase().replace("[^ a-z0-9]".toRegex(), "").replace(' ', '-')

fun String.hashed() = Hasher.hash(this)
fun String?.encode(): String? = if (this == null) null else Encode.forHtml(this)
fun doEncode(str: String): String = str.replace('<', '[').replace('>', ']')
fun doMEncode(str: String?): String? = if (str != null) doEncode(str) else null
fun String.encode(): String = doEncode(this)
fun String?.mencode(): String? = doMEncode(this)

data class Counts(
val users: Int,
Expand Down
1 change: 0 additions & 1 deletion src/test/kotlin/blog/ApiTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ class ApiTests {
assertThat(response.status).isEqualTo(HttpStatusCode.OK)
val ur = response.body<UserResponse>()
assertThat(ur.tasks).hasSize(1)
println("ur: $ur")

response = client.get("http://localhost:8181/info/counts")
println(response.body<Counts>())
Expand Down
13 changes: 13 additions & 0 deletions src/test/kotlin/blog/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import io.ktor.server.routing.*
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.util.UUID
import blog.model.encode

object MainTest {

Expand Down Expand Up @@ -100,4 +101,16 @@ class MainTests {
assertThat(kfg.server.host).isEqualTo("localhost")
println("kfg: $kfg")
}

@Test
fun testEncoding() {
var str = "Test with <h1>html tags</h1>"
assertThat(str.encode()).doesNotContain("<").doesNotContain(">")

str = "Test with \" and & and '"
assertThat(str.encode()).isEqualTo(str)

str = "and now with [h1]square brackets[/h1]"
assertThat(str.encode()).isEqualTo(str)
}
}
8 changes: 1 addition & 7 deletions src/test/kotlin/blog/model/NoteTests.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package blog.model

import java.util.UUID
import akka.Done
import akka.actor.testkit.typed.javadsl.TestKitJunitResource
import akka.pattern.StatusReply
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.util.UUID
import org.owasp.encoder.Encode

class NoteTests {

Expand Down Expand Up @@ -66,11 +65,6 @@ class NoteTests {
assertThat(updated.user).isEqualTo(userId)
assertThat(updated.title).isEqualTo(run.title)
assertThat(updated.body).isEqualTo(run.body)

var str = Encode.forHtml("")
assertThat(str).isEmpty()
str = Encode.forHtml(null)
assertThat(str).isNotNull();
}

@Test
Expand Down

0 comments on commit 5c17306

Please sign in to comment.