Improve error handling & security

This commit is contained in:
dakedres 2024-01-10 20:02:25 -05:00
parent 3219eb8813
commit 1a1ff23107

View File

@ -1,11 +1,3 @@
// const fetch = require('node-fetch')
// const config = require('./config.js')
// const Path = require('path')
// const { writeFile } = require('fs/promises')
// let cache = require('./cache.json')
// const { JSDOM } = require('jsdom')
import fetch from "node-fetch"
import Path from "path"
import FS from "fs/promises"
@ -47,8 +39,7 @@ const handleNitterUser = async user => {
console.warn(`Failed to fetch ${user} from ${source}`)
index++
} else {
console.error(err)
break
throw err
}
}
}
@ -166,7 +157,7 @@ const processTumblr = (rss, user) => {
const oneDay = 1000 * 60 * 60 * 24
const printFeed = async (sources, directory, header, viewOptions) => {
const printFeed = async (sources, directory, header, viewOptions, error) => {
// Coalate
let feed = []
let tooLongAgo = viewOptions.tooLongAgo ?
@ -214,7 +205,15 @@ const printFeed = async (sources, directory, header, viewOptions) => {
}
if(pages.length == 0) {
writePage(0, renderPage('No posts', [], header, 'No posts available') )
let message = 'No posts available'
if(error) {
// Put in an iframe to prevent potential XSS through response body? Who knows.
message += `<br><br>
<iframe src="data:text/plain,${encodeURIComponent(error.stack)}" style="width: 100%;"></iframe>`
}
writePage(0, renderPage('No posts', [], header, message) )
}
return Promise.all(promises)
@ -291,8 +290,12 @@ ${footer}
return html
}
const renderImage = image => `\
<a href="${image}"><img src="${image}" loading="lazy"></img></a>`
const renderImage = image => {
let { href } = new URL(image)
return `\
<a href="${href}"><img src="${href}" loading="lazy"></img></a>`
}
const main = async () => {
let promises = []
@ -306,18 +309,20 @@ const main = async () => {
let feed = config.feeds[feedName]
let feedSources = []
const subscribe = (postPromise, type, name) =>
postPromise
.catch(error => console.error(error) )
const subscribe = (postPromise, type, name) => {
let source = { type, name, link: Path.join(type, name) }
return postPromise
.catch(error => {
source.error = error
console.error(error)
})
.then(posts => {
feedSources.push(posts)
sources.push({
type,
name,
link: Path.join(type, name),
posts
})
source.posts = posts
sources.push(source)
})
}
if(feed.nitter) {
for(let user of feed.nitter) {
@ -354,8 +359,10 @@ const main = async () => {
let link = buildLink(root + page.link + '/')
let extra = ''
if(page.posts === undefined) {
extra += ' (missing)'
if(page.error) {
extra += ' (errored)'
} else if (page.posts.length == 0) {
extra += ' (empty)'
}
return `<li><a href="${link}">${name}</a>${extra}</li>`
@ -393,7 +400,7 @@ ${sources.map(source => renderEntry(source)).join('\n')}
console.log('Writing...')
for(let source of sources) {
wait(
printFeed([ source.posts ], Path.join('out', source.link), navs[2], config.sourceView)
printFeed([ source.posts ], Path.join('out', source.link), navs[2], config.sourceView, source.error)
)
}
for(let feed of feeds) {