-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(upload): bug sur l'ajout de l'extension #1367
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { isUploadFileSafe, isAllowedFile } from "../secu"; | ||
import * as formidable from "formidable"; | ||
import fs from "fs"; | ||
|
||
jest.mock("fs"); | ||
|
||
describe("secu.ts", () => { | ||
describe("isAllowedFile", () => { | ||
test("should return true for allowed extensions", () => { | ||
const file: formidable.File = { | ||
originalFilename: "test.png", | ||
} as formidable.File; | ||
|
||
expect(isAllowedFile(file)).toBe(true); | ||
}); | ||
|
||
test("should return false for disallowed extensions", () => { | ||
const file: formidable.File = { | ||
originalFilename: "test.exe", | ||
} as formidable.File; | ||
|
||
expect(isAllowedFile(file)).toBe(false); | ||
}); | ||
|
||
test("should return false if filename is undefined", () => { | ||
const file: formidable.File = { | ||
originalFilename: undefined, | ||
} as any; | ||
|
||
expect(isAllowedFile(file)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("isUploadFileSafe", () => { | ||
test("should resolve false for disallowed file extensions", async () => { | ||
const file: formidable.File = { | ||
originalFilename: "test.exe", | ||
mimetype: "application/octet-stream", | ||
} as formidable.File; | ||
|
||
await expect(isUploadFileSafe(file)).resolves.toBe(false); | ||
}); | ||
|
||
test("should resolve true for non-SVG file types", async () => { | ||
const file: formidable.File = { | ||
originalFilename: "test.png", | ||
mimetype: "image/png", | ||
} as formidable.File; | ||
|
||
await expect(isUploadFileSafe(file)).resolves.toBe(true); | ||
}); | ||
|
||
test("should resolve true for SVG files without script tags", async () => { | ||
(fs.readFileSync as jest.Mock).mockReturnValue("<svg></svg>"); | ||
|
||
const file: formidable.File = { | ||
originalFilename: "test.svg", | ||
mimetype: "image/svg+xml", | ||
} as formidable.File; | ||
|
||
await expect(isUploadFileSafe(file)).resolves.toBe(true); | ||
}); | ||
|
||
test("should resolve false for SVG files with script tags", async () => { | ||
(fs.readFileSync as jest.Mock).mockReturnValue( | ||
'<svg><script>alert("XSS")</script></svg>' | ||
); | ||
|
||
const file: formidable.File = { | ||
originalFilename: "test.svg", | ||
mimetype: "image/svg+xml", | ||
} as formidable.File; | ||
|
||
await expect(isUploadFileSafe(file)).resolves.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
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 |
---|---|---|
@@ -1,22 +1,19 @@ | ||
import Boom from "@hapi/boom"; | ||
import formidable, { IncomingForm } from "formidable"; | ||
import { createErrorFor } from "src/lib/apiError"; | ||
import { isUploadFileSafe } from "src/lib/secu"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { getApiAllFiles, uploadApiFiles } from "src/lib/upload"; | ||
import fs from "fs"; | ||
|
||
async function endPoint(req: NextApiRequest, res: NextApiResponse) { | ||
const apiError = createErrorFor(res); | ||
|
||
switch (req.method) { | ||
case "POST": | ||
return uploadFiles(req, res); | ||
case "GET": | ||
return getFiles(req, res); | ||
default: { | ||
res.setHeader("Allow", "GET, POST"); | ||
apiError(Boom.methodNotAllowed(`${req.method} not allowed`)); | ||
return res | ||
.status(400) | ||
.json({ success: false, errorMessage: `${req.method} not allowed` }); | ||
} | ||
} | ||
} | ||
|
@@ -34,14 +31,16 @@ function uploadFiles(req: NextApiRequest, res: NextApiResponse) { | |
const file = allFiles[i]; | ||
const isSafe = await isUploadFileSafe(file); | ||
if (!isSafe) { | ||
console.error("A malicious code was find in the upload"); | ||
return res.status(400).json({ success: false }); | ||
console.error("Malicious code detected"); | ||
return res | ||
.status(400) | ||
.json({ success: false, errorMessage: "Malicious code detected" }); | ||
} | ||
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Je gère mieux les erreurs comme ça, car ça venait du fait que le fichier était pas considéré comme safe |
||
const fileContent = fs.readFileSync(file.filepath); | ||
await uploadApiFiles(`${file.originalFilename}`, fileContent); | ||
} | ||
res.status(200).json({ success: true }); | ||
}); | ||
res.status(200).json({ success: true }); | ||
} | ||
|
||
async function getFiles(_req: NextApiRequest, res: NextApiResponse) { | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tout ça à cause de ça x)