Skip to content

Commit

Permalink
Make every auth token store to a specific folder, return where the fi…
Browse files Browse the repository at this point in the history
…le was saved, filter path traversal (even tho we are the only one using it...) and allow checking if a file already exists
  • Loading branch information
MrPowerGamerBR committed Oct 15, 2024
1 parent bfbfc85 commit fddff49
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,22 @@ class PostUploadFileRoute(val m: EtherealGambi) : BaseRoute("/api/v1/upload") {

val fileData = Base64.getDecoder().decode(request.dataBase64)

val file = File(m.files, request.path)
if (request.path.contains("..")) {
call.respondText(Json.encodeToString(UploadFileResponse.PathTraversalDisallowed))
return
}

val writeToPath = "/${authorizationToken.folder}/${request.path}"
val file = File(m.files, writeToPath)
if (request.failIfFileAlreadyExists && file.exists()) {
call.respondText(Json.encodeToString(UploadFileResponse.FileAlreadyExists))
return
}

val folder = file.parentFile
folder.mkdirs()
file.writeBytes(fileData)

call.respondText(Json.encodeToString(UploadFileResponse.Success))
call.respondText(Json.encodeToString(UploadFileResponse.Success(writeToPath)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ data class EtherealGambiConfig(
@Serializable
data class AuthorizationToken(
val name: String,
val token: String
)

@Serializable
data class OptimizationSettings(
val path: String,
val useOptiPNG: Boolean
val folder: String,
val token: String,
)
}
5 changes: 1 addition & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ plugins {
kotlin("plugin.serialization") version libs.versions.kotlin apply false
}

group = "net.perfectdreams.etherealgambi"
version = "1.0.1-SNAPSHOT"

allprojects {
group = "net.perfectdreams.etherealgambi"
version = "1.0.1"
version = "1.0.2"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import net.perfectdreams.etherealgambi.data.api.UploadFileRequest
import net.perfectdreams.etherealgambi.data.api.UploadFileResponse
import net.perfectdreams.etherealgambi.data.api.requests.ImageVariantsRequest
import net.perfectdreams.etherealgambi.data.api.responses.ImageVariantsResponse
import java.io.Closeable
Expand All @@ -32,14 +33,14 @@ class EtherealGambiClient(baseUrl: String) : Closeable {
)
}

suspend fun uploadFile(token: String, path: String, data: ByteArray) {
return Json.decodeFromString(
suspend fun uploadFile(token: String, path: String, failIfFileAlreadyExists: Boolean, data: ByteArray): UploadFileResponse {
return Json.decodeFromString<UploadFileResponse>(
http.post("$baseUrl/api/v1/upload") {
header(HttpHeaders.Authorization, token)

setBody(
TextContent(
Json.encodeToString(UploadFileRequest(path, Base64.getEncoder().encodeToString(data))),
Json.encodeToString(UploadFileRequest(path, failIfFileAlreadyExists, Base64.getEncoder().encodeToString(data))),
ContentType.Application.Json
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import kotlinx.serialization.Serializable
@Serializable
data class UploadFileRequest(
val path: String,
val dataBase64: String
val failIfFileAlreadyExists: Boolean,
val dataBase64: String,
)

@Serializable
sealed class UploadFileResponse {
@Serializable
data object Success : UploadFileResponse()
data class Success(val path: String) : UploadFileResponse()

@Serializable
data object FileAlreadyExists : UploadFileResponse()

@Serializable
data object PathTraversalDisallowed : UploadFileResponse()

@Serializable
data object Unauthorized : UploadFileResponse()
Expand Down

0 comments on commit fddff49

Please sign in to comment.