|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * license agreements; and to You under the Apache License, version 2.0: |
| 4 | + * |
| 5 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * This file is part of the Apache Pekko project, which was derived from Akka. |
| 8 | + */ |
| 9 | + |
| 10 | +/* |
| 11 | + * Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com> |
| 12 | + */ |
| 13 | + |
| 14 | +package org.apache.pekko.http.scaladsl.server |
| 15 | +package directives |
| 16 | + |
| 17 | +import java.io.File |
| 18 | +import java.nio.file.{ Files, Paths } |
| 19 | + |
| 20 | +import scala.concurrent.duration._ |
| 21 | + |
| 22 | +import org.apache.pekko |
| 23 | +import pekko.http.scaladsl.testkit.RouteTestTimeout |
| 24 | +import pekko.testkit._ |
| 25 | + |
| 26 | +import org.scalatest.{ BeforeAndAfterAll, Inside, Inspectors } |
| 27 | + |
| 28 | +class FileAndResourceDirectivesSymlinkSpec extends RoutingSpec |
| 29 | + with Inspectors with Inside with BeforeAndAfterAll { |
| 30 | + |
| 31 | + // need to serve from the src directory, when sbt copies the resource directory over to the |
| 32 | + // target directory it will resolve symlinks in the process |
| 33 | + val testRoot = new File("http-tests/src/test/resources") |
| 34 | + require(testRoot.exists(), s"testRoot was not found at ${testRoot.getAbsolutePath}") |
| 35 | + |
| 36 | + val tempDir = Files.createTempDirectory("pekko-http-symlink-test") |
| 37 | + tempDir.toFile.deleteOnExit() |
| 38 | + val dirWithLink = new File(tempDir.toFile, "dirWithLink") |
| 39 | + dirWithLink.mkdir() |
| 40 | + val symlink = Files.createSymbolicLink( |
| 41 | + Paths.get(dirWithLink.getAbsolutePath, "linked-dir"), |
| 42 | + new File(testRoot, "subDirectory").toPath.toAbsolutePath) |
| 43 | + |
| 44 | + override def afterAll(): Unit = { |
| 45 | + super.afterAll() |
| 46 | + Files.deleteIfExists(symlink) |
| 47 | + Files.deleteIfExists(dirWithLink.toPath) |
| 48 | + Files.deleteIfExists(tempDir) |
| 49 | + } |
| 50 | + |
| 51 | + // operations touch files, can be randomly hit by slowness |
| 52 | + implicit val routeTestTimeout: RouteTestTimeout = RouteTestTimeout(3.seconds.dilated) |
| 53 | + |
| 54 | + override def testConfigSource = super.testConfigSource ++ """ |
| 55 | + pekko.http.routing.range-coalescing-threshold = 1 |
| 56 | + """ |
| 57 | + |
| 58 | + "getFromDirectory" should { |
| 59 | + def _getFromDirectory() = getFromDirectory(dirWithLink.getCanonicalPath) |
| 60 | + |
| 61 | + "not follow symbolic links to find a file" in { |
| 62 | + Files.isSymbolicLink(symlink) shouldBe true |
| 63 | + EventFilter.warning(pattern = ".* points to a location that is not part of .*", occurrences = 1).intercept { |
| 64 | + Get("linked-dir/empty.pdf") ~> _getFromDirectory() ~> check { |
| 65 | + handled shouldBe false |
| 66 | + /* TODO: resurrect following links under an option |
| 67 | + responseAs[String] shouldEqual "123" |
| 68 | + mediaType shouldEqual `application/pdf`*/ |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments