-
-
Notifications
You must be signed in to change notification settings - Fork 15
fix: make episode identity key collision-resistant and prototype-safe #226
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
Merged
Changes from all commits
Commits
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
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,96 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import type { Episode } from "src/types/Episode"; | ||
| import type { PlayedEpisode } from "src/types/PlayedEpisode"; | ||
| import type { PodcastFeed } from "src/types/PodcastFeed"; | ||
|
|
||
| // Feed parsing hits the network, so stub it with a per-url lookup the tests fill in. | ||
| const { feedEpisodes } = vi.hoisted(() => ({ | ||
| feedEpisodes: new Map<string, Episode[]>(), | ||
| })); | ||
|
|
||
| vi.mock("src/parser/feedParser", () => ({ | ||
| default: class { | ||
| private feed: PodcastFeed | undefined; | ||
| constructor(feed?: PodcastFeed) { | ||
| this.feed = feed; | ||
| } | ||
| async getEpisodes(url: string): Promise<Episode[]> { | ||
| return feedEpisodes.get(url) ?? []; | ||
| } | ||
| }, | ||
| })); | ||
|
|
||
| import findPlayedEpisodesInFeeds from "./findPlayedEpisodes"; | ||
|
|
||
| function playedEp(title: string, podcastName: string): PlayedEpisode { | ||
| return { title, podcastName, time: 0, duration: 0, finished: true }; | ||
| } | ||
|
|
||
| function episode(title: string, podcastName: string): Episode { | ||
| return { | ||
| title, | ||
| podcastName, | ||
| streamUrl: "", | ||
| url: "", | ||
| description: "", | ||
| content: "", | ||
| }; | ||
| } | ||
|
|
||
| function feed(title: string, url: string): PodcastFeed { | ||
| return { title, url, artworkUrl: "" }; | ||
| } | ||
|
|
||
| describe("findPlayedEpisodesInFeeds", () => { | ||
| beforeEach(() => { | ||
| feedEpisodes.clear(); | ||
| }); | ||
|
|
||
| it("does not crash when a feed-controlled podcast name shadows Object.prototype (#other-unsafe-object-key)", async () => { | ||
| // "__proto__"/"constructor"/"hasOwnProperty" resolve to inherited members on | ||
| // a plain object accumulator and would throw on `.push`; a Map keys safely. | ||
| const played = [ | ||
| playedEp("Ep A", "__proto__"), | ||
| playedEp("Ep B", "constructor"), | ||
| playedEp("Ep C", "hasOwnProperty"), | ||
| ]; | ||
|
|
||
| await expect(findPlayedEpisodesInFeeds(played, [])).resolves.toEqual([]); | ||
| }); | ||
|
|
||
| it("returns the feed episodes whose title matches a played episode, including for a __proto__ feed", async () => { | ||
| feedEpisodes.set("https://a.example/feed", [ | ||
| episode("Ep A", "Pod A"), | ||
| episode("Ep Z", "Pod A"), | ||
| ]); | ||
| feedEpisodes.set("https://proto.example/feed", [ | ||
| episode("Ep P", "__proto__"), | ||
| ]); | ||
|
|
||
| const played = [ | ||
| playedEp("Ep A", "Pod A"), | ||
| playedEp("Missing", "Pod A"), | ||
| playedEp("Ep P", "__proto__"), | ||
| ]; | ||
| const feeds = [ | ||
| feed("Pod A", "https://a.example/feed"), | ||
| feed("__proto__", "https://proto.example/feed"), | ||
| ]; | ||
|
|
||
| const result = await findPlayedEpisodesInFeeds(played, feeds); | ||
|
|
||
| expect(result.map((e) => e.title)).toEqual(["Ep A", "Ep P"]); | ||
| }); | ||
|
|
||
| it("skips played episodes whose podcast is not among the feeds", async () => { | ||
| feedEpisodes.set("https://a.example/feed", [episode("Ep A", "Pod A")]); | ||
|
|
||
| const result = await findPlayedEpisodesInFeeds( | ||
| [playedEp("Ep A", "Pod A"), playedEp("Ep B", "Unsubscribed")], | ||
| [feed("Pod A", "https://a.example/feed")], | ||
| ); | ||
|
|
||
| expect(result.map((e) => e.title)).toEqual(["Ep A"]); | ||
| }); | ||
| }); |
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.
When an existing episode has a delimiter-forging name/title, this now returns a new escaped key, but prior releases saved its progress under the raw
${podcastName}::${title}key. The restore path (getPlayedEpisode/restoreSavedPlaybackTime) only probesgetEpisodeKey(episode)and the title-only legacy key, so a user who already has progress for a podcast likeA/B::Cwill lose the saved position/finished state after upgrading until the episode is played again. Please keep the old raw composite key as an alias or migrate it for the ambiguous cases.Useful? React with 👍 / 👎.