-
Notifications
You must be signed in to change notification settings - Fork 71
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
Add a maximum feed size #906
Open
Half-Shot
wants to merge
10
commits into
main
Choose a base branch
from
hs/maximumFeedSize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1ef0026
Aidd a maximum speed size
Half-Shot 1140a7f
Add changelog.
Half-Shot e29e2cb
Ensure value is a whole number
Half-Shot a46ddc0
Ensure we check MBs
Half-Shot bc49b17
Add a test for maximum feed size.
Half-Shot 3790871
lint
Half-Shot de0953b
update sample config
Half-Shot 6157fbc
Add sensible linter rules.
Half-Shot f2d97af
Fixup tests.
Half-Shot 1b27b4b
lint
Half-Shot 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
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 @@ | ||
Add a maximum allowed payload size to RSS feeds. This prevents oversized feeds from potentially slowing down Hookshot. |
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
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { expect } from "chai"; | ||
import EventEmitter from "events"; | ||
import { BridgeConfigFeeds } from "../src/config/Config"; | ||
import { BridgeConfigFeeds, BridgeConfigFeedsYAML } from "../src/config/Config"; | ||
import { ConnectionManager } from "../src/ConnectionManager"; | ||
import { IConnection } from "../src/Connections"; | ||
import { FeedEntry, FeedReader } from "../src/feeds/FeedReader"; | ||
|
@@ -39,14 +39,15 @@ class MockMessageQueue extends EventEmitter implements MessageQueue { | |
} | ||
} | ||
|
||
async function constructFeedReader(feedResponse: () => {headers: Record<string,string>, data: string}) { | ||
async function constructFeedReader(feedResponse: () => {headers?: Record<string,string>, data: string}, extraConfig?: Partial<BridgeConfigFeedsYAML>) { | ||
|
||
const httpServer = await new Promise<Server>(resolve => { | ||
const srv = createServer((_req, res) => { | ||
res.writeHead(200); | ||
const { headers, data } = feedResponse(); | ||
Object.entries(headers).forEach(([key,value]) => { | ||
Object.entries(headers ?? {}).forEach(([key,value]) => { | ||
res.setHeader(key, value); | ||
}); | ||
res.writeHead(200); | ||
res.write(data); | ||
res.end(); | ||
}).listen(0, '127.0.0.1', () => { | ||
|
@@ -59,6 +60,7 @@ async function constructFeedReader(feedResponse: () => {headers: Record<string,s | |
enabled: true, | ||
pollIntervalSeconds: 1, | ||
pollTimeoutSeconds: 1, | ||
...extraConfig, | ||
}); | ||
const cm = new MockConnectionManager([{ feedUrl } as unknown as IConnection]) as unknown as ConnectionManager | ||
const mq = new MockMessageQueue(); | ||
|
@@ -71,7 +73,12 @@ async function constructFeedReader(feedResponse: () => {headers: Record<string,s | |
const feedReader = new FeedReader( | ||
config, cm, mq, storage, | ||
); | ||
after(() => httpServer.close()); | ||
|
||
after(() => { | ||
httpServer.close() | ||
feedReader.stop(); | ||
}); | ||
|
||
return {config, cm, events, feedReader, feedUrl, httpServer, storage}; | ||
} | ||
|
||
|
@@ -94,12 +101,12 @@ describe("FeedReader", () => { | |
})); | ||
|
||
await feedReader.pollFeed(feedUrl); | ||
feedReader.stop(); | ||
expect(events).to.have.lengthOf(1); | ||
|
||
expect(events[0].data.feed.title).to.equal(null); | ||
expect(events[0].data.title).to.equal(null); | ||
}); | ||
|
||
it("should handle RSS 2.0 feeds", async () => { | ||
const { events, feedReader, feedUrl } = await constructFeedReader(() => ({ | ||
headers: {}, data: ` | ||
|
@@ -127,7 +134,6 @@ describe("FeedReader", () => { | |
})); | ||
|
||
await feedReader.pollFeed(feedUrl); | ||
feedReader.stop(); | ||
expect(events).to.have.lengthOf(1); | ||
|
||
expect(events[0].data.feed.title).to.equal('RSS Title'); | ||
|
@@ -137,6 +143,7 @@ describe("FeedReader", () => { | |
expect(events[0].data.link).to.equal('http://www.example.com/blog/post/1'); | ||
expect(events[0].data.pubdate).to.equal('Sun, 6 Sep 2009 16:20:00 +0000'); | ||
}); | ||
|
||
it("should handle RSS feeds with a permalink url", async () => { | ||
const { events, feedReader, feedUrl } = await constructFeedReader(() => ({ | ||
headers: {}, data: ` | ||
|
@@ -163,7 +170,6 @@ describe("FeedReader", () => { | |
})); | ||
|
||
await feedReader.pollFeed(feedUrl); | ||
feedReader.stop(); | ||
expect(events).to.have.lengthOf(1); | ||
|
||
expect(events[0].data.feed.title).to.equal('RSS Title'); | ||
|
@@ -173,6 +179,7 @@ describe("FeedReader", () => { | |
expect(events[0].data.link).to.equal('http://www.example.com/blog/post/1'); | ||
expect(events[0].data.pubdate).to.equal('Sun, 6 Sep 2009 16:20:00 +0000'); | ||
}); | ||
|
||
it("should handle Atom feeds", async () => { | ||
const { events, feedReader, feedUrl } = await constructFeedReader(() => ({ | ||
headers: {}, data: ` | ||
|
@@ -203,7 +210,6 @@ describe("FeedReader", () => { | |
})); | ||
|
||
await feedReader.pollFeed(feedUrl); | ||
feedReader.stop(); | ||
expect(events).to.have.lengthOf(1); | ||
|
||
expect(events[0].data.feed.title).to.equal('Example Feed'); | ||
|
@@ -213,6 +219,7 @@ describe("FeedReader", () => { | |
expect(events[0].data.link).to.equal('http://example.org/2003/12/13/atom03'); | ||
expect(events[0].data.pubdate).to.equal('Sat, 13 Dec 2003 18:30:02 +0000'); | ||
}); | ||
|
||
it("should not duplicate feed entries", async () => { | ||
const { events, feedReader, feedUrl } = await constructFeedReader(() => ({ | ||
headers: {}, data: ` | ||
|
@@ -235,9 +242,9 @@ describe("FeedReader", () => { | |
await feedReader.pollFeed(feedUrl); | ||
await feedReader.pollFeed(feedUrl); | ||
await feedReader.pollFeed(feedUrl); | ||
feedReader.stop(); | ||
expect(events).to.have.lengthOf(1); | ||
}); | ||
|
||
it("should always hash to the same value for Atom feeds", async () => { | ||
const expectedHash = ['md5:d41d8cd98f00b204e9800998ecf8427e']; | ||
const { feedReader, feedUrl, storage } = await constructFeedReader(() => ({ | ||
|
@@ -254,10 +261,10 @@ describe("FeedReader", () => { | |
})); | ||
|
||
await feedReader.pollFeed(feedUrl); | ||
feedReader.stop(); | ||
const items = await storage.hasSeenFeedGuids(feedUrl, ...expectedHash); | ||
expect(items).to.deep.equal(expectedHash); | ||
}); | ||
|
||
it("should always hash to the same value for RSS feeds", async () => { | ||
const expectedHash = [ | ||
'md5:98bafde155b931e656ad7c137cd7711e', // guid | ||
|
@@ -288,8 +295,53 @@ describe("FeedReader", () => { | |
})); | ||
|
||
await feedReader.pollFeed(feedUrl); | ||
feedReader.stop(); | ||
const items = await storage.hasSeenFeedGuids(feedUrl, ...expectedHash); | ||
expect(items).to.deep.equal(expectedHash); | ||
}); | ||
|
||
it("should fail to handle a feed which exceed the maximum size.", async () => { | ||
// Create some data of the right length | ||
const data = ` | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<rss version="2.0"> | ||
<channel> | ||
<title>RSS Title</title> | ||
<description>This is an example of an RSS feed</description> | ||
${Array.from({length: 8000}).map((_, i) => `<item> | ||
<title>Example entry</title> | ||
<guid isPermaLink="true">http://www.example.com/blog/post/${i}</guid> | ||
</item>`).join('')} | ||
</channel> | ||
</rss>`; | ||
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. Note: this is fine, measuring at 1.2 MB. If we wanted to be extra careful, we could do an assert checking if it's longer than 1024*1024. |
||
const { feedReader, feedUrl } = await constructFeedReader(() => ({ | ||
data, headers: { 'Content-Length': data.length.toString()} | ||
}), { | ||
maximumFeedSizeMB: 1 | ||
}); | ||
await feedReader.pollFeed(feedUrl); | ||
expect(feedReader["feedsFailingParsing"]).to.contain(feedUrl); | ||
}); | ||
|
||
it("should fail to handle a feed which exceed the maximum size which does NOT send a Content-Length.", async () => { | ||
// Create some data of the right length | ||
const data = ` | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<rss version="2.0"> | ||
<channel> | ||
<title>RSS Title</title> | ||
<description>This is an example of an RSS feed</description> | ||
${Array.from({length: 8000}).map((_, i) => `<item> | ||
<title>Example entry</title> | ||
<guid isPermaLink="true">http://www.example.com/blog/post/${i}</guid> | ||
</item>`).join('')} | ||
</channel> | ||
</rss>`; | ||
const { feedReader, feedUrl } = await constructFeedReader(() => ({ | ||
data | ||
}), { | ||
maximumFeedSizeMB: 1 | ||
}); | ||
await feedReader.pollFeed(feedUrl); | ||
expect(feedReader["feedsFailingParsing"]).to.contain(feedUrl); | ||
}); | ||
}); |
Oops, something went wrong.
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.
Minor: I'd just write it as
if body.len() as u64 <= max_content_size {
..., sinceif
is also an expression in Rust.match
kinda makes it seems like there's more to it than that.