-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validate files to be uploaded (#3872)
* Only allow certain file types to be uploaded * refactor file type checking * move file type check to docs-validator * Add file types rule to all rules list * Split out validator function and add a test * Check if file exists before checking its type
- Loading branch information
1 parent
1ae3090
commit a262eb7
Showing
18 changed files
with
264 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+6.8 KB
.yarn/cache/readable-web-to-node-stream-npm-3.0.2-682f5de297-8c56cc62c6.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Binary file added
BIN
+54 KB
packages/cli/yaml/docs-validator/src/__test__/valid-file-types/bird.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+53.5 KB
packages/cli/yaml/docs-validator/src/__test__/valid-file-types/bird.zip
Binary file not shown.
17 changes: 17 additions & 0 deletions
17
packages/cli/yaml/docs-validator/src/__test__/valid-file-types/valid-file-types.test.ts
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,17 @@ | ||
import { readFile } from "fs/promises"; | ||
import path from "path"; | ||
import { isValidFileType } from "../../rules/valid-file-types/valid-file-types"; | ||
|
||
describe("isValidFileType", () => { | ||
it("should return true for valid file types", async () => { | ||
const file = await readFile(path.join(__dirname, "bird.jpg")); | ||
const isValid = await isValidFileType(file); | ||
expect(isValid).toBe(true); | ||
}); | ||
|
||
it("should return false for invalid file types", async () => { | ||
const file = await readFile(path.join(__dirname, "bird.zip")); | ||
const isValid = await isValidFileType(file); | ||
expect(isValid).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
1 change: 1 addition & 0 deletions
1
packages/cli/yaml/docs-validator/src/rules/valid-file-types/index.ts
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 @@ | ||
export { ValidFileTypes } from "./valid-file-types"; |
67 changes: 67 additions & 0 deletions
67
packages/cli/yaml/docs-validator/src/rules/valid-file-types/valid-file-types.ts
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,67 @@ | ||
import { doesPathExist } from "@fern-api/fs-utils"; | ||
import { fileTypeFromBuffer } from "file-type"; | ||
import { readFile } from "fs/promises"; | ||
import isSvg from "is-svg"; | ||
import { Rule } from "../../Rule"; | ||
|
||
const ALLOWED_FILE_TYPES = [ | ||
// image files | ||
"image/jpeg", | ||
"image/png", | ||
"image/gif", | ||
"image/webp", | ||
"image/tiff", | ||
// video files | ||
"video/quicktime", | ||
"video/mp4", | ||
"video/webm", | ||
// audio files | ||
"audio/mpeg", | ||
"audio/ogg", | ||
"audio/wav", | ||
// document files | ||
"application/pdf" | ||
]; | ||
|
||
export const ValidFileTypes: Rule = { | ||
name: "valid-file-types", | ||
create: () => { | ||
return { | ||
filepath: async ({ absoluteFilepath, value }) => { | ||
const doesExist = await doesPathExist(absoluteFilepath); | ||
|
||
if (doesExist) { | ||
const file = await readFile(absoluteFilepath); | ||
const isValid = await isValidFileType(file); | ||
|
||
if (!isValid) { | ||
return [ | ||
{ | ||
severity: "error", | ||
message: `File type of ${value} is invalid` | ||
} | ||
]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
export const isValidFileType = async (file: Buffer): Promise<boolean> => { | ||
if (isSvg(file.toString("utf-8"))) { | ||
// exit early if the file is an SVG | ||
return true; | ||
} | ||
|
||
// otherwise, check the file type | ||
const fileType = await fileTypeFromBuffer(file); | ||
if (fileType && ALLOWED_FILE_TYPES.includes(fileType.mime)) { | ||
return true; | ||
} | ||
|
||
// in all other cases, return false | ||
return false; | ||
}; |
Oops, something went wrong.