<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "System",
  "identifier" : "/documentation/System/FilePath/lexicallyResolving(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "System"
    ],
    "preciseIdentifier" : "s:6System8FilePathV18lexicallyResolvingyACSgACnF"
  },
  "title" : "lexicallyResolving(_:)"
}
-->

# lexicallyResolving(_:)

Create a new `FilePath` by resolving `subpath` relative to `self`,
ensuring that the result is lexically contained within `self`.

```
func lexicallyResolving(_ subpath: FilePath) -> FilePath?
```

## Discussion

`subpath` will be lexically normalized (see `lexicallyNormalize`) as
part of resolution, meaning any contained `.` and `..` components will
be collapsed without resolving symlinks. Any root in `subpath` will be
ignored.

Returns `nil` if the result would “escape” from `self` through use of
the special directory component `..`.

This is useful for protecting against arbitrary path traversal from an
untrusted subpath: the result is guaranteed to be lexically contained
within `self`. Since this operation does not consult the file system to
resolve symlinks, any escaping symlinks nested inside of `self` can still
be targeted by the result.

Example:

```
let staticContent: FilePath = "/var/www/my-website/static"
let links: [FilePath] =
  ["index.html", "/assets/main.css", "../../../../etc/passwd"]
links.map { staticContent.lexicallyResolving($0) }
  // ["/var/www/my-website/static/index.html",
  //  "/var/www/my-website/static/assets/main.css",
  //  nil]
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)