Skip to content

Commit 328eeaa

Browse files
committed
more tests, more bugs
1 parent cf0b274 commit 328eeaa

File tree

5 files changed

+662
-53
lines changed

5 files changed

+662
-53
lines changed

MODULES.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,15 @@ This function always unsandboxes the path.
354354
#### `currentDir`
355355

356356
``` purescript
357-
currentDir :: Path Rel Dir Sandboxed
357+
currentDir :: forall s. Path Rel Dir s
358358
```
359359

360360
The "current directory", which can be used to define relatively-located resources.
361361

362362
#### `rootDir`
363363

364364
``` purescript
365-
rootDir :: Path Abs Dir Sandboxed
365+
rootDir :: forall s. Path Abs Dir s
366366
```
367367

368368
The root directory, which can be used to define absolutely-located resources.

README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ A type-safe abstraction for platform-independent file system paths.
88
fullPath = rootDir </> dir "baz" </> file "foo.png"
99
```
1010

11+
See the [examples file](/examples/Examples.purs) for more.
12+
1113
# Getting Started
1214

1315
## Installation
@@ -41,11 +43,12 @@ Many paths come from user-input or configuration data. Pathy can parse such stri
4143
Building path liberals is easy. You will typically build path literals from the following components:
4244

4345
* `rootDir` &mdash; The root directory of an absolute path.
44-
* `currentDir` &mdash; The current directory (AKA the "working directory"), useful for describing relative paths.
46+
* `currentDir` &mdash; The current directory (AKA the "working directory"), useful for building relative paths.
4547
* `file` &mdash; A file (in the current directory).
4648
* `dir` &mdash; A directory (in the current directory).
47-
* `(</>)` &mdash; Combines two paths into one, if the composition makes sense!
49+
* `(</>)` &mdash; Adds a relative path to the end of a (relative or absolute) path.
4850
* `(<.>)` &mdash; Sets the extension of a file path.
51+
* `(<..>)` &mdash; Ascends one level in a directory, then descends into the specified relative path.
4952

5053
For example:
5154

@@ -85,9 +88,26 @@ Pathy also carries information on whether a path is a file or directory, and whe
8588

8689
`parentDir'`
8790

88-
`sandbox`
91+
### Sandboxing
92+
93+
Pathy makes it easy to create relative paths, even paths that ascend into parent directories of relative paths.
94+
95+
With this power comes danger: if you parse a user string, the user may be able to escape any arbitrary directory.
96+
97+
Pathy solves this security problem by *disallowing* conversion from a `Path` to a `String` until the `Path` has been *sandboxed*.
98+
99+
To sandbox a path, you just call `sandbox` and provide the sandbox directory, as well as the path to sandbox:
100+
101+
```purescript
102+
sandbox (rootDir </> dir "foo") (rootDir </> dir "foo" </> dir "bar")
103+
```
104+
105+
This returns a `Maybe`, which is either equal to `Nothing` if the tainted path escapes the sandbox, or `Just p`, where `p` is the tainted path, relative to the sandbox path.
106+
107+
After you have sandboxed a foreign path, you may call `printPath` on it. There's no need to remember this rule because it's enforced at compile-time by phantom types!
89108

109+
All the path literals you build by hand are automatically sandboxed, unless you call `parentDir'` on them.
90110

91111
# API Docs
92112

93-
[MODULES.md](MODULES.md)
113+
For complete documentation on all functions and types, see [MODULES.md](MODULES.md).

examples/src/Examples.purs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Examples where
22
import Debug.Trace(Trace(), trace)
33
import Control.Monad.Eff(Eff())
4+
import Data.Maybe(Maybe(..))
45
import Data.Maybe.Unsafe(fromJust)
56
import Data.Path.Pathy
67

@@ -57,3 +58,7 @@ module Examples where
5758
(fromJust $ sandbox (rootDir </> dir "foo") (rootDir </> dir "foo" </> dir "bar")) "./bar/"
5859

5960
test "depth - negative" (depth (parentDir' $ parentDir' $ parentDir' $ currentDir)) (-3)
61+
62+
test "parseRelDir - empty string" (parseRelDir "") (Just $ currentDir)
63+
64+
test "parseRelFile - image.png" (parseRelFile "image.png") (Just $ file "image.png")

0 commit comments

Comments
 (0)