From 1d5d68afd17061f35cc3b913a4dc3901ee87d854 Mon Sep 17 00:00:00 2001 From: Anton Sorokin Date: Mon, 3 Oct 2022 16:30:06 +0300 Subject: [PATCH] fixup! fixup! fixup! [#138] Report links that escape repo directory --- src/Xrefcheck/Verify.hs | 2 +- .../bad-absolute-filepath.md | 8 -- .../check-local-references-outside-repo.bats | 90 ------------------ .../check-local-references-outside-repo.md | 8 -- .../check-local-refs/check-local-refs.bats | 6 ++ .../config-with-virtual-files.yaml} | 2 +- .../d0f1.md} | 3 +- .../golden/check-local-refs/dir1/dir2/d2f1.md | 12 +++ tests/golden/check-local-refs/expected1.gold | 22 ++++- tests/golden/check-local-refs/expected2.gold | 52 ++++++++++- tests/golden/check-local-refs/expected3.gold | 91 +++++++++++++++++++ 11 files changed, 185 insertions(+), 111 deletions(-) delete mode 100644 tests/golden/check-local-references-outside-repo/bad-absolute-filepath.md delete mode 100644 tests/golden/check-local-references-outside-repo/check-local-references-outside-repo.bats delete mode 100644 tests/golden/check-local-references-outside-repo/inner-directory/check-local-references-outside-repo.md rename tests/golden/{check-local-references-outside-repo/config-virtual-file.yaml => check-local-refs/config-with-virtual-files.yaml} (87%) rename tests/golden/{check-local-references-outside-repo/link-target.md => check-local-refs/d0f1.md} (66%) create mode 100644 tests/golden/check-local-refs/expected3.gold diff --git a/src/Xrefcheck/Verify.hs b/src/Xrefcheck/Verify.hs index bbc33dc4..ebe17a0a 100644 --- a/src/Xrefcheck/Verify.hs +++ b/src/Xrefcheck/Verify.hs @@ -58,7 +58,7 @@ import Network.HTTP.Types.Header (hRetryAfter) import Network.HTTP.Types.Status (Status, statusCode, statusMessage) import System.Console.Pretty (Style (..), style) import System.Directory (doesDirectoryExist, doesFileExist) -import System.FilePath (takeDirectory, (), normalise, splitDirectories, makeRelative) +import System.FilePath (makeRelative, normalise, splitDirectories, takeDirectory, ()) import Text.ParserCombinators.ReadPrec qualified as ReadPrec (lift) import Text.Regex.TDFA.Text (Regex, regexec) import Text.URI (Authority (..), ParseExceptionBs, URI (..), mkURIBs) diff --git a/tests/golden/check-local-references-outside-repo/bad-absolute-filepath.md b/tests/golden/check-local-references-outside-repo/bad-absolute-filepath.md deleted file mode 100644 index 33de9124..00000000 --- a/tests/golden/check-local-references-outside-repo/bad-absolute-filepath.md +++ /dev/null @@ -1,8 +0,0 @@ - - -[A](/../../a.md) -[B](/b/../../b.md) diff --git a/tests/golden/check-local-references-outside-repo/check-local-references-outside-repo.bats b/tests/golden/check-local-references-outside-repo/check-local-references-outside-repo.bats deleted file mode 100644 index e8d9dbd4..00000000 --- a/tests/golden/check-local-references-outside-repo/check-local-references-outside-repo.bats +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/env bats - -# SPDX-FileCopyrightText: 2021 Serokell -# -# SPDX-License-Identifier: MPL-2.0 - - -load '../helpers/bats-support/load' -load '../helpers/bats-assert/load' -load '../helpers/bats-file/load' -load '../helpers' - -@test "Relative filepaths: we report all links that target files outside root" { - to_temp xrefcheck -r inner-directory - - assert_diff - < - - - - SPDX-License-Identifier: MPL-2.0 - --> - -[Without anchor](../link-target.md) -[With anchor](../link-target.md#link-target-anchor) diff --git a/tests/golden/check-local-refs/check-local-refs.bats b/tests/golden/check-local-refs/check-local-refs.bats index 3dfe9e98..ef2ef3ac 100644 --- a/tests/golden/check-local-refs/check-local-refs.bats +++ b/tests/golden/check-local-refs/check-local-refs.bats @@ -21,3 +21,9 @@ load '../helpers' assert_diff expected2.gold } + +@test "Checking behavior when there are virtual files, root = \"dir1\"" { + to_temp xrefcheck -r dir1 -c config-with-virtual-files.yaml + + assert_diff expected3.gold +} diff --git a/tests/golden/check-local-references-outside-repo/config-virtual-file.yaml b/tests/golden/check-local-refs/config-with-virtual-files.yaml similarity index 87% rename from tests/golden/check-local-references-outside-repo/config-virtual-file.yaml rename to tests/golden/check-local-refs/config-with-virtual-files.yaml index c6a33118..46dff016 100644 --- a/tests/golden/check-local-references-outside-repo/config-virtual-file.yaml +++ b/tests/golden/check-local-refs/config-with-virtual-files.yaml @@ -3,6 +3,6 @@ # SPDX-License-Identifier: Unlicense verification: virtualFiles: - - ../link-target.md + - ../d0f1.md - ../../a.md - b/../../* diff --git a/tests/golden/check-local-references-outside-repo/link-target.md b/tests/golden/check-local-refs/d0f1.md similarity index 66% rename from tests/golden/check-local-references-outside-repo/link-target.md rename to tests/golden/check-local-refs/d0f1.md index a30a59d8..49a2a499 100644 --- a/tests/golden/check-local-references-outside-repo/link-target.md +++ b/tests/golden/check-local-refs/d0f1.md @@ -4,4 +4,5 @@ - SPDX-License-Identifier: MPL-2.0 --> -# Link target anchor +We are testing links in [this](dir1/dir2/d2f1.md) file + \ No newline at end of file diff --git a/tests/golden/check-local-refs/dir1/dir2/d2f1.md b/tests/golden/check-local-refs/dir1/dir2/d2f1.md index b35d53b0..cbc1edee 100644 --- a/tests/golden/check-local-refs/dir1/dir2/d2f1.md +++ b/tests/golden/check-local-refs/dir1/dir2/d2f1.md @@ -33,3 +33,15 @@ Should be correct when root is `/tests/golden/check-local-refs/dir1`: [file-abs-3](/dir2/d2f2.md) [file-abs-4](/./dir2/../d1f1.md) [anchor-abs-3](/./dir2/../d1f1.md#existing-anchor-d1f1) + +# Test references outside repo + +This should be reported as "reference to file outside repo" when root is `dir1` +[path-through-top-dir](../../dir1/d1f1.md) +[path-through-top-dir-with-anchor](../../dir1/d1f1.md#existing-anchor-d1f1) +[ref-to-d0](../../d0f1.md) + +Such absoulute paths should be reported, +unless we specify in config that we ignore references to them +[A](/../../a.md) +[B](/b/../../b.md) diff --git a/tests/golden/check-local-refs/expected1.gold b/tests/golden/check-local-refs/expected1.gold index 46376571..29963ea8 100644 --- a/tests/golden/check-local-refs/expected1.gold +++ b/tests/golden/check-local-refs/expected1.gold @@ -88,4 +88,24 @@ ././dir2/../d1f1.md -Invalid references dumped, 9 in total. + ➥ In file dir1/dir2/d2f1.md + bad reference (absolute) at src:46:1-16: + - text: "A" + - link: /../../a.md + - anchor: - + + ⛀ Link targets a local file outside repository: + ./../../a.md + + + ➥ In file dir1/dir2/d2f1.md + bad reference (absolute) at src:47:1-18: + - text: "B" + - link: /b/../../b.md + - anchor: - + + ⛀ Link targets a local file outside repository: + ./b/../../b.md + + +Invalid references dumped, 11 in total. diff --git a/tests/golden/check-local-refs/expected2.gold b/tests/golden/check-local-refs/expected2.gold index 09f10d70..15475e26 100644 --- a/tests/golden/check-local-refs/expected2.gold +++ b/tests/golden/check-local-refs/expected2.gold @@ -68,4 +68,54 @@ dir1/dir1/dir2/../../dir1/./dir2/d2f2.md -Invalid references dumped, 7 in total. + ➥ In file dir1/dir2/d2f1.md + bad reference (relative) at src:40:1-42: + - text: "path-through-top-dir" + - link: ../../dir1/d1f1.md + - anchor: - + + ⛀ Link targets a local file outside repository: + dir1/dir2/../../dir1/d1f1.md + + + ➥ In file dir1/dir2/d2f1.md + bad reference (relative) at src:41:1-75: + - text: "path-through-top-dir-with-anchor" + - link: ../../dir1/d1f1.md + - anchor: existing-anchor-d1f1 + + ⛀ Link targets a local file outside repository: + dir1/dir2/../../dir1/d1f1.md + + + ➥ In file dir1/dir2/d2f1.md + bad reference (relative) at src:42:1-26: + - text: "ref-to-d0" + - link: ../../d0f1.md + - anchor: - + + ⛀ Link targets a local file outside repository: + dir1/dir2/../../d0f1.md + + + ➥ In file dir1/dir2/d2f1.md + bad reference (absolute) at src:46:1-16: + - text: "A" + - link: /../../a.md + - anchor: - + + ⛀ Link targets a local file outside repository: + dir1/../../a.md + + + ➥ In file dir1/dir2/d2f1.md + bad reference (absolute) at src:47:1-18: + - text: "B" + - link: /b/../../b.md + - anchor: - + + ⛀ Link targets a local file outside repository: + dir1/b/../../b.md + + +Invalid references dumped, 12 in total. diff --git a/tests/golden/check-local-refs/expected3.gold b/tests/golden/check-local-refs/expected3.gold new file mode 100644 index 00000000..8f634d4c --- /dev/null +++ b/tests/golden/check-local-refs/expected3.gold @@ -0,0 +1,91 @@ +=== Invalid references found === + + ➥ In file dir1/dir2/d2f1.md + bad reference (current file) at src:9:1-18: + - text: "bad-cf-ref" + - link: + - anchor: bad + + ⛀ Anchor 'bad' is not present + + + ➥ In file dir1/dir2/d2f1.md + bad reference (relative) at src:21:1-43: + - text: "bad-file-rel" + - link: ../a/b/c/unexisting-file.md + - anchor: - + + ⛀ File does not exist: + dir1/dir2/../a/b/c/unexisting-file.md + + + ➥ In file dir1/dir2/d2f1.md + bad reference (relative) at src:23:1-38: + - text: "bad-anchor-rel-1" + - link: d2f2.md + - anchor: bad-anchor + + ⛀ Anchor 'bad-anchor' is not present + + + ➥ In file dir1/dir2/d2f1.md + bad reference (relative) at src:24:1-49: + - text: "bad-anchor-rel-2" + - link: unexisting-file.md + - anchor: bad-anchor + + ⛀ File does not exist: + dir1/dir2/unexisting-file.md + + + ➥ In file dir1/dir2/d2f1.md + bad reference (absolute) at src:28:1-29: + - text: "file-abs-1" + - link: /dir1/./d1f1.md + - anchor: - + + ⛀ File does not exist: + dir1/dir1/./d1f1.md + + + ➥ In file dir1/dir2/d2f1.md + bad reference (absolute) at src:29:1-55: + - text: "anchor-abs-1" + - link: /dir1/.././d1f1.md + - anchor: existing-anchor-d1f1 + + ⛀ File does not exist: + dir1/dir1/.././d1f1.md + + + ➥ In file dir1/dir2/d2f1.md + bad reference (absolute) at src:30:1-73: + - text: "anchor-abs-2" + - link: /dir1/dir2/../../dir1/./dir2/d2f2.md + - anchor: existing-anchor-d2f2 + + ⛀ File does not exist: + dir1/dir1/dir2/../../dir1/./dir2/d2f2.md + + + ➥ In file dir1/dir2/d2f1.md + bad reference (relative) at src:40:1-42: + - text: "path-through-top-dir" + - link: ../../dir1/d1f1.md + - anchor: - + + ⛀ Link targets a local file outside repository: + dir1/dir2/../../dir1/d1f1.md + + + ➥ In file dir1/dir2/d2f1.md + bad reference (relative) at src:41:1-75: + - text: "path-through-top-dir-with-anchor" + - link: ../../dir1/d1f1.md + - anchor: existing-anchor-d1f1 + + ⛀ Link targets a local file outside repository: + dir1/dir2/../../dir1/d1f1.md + + +Invalid references dumped, 9 in total.