Building42 / Telegraph

Secure Web Server for iOS, tvOS and macOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serving symbolic link of folder

Joebayld opened this issue · comments

Hi,

I've noticed some strange behavior when sharing a symbolic link (alias) of a website. It's a single page application and I want the root route to pass the index.html file. If I serve the directory of a folder, the index.html is properly read and returned. However, if I serve a directory that is an alias of a folder that contains index.html, an error is thrown that I don't have permission to access the file.

I've ruled it down to this set of code.

    if resourceType == .typeDirectory {
      guard let index = index else { return HTTPResponse(.forbidden) }

      // Create a response based on the index file in the directory
      let indexURL = url.appendingPathComponent(index, isDirectory: false)
      return try responseForURL(indexURL, byteRange: byteRange)
    }

It will only serve the index file if the resource is a folder, but in my case, it's a symbolic link pointing to a folder. Any ideas on how to tackle this situation?

I'm wondering if we could put a check on what is passed to the initial `serveDirectory' to see if a symbolic link was passed initially.

I'm happy to make a PR but not sure if it will be a breaking change..

Hi,

Yes I think the code should be a little bit smarter in the way that it handles symbolic links.

Perhaps the first step in the responseForURL(_ url: URL, byteRange: ByteRange?) function should be to check if the resourceType is a symbolic link. If it is, we could use FileManager's destinationOfSymbolicLink(atPath:) function to follow that symbolic link. We could probably use recursion to then feed the url of destinationOfSymbolicLink to the responseForURL function again to process the actual file / directory url.

See https://developer.apple.com/documentation/foundation/filemanager/1415161-destinationofsymboliclink

Thank you for the PR!