Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith257 committed Sep 28, 2024
1 parent 88ffdd7 commit 850538e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
10 changes: 10 additions & 0 deletions zio-http/jvm/src/test/scala/zio/http/ConformanceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,16 @@ object ConformanceSpec extends ZIOSpecDefault {
.addHeader(Header.XFrameOptions.Deny)
.addHeader(Header.XFrameOptions.SameOrigin),
),
// need test fail assertion something like this
// request.headers.get(Header.IfModifiedSince.name) match {
// case Some(_) =>
// Response.status(Status.NotModified).addHeader(Header.ContentLength(14)).copy(body = Body.empty)
// case None =>
// Response
// .status(Status.Ok)
// .addHeader(Header.ContentLength(14))
// .copy(body = Body.fromString("<div>ABC</div>"))
// }
)
for {
response <- app.runZIO(Request.get("/test"))
Expand Down
13 changes: 13 additions & 0 deletions zio-http/shared/src/main/scala/zio/http/Header.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ object Header {
override def render(value: HeaderValue): String = value.value.toString
}

def validate(headers: Headers): ZIO[Any, Response, Unit] = {
val invalidHeaderChars = Set('\r', '\n', '\u0000')
val hasInvalidChar = headers.toList.exists { header =>
header.renderedValue.exists(invalidHeaderChars.contains)
}

if (hasInvalidChar) {
ZIO.fail(Response.status(Status.BadRequest))
} else {
ZIO.unit
}
}

private[http] override def headerNameAsCharSequence: CharSequence = customName
private[http] override def renderedValueAsCharSequence: CharSequence = value

Expand Down
48 changes: 24 additions & 24 deletions zio-http/shared/src/main/scala/zio/http/Routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
val tree = self.tree
Handler
.fromFunctionHandler[Request] { req =>
validateHeaders(req).flatMap { _ =>
validateHeaders(req) *> {
val chunk = tree.get(req.method, req.path)
val allowedMethods = tree.getAllMethods(req.path)

Expand Down Expand Up @@ -297,29 +297,29 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
_tree.asInstanceOf[Routes.Tree[Env]]
}

private def validateHeaders(req: Request): Handler[Any, Response, Request, Response] = {
val invalidHeaderChars = Set('\r', '\n', '\u0000')

ZIO.logInfo(s"Validating headers for request: ${req.headers}")

// Check if any header contains invalid characters
val hasInvalidChar = req.headers.toList.exists { header =>
// header.renderedValue.exists(invalidHeaderChars.contains)
val hasInvalid = header.renderedValue.exists(invalidHeaderChars.contains)
if (hasInvalid) {
ZIO.logInfo(s"Invalid header found: -> ${header.renderedValue}")
// Alternatively, you can escape the arrow like this:
// ZIO.logInfo(s"Invalid header found: -> ${header.renderedValue}")
}
hasInvalid
}

if (hasInvalidChar) {
Handler.badRequest
} else {
Handler.ok
}
}
// private def validateHeaders(req: Request): Handler[Any, Response, Request, Response] = {
// val invalidHeaderChars = Set('\r', '\n', '\u0000')

// ZIO.logInfo(s"Validating headers for request: ${req.headers}")

// // Check if any header contains invalid characters
// val hasInvalidChar = req.headers.toList.exists { header =>
// // header.renderedValue.exists(invalidHeaderChars.contains)
// val hasInvalid = header.renderedValue.exists(invalidHeaderChars.contains)
// if (hasInvalid) {
// ZIO.logInfo(s"Invalid header found: -> ${header.renderedValue}")
// // Alternatively, you can escape the arrow like this:
// // ZIO.logInfo(s"Invalid header found: -> ${header.renderedValue}")
// }
// hasInvalid
// }

// if (hasInvalidChar) {
// Handler.badRequest
// } else {
// Handler.ok
// }
// }

}

Expand Down

0 comments on commit 850538e

Please sign in to comment.