-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
229 lines (206 loc) · 7.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
const express = require('express')
const rateLimit = require('express-rate-limit')
const cors = require('cors')
const csrf = require('@dr.pogodin/csurf')
const cookieParser = require('cookie-parser')
const helmet = require('helmet')
const fs = require('fs')
const path = require('path')
const sharp = require('sharp')
const port = process.env.PORT || 8080
const accepted = ['jpg', 'jpeg', 'png']
const logoPath = path.join(__dirname, 'Wanderstories-logo.png')
const app = express()
app.set('trust proxy', 1) // trust first proxy
// Use necessary middleware
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.use(cookieParser())
app.use(csrf({ cookie: true })) // Implement CSRF protection
app.use(
helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: [
"'self'",
'https://static.cloudflareinsights.com',
'https://www.googletagmanager.com',
'https://www.google-analytics.com',
],
connectSrc: [
"'self'",
'https://images.wanderstories.space',
'https://static.cloudflareinsights.com',
'https://www.google.com.au',
'https://www.google-analytics.com',
],
styleSrc: ["'self'"],
imgSrc: ["'self'", 'data:', 'https://wanderstories.space'],
objectSrc: ["'none'"],
baseUri: ["'self'"],
formAction: ["'self'"],
frameAncestors: ["'self'"],
upgradeInsecureRequests: [],
},
},
crossOriginOpenerPolicy: { policy: 'same-origin' },
crossOriginResourcePolicy: { policy: 'cross-origin' },
})
) // Implement security headers
const allowedOrigins = [
'https://images.wanderstories.space',
'https://wanderstories.space',
]
const corsOptions = {
origin: function (origin, callback) {
if (allowedOrigins.includes(origin)) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
},
credentials: true,
optionsSuccessStatus: 204,
}
app.use(cors(corsOptions))
// Enable CORS for all routes
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 200,
standardHeaders: true,
legacyHeaders: false,
})
app.use(limiter)
app.get('/', async (req, res, next) => {
res.writeHead(200, {
'Content-Type': 'text/plain',
})
res.end('Wanderstories Image Watermarker')
})
app.get('/favicon.ico', async (req, res, next) => {
res.sendFile(path.join(__dirname, 'favicon.ico'))
})
app.get('/content/images/*', async (req, res, next) => {
// Only allow this specific pattern
try {
const relativePath = req.params[0] // Get the relative path after /content/images/
// Very strict validation to only allow certain characters in the path
if (!/^[\w\-\/\.]+$/.test(relativePath)) {
// invalid path
//console.error('Invalid path');
return res.status(400).send('Invalid request')
}
const fileExtension = path
.extname(relativePath)
.substring(1)
.toLowerCase()
if (!accepted.includes(fileExtension)) {
// invalid file type
//console.error('Invalid file type');
return res.status(400).send('Invalid request')
}
const imagePath = path.join(
__dirname,
'content',
'images',
relativePath
)
try {
// await fs.promises.access(imagePath, fs.constants.F_OK);
await fs.promises.access(imagePath)
return res.sendFile(imagePath)
} catch (err) {
// File doesn't exist (this is expected), continue processing
// console.error("File does not exist, proceeding with processing: ", err);
}
// Split the path by slashes
const pathSegments = relativePath.split('/')
// Encode each segment separately
const encodedSegments = pathSegments.map((segment) =>
encodeURIComponent(segment)
)
// Join them back together with slashes
const encodedPath = encodedSegments.join('/')
// Construct the URL
const originalImageUrl = new URL(
`https://wanderstories.space/content/images/${encodedPath}`
)
let imageBuffer
try {
const imageResponse = await fetch(originalImageUrl.href)
if (!imageResponse.ok) {
console.error('Failed to fetch image')
throw new Error('Failed to fetch image')
}
imageBuffer = await imageResponse.arrayBuffer()
} catch (err) {
//console.error(err);
return res.redirect(originalImageUrl.href)
}
const imageMetadata = await sharp(imageBuffer).metadata()
// Check if image exceeds maximum dimensions
// Note that Ghost only reduces the width to 2000px
if (imageMetadata.width > 2000 || imageMetadata.height > 4000) {
return res.status(413).send('Image too large')
}
const logoMetadata = await sharp(logoPath).metadata()
let compositeLogoBuffer
// Validate that the logo resizing operation is successful before compositing
if (
logoMetadata.width > imageMetadata.width ||
logoMetadata.height > imageMetadata.height
) {
// console.log('Logo dimensions are larger than base image');
// Resize the logo to fit within the base image
compositeLogoBuffer = await sharp(logoPath)
.resize({
width: imageMetadata.width,
height: imageMetadata.height,
fit: 'inside', // Preserve aspect ratio
})
.toBuffer()
} else {
// If the logo is already smaller than the base image, use it as is
compositeLogoBuffer = await sharp(logoPath).toBuffer()
}
// Resize the logo to 1/5 of the base image width for compositing
const resizedLogoBuffer = await sharp(compositeLogoBuffer)
.resize({ width: Math.round(imageMetadata.width / 5) })
.toBuffer()
// Composite the resized logo onto the base image
const outputBuffer = await sharp(imageBuffer)
.composite([
{
input: resizedLogoBuffer,
gravity: 'center',
blend: 'overlay',
},
{
input: resizedLogoBuffer,
gravity: 'center',
blend: 'dest-over',
opacity: 0.1,
},
])
.jpeg({ quality: 60 })
.toBuffer()
// Safely create directory and write file
const directoryPath = path.dirname(imagePath)
try {
await fs.promises.mkdir(directoryPath, { recursive: true })
await fs.promises.writeFile(imagePath, outputBuffer)
} catch (err) {
// Handle file writing error
// console.error("Error writing file: ", err);
return res.status(500).send('Internal Server Error')
}
return res.sendFile(imagePath)
} catch (err) {
//console.error(err);
return res.status(500).send('Internal Server Error')
}
})
app.listen(port, () => {
console.log(`Listening on http://localhost:${port}`)
})