-
Notifications
You must be signed in to change notification settings - Fork 632
fix(lib-storage): restrict lstat calls to fs.ReadStream objects #7411
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
Merged
+159
−53
Merged
Changes from all commits
Commits
Show all changes
2 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 hidden or 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,70 +1,78 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
import fs from "node:fs"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { BYTE_LENGTH_SOURCE, byteLengthSource } from "./byteLengthSource"; | ||
import { runtimeConfig } from "./runtimeConfig"; | ||
import { byteLength } from "./byteLength"; | ||
|
||
vi.mock("./runtimeConfig", () => ({ | ||
runtimeConfig: { | ||
lstatSync: vi.fn(), | ||
}, | ||
})); | ||
|
||
describe("byteLengthSource", () => { | ||
it("should return CONTENT_LENGTH when override is provided", () => { | ||
expect(byteLengthSource({}, 100)).toBe(BYTE_LENGTH_SOURCE.CONTENT_LENGTH); | ||
describe("byteLength", () => { | ||
it("should handle null and undefined input", () => { | ||
expect(byteLength(null)).toBe(0); | ||
expect(byteLength(undefined)).toBe(0); | ||
}); | ||
|
||
it("should return EMPTY_INPUT for null input", () => { | ||
expect(byteLengthSource(null)).toBe(BYTE_LENGTH_SOURCE.EMPTY_INPUT); | ||
}); | ||
describe("strings", () => { | ||
it("empty", () => { | ||
expect(byteLength("")).toBe(0); | ||
}); | ||
|
||
it("should return EMPTY_INPUT for undefined input", () => { | ||
expect(byteLengthSource(undefined)).toBe(BYTE_LENGTH_SOURCE.EMPTY_INPUT); | ||
}); | ||
it("should return correct length for ASCII characters", () => { | ||
expect(byteLength("hello")).toBe(5); | ||
expect(byteLength("12345")).toBe(5); | ||
expect(byteLength("!@#$%")).toBe(5); | ||
}); | ||
|
||
it("should return STRING_LENGTH for string input", () => { | ||
expect(byteLengthSource("test")).toBe(BYTE_LENGTH_SOURCE.STRING_LENGTH); | ||
}); | ||
it("should return correct length for unicode characters", () => { | ||
expect(byteLength("😀")).toBe(4); | ||
}); | ||
|
||
it("should return TYPED_ARRAY for input with byteLength", () => { | ||
const input = new Uint8Array(10); | ||
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.TYPED_ARRAY); | ||
it("should handle mixed ASCII and unicode characters", () => { | ||
expect(byteLength("hello 世界")).toBe(12); | ||
expect(byteLength("hi 😀")).toBe(7); | ||
}); | ||
}); | ||
|
||
it("should return LENGTH for input with length property", () => { | ||
const input = { length: 10 }; | ||
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.LENGTH); | ||
}); | ||
describe("byte arrays", () => { | ||
it("should handle Uint8Array", () => { | ||
expect(byteLength(new Uint8Array([1, 2, 3]))).toBe(3); | ||
expect(byteLength(new Uint8Array([]))).toBe(0); | ||
}); | ||
|
||
it("should return SIZE for input with size property", () => { | ||
const input = { size: 10 }; | ||
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.SIZE); | ||
it("should handle Buffer", () => { | ||
expect(byteLength(Buffer.from([1, 2, 3]))).toBe(3); | ||
expect(byteLength(Buffer.from([]))).toBe(0); | ||
}); | ||
}); | ||
|
||
it("should return START_END_DIFF for input with start and end properties", () => { | ||
const input = { start: 0, end: 10 }; | ||
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.START_END_DIFF); | ||
}); | ||
describe("things with length or size properties", () => { | ||
it("should handle arrays", () => { | ||
expect(byteLength([1, 2, 3])).toBe(3); | ||
expect(byteLength([])).toBe(0); | ||
}); | ||
|
||
it("should return LSTAT for input with path that exists", () => { | ||
const input = { path: "/test/path" }; | ||
vi.mocked(runtimeConfig.lstatSync).mockReturnValue({ size: 100 } as any); | ||
it("should handle objects with length property", () => { | ||
expect(byteLength({ length: 5 })).toBe(5); | ||
expect(byteLength({ length: 0 })).toBe(0); | ||
}); | ||
|
||
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.LSTAT); | ||
expect(runtimeConfig.lstatSync).toHaveBeenCalledWith("/test/path"); | ||
it("should handle objects with size property", () => { | ||
expect(byteLength({ size: 10 })).toBe(10); | ||
expect(byteLength({ size: 0 })).toBe(0); | ||
}); | ||
}); | ||
|
||
it("should return undefined for input with path that throws error", () => { | ||
const input = { path: "/test/path" }; | ||
vi.mocked(runtimeConfig.lstatSync).mockImplementation(() => { | ||
throw new Error("File not found"); | ||
describe("start end differentials", () => { | ||
it("should handle readable streams", () => { | ||
const stream = fs.createReadStream(__filename, { | ||
start: 1000, | ||
end: 1499, | ||
}); | ||
expect(byteLength(stream)).toBe(500); | ||
}); | ||
|
||
expect(byteLengthSource(input)).toBeUndefined(); | ||
}); | ||
|
||
it("should return undefined for input with no matching properties", () => { | ||
const input = { foo: "bar" }; | ||
expect(byteLengthSource(input)).toBeUndefined(); | ||
describe("filestreams", () => { | ||
it("should handle readable streams", () => { | ||
const stream = fs.createReadStream(__filename); | ||
expect(byteLength(stream)).toBe(fs.lstatSync(__filename).size); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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 hidden or 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,68 @@ | ||
import fs from "node:fs"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { BYTE_LENGTH_SOURCE, byteLengthSource } from "./byteLengthSource"; | ||
import { runtimeConfig } from "./runtimeConfig"; | ||
|
||
describe("byteLengthSource", () => { | ||
it("should return CONTENT_LENGTH when override is provided", () => { | ||
expect(byteLengthSource({}, 100)).toBe(BYTE_LENGTH_SOURCE.CONTENT_LENGTH); | ||
}); | ||
|
||
it("should return EMPTY_INPUT for null input", () => { | ||
expect(byteLengthSource(null)).toBe(BYTE_LENGTH_SOURCE.EMPTY_INPUT); | ||
}); | ||
|
||
it("should return EMPTY_INPUT for undefined input", () => { | ||
expect(byteLengthSource(undefined)).toBe(BYTE_LENGTH_SOURCE.EMPTY_INPUT); | ||
}); | ||
|
||
it("should return STRING_LENGTH for string input", () => { | ||
expect(byteLengthSource("test")).toBe(BYTE_LENGTH_SOURCE.STRING_LENGTH); | ||
}); | ||
|
||
it("should return TYPED_ARRAY for input with byteLength", () => { | ||
const input = new Uint8Array(10); | ||
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.TYPED_ARRAY); | ||
}); | ||
|
||
it("should return LENGTH for input with length property", () => { | ||
const input = { length: 10 }; | ||
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.LENGTH); | ||
}); | ||
|
||
it("should return SIZE for input with size property", () => { | ||
const input = { size: 10 }; | ||
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.SIZE); | ||
}); | ||
|
||
it("should return START_END_DIFF for input with start and end properties", () => { | ||
const input = { start: 0, end: 10 }; | ||
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.START_END_DIFF); | ||
}); | ||
|
||
it("should return LSTAT for input with path that exists", () => { | ||
const input = fs.createReadStream(__filename); | ||
vi.spyOn(runtimeConfig, "lstatSync"); | ||
|
||
expect(byteLengthSource(input)).toBe(BYTE_LENGTH_SOURCE.LSTAT); | ||
expect(runtimeConfig.lstatSync).toHaveBeenCalledWith(__filename); | ||
}); | ||
|
||
it("ignores objects with a path property that aren't fs.ReadStream objects", () => { | ||
const input = { path: __filename }; | ||
|
||
expect(byteLengthSource(input)).toBe(undefined); | ||
}); | ||
|
||
it("should return undefined for input with path that throws error", () => { | ||
const input = { path: "surely-this-path-does-not-exist" }; | ||
|
||
expect(byteLengthSource(input)).toBeUndefined(); | ||
}); | ||
|
||
it("should return undefined for input with no matching properties", () => { | ||
const input = { foo: "bar" }; | ||
expect(byteLengthSource(input)).toBeUndefined(); | ||
}); | ||
}); |
This file contains hidden or 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 hidden or 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,9 +1,34 @@ | ||
import fs from "node:fs"; | ||
import { describe, expect, test as it } from "vitest"; | ||
|
||
import * as Storage from "./index"; | ||
import { runtimeConfig } from "./runtimeConfig"; | ||
import { runtimeConfig as runtimeConfigBrowser } from "./runtimeConfig.browser"; | ||
import { runtimeConfig as runtimeConfigNative } from "./runtimeConfig.native"; | ||
|
||
describe("Storage Packages", () => { | ||
it("has Upload", () => { | ||
expect(Storage.Upload).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe("runtimeConfig", () => { | ||
it("all environments have the same signature", () => { | ||
const configs = [runtimeConfig, runtimeConfigBrowser, runtimeConfigNative]; | ||
|
||
for (const config of configs) { | ||
expect(typeof config.lstatSync).toBe("function"); | ||
expect(typeof config.isFileReadStream).toBe("function"); | ||
} | ||
|
||
const stream = fs.createReadStream(__filename); | ||
|
||
expect(runtimeConfig.isFileReadStream(stream)).toBe(true); | ||
expect(runtimeConfigBrowser.isFileReadStream(stream)).toBe(false); | ||
expect(runtimeConfigNative.isFileReadStream(stream)).toBe(false); | ||
|
||
expect(runtimeConfig.runtime).toBe("node"); | ||
expect(runtimeConfigBrowser.runtime).toBe("browser"); | ||
expect(runtimeConfigNative.runtime).toBe("react-native"); | ||
}); | ||
}); |
This file contains hidden or 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 |
---|---|---|
|
@@ -3,4 +3,7 @@ | |
*/ | ||
export const runtimeConfigShared = { | ||
lstatSync: () => {}, | ||
isFileReadStream(f: unknown) { | ||
return false; | ||
}, | ||
}; |
This file contains hidden or 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.
👍