-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This should hopefully reduce confusion for users.
- Loading branch information
Showing
7 changed files
with
198 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { canAccess } from "./canAccess"; | ||
|
||
|
||
test("returns false on path traversal", () => { | ||
// Arrange | ||
const directoryPath = '/tmp/..'; | ||
|
||
// Act | ||
const access = canAccess(directoryPath); | ||
|
||
// Assert | ||
expect(access).toBe(false); | ||
}) | ||
|
||
test("returns true for workspace path", () => { | ||
// Arrange | ||
const directoryPath = '/tmp/download/03d993ad-7b85-44bc-a810-aa3098a1b483' | ||
|
||
// Act | ||
const access = canAccess(directoryPath, '/tmp'); | ||
|
||
// Assert | ||
expect(access).toBe(true); | ||
}) | ||
|
||
test("returns false for relative path", () => { | ||
// Arrange | ||
const directoryPath = '~/.config'; | ||
|
||
// Act | ||
const access = canAccess(directoryPath); | ||
|
||
// Assert | ||
expect(access).toBe(false); | ||
}) | ||
|
||
test("returns false if outside of basePath", () => { | ||
// Arrange | ||
const directoryPath = '/home/user/Downloads'; | ||
|
||
// Act | ||
const access = canAccess(directoryPath, '/tmp/workspace'); | ||
|
||
// Assert | ||
expect(access).toBe(false); | ||
}) | ||
|
||
test("returns true for APKG in workspace", () => { | ||
// Arrange | ||
const directoryPath = '/tmp/download/03d993ad-7b85-44bc-a810-aa3098a1b483/x.apkg' | ||
|
||
// Act | ||
const access = canAccess(directoryPath, '/tmp'); | ||
|
||
// Assert | ||
expect(access).toBe(true); | ||
}) | ||
|
||
|
||
test("returns false for newlines ", () => { | ||
// Arrange | ||
const newLines = "This is a long string" | ||
+ " that spans multiple lines." | ||
+ "\nIt can contain newlines" | ||
+ " and other characters without any issues."; | ||
|
||
// Act | ||
const access = canAccess(newLines); | ||
|
||
// Assert | ||
expect(access).toBe(false); | ||
}) | ||
|
||
test("returns false for long filename", () => { | ||
// Arrange | ||
const longString = "A musical instrument is a device created or adapted to make musical sounds. In principle, any object that produces sound can be considered a musical instrument—it is through purpose that the object becomes a musical instrument. A person who plays a musical instrument is known as an instrumentalist. The history of musical instruments dates to the beginnings of human culture. Early musical instruments may have been used for rituals, such as a horn to signal success on the hunt, or a drum in a religious ceremony. Cultures eventually developed composition and performance of melodies for entertainment. Musical instruments evolved in step with changing applications and technologies." | ||
|
||
// Act | ||
const access = canAccess(longString); | ||
|
||
// Assert | ||
expect(access).toBe(false); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const MAX_FILE_NAME_LENGTH = 255; | ||
|
||
export const canAccess = (thePath: string, basePath?: string) => { | ||
console.log('canAccess', thePath, basePath); | ||
|
||
if (thePath.includes('..')) { | ||
return false; | ||
} | ||
|
||
if (thePath.includes('~')) { | ||
return false; | ||
} | ||
|
||
if (basePath) { | ||
return thePath.startsWith(basePath); | ||
} | ||
|
||
if (thePath.length >= MAX_FILE_NAME_LENGTH) { | ||
return false; | ||
} | ||
|
||
return /^[\w\-. ]+$/.test(thePath); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters