]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/requests.ts
Fix request body limit
[github/Chocobozzz/PeerTube.git] / server / helpers / requests.ts
1 import { createWriteStream, remove } from 'fs-extra'
2 import got, { CancelableRequest, Options as GotOptions } from 'got'
3 import { join } from 'path'
4 import { CONFIG } from '../initializers/config'
5 import { ACTIVITY_PUB, PEERTUBE_VERSION, WEBSERVER } from '../initializers/constants'
6 import { pipelinePromise } from './core-utils'
7 import { processImage } from './image-utils'
8 import { logger } from './logger'
9
10 const httpSignature = require('http-signature')
11
12 type PeerTubeRequestOptions = {
13 activityPub?: boolean
14 bodyKBLimit?: number // 1MB
15 httpSignature?: {
16 algorithm: string
17 authorizationHeaderName: string
18 keyId: string
19 key: string
20 headers: string[]
21 }
22 jsonResponse?: boolean
23 } & Pick<GotOptions, 'headers' | 'json' | 'method' | 'searchParams'>
24
25 const peertubeGot = got.extend({
26 headers: {
27 'user-agent': getUserAgent()
28 },
29
30 handlers: [
31 (options, next) => {
32 const promiseOrStream = next(options) as CancelableRequest<any>
33 const bodyKBLimit = options.context?.bodyKBLimit as number
34 if (!bodyKBLimit) throw new Error('No KB limit for this request')
35
36 const bodyLimit = bodyKBLimit * 1000
37
38 /* eslint-disable @typescript-eslint/no-floating-promises */
39 promiseOrStream.on('downloadProgress', progress => {
40 if (progress.transferred > bodyLimit && progress.percent !== 1) {
41 const message = `Exceeded the download limit of ${bodyLimit} B`
42 logger.warn(message)
43
44 // CancelableRequest
45 if (promiseOrStream.cancel) {
46 promiseOrStream.cancel()
47 return
48 }
49
50 // Stream
51 (promiseOrStream as any).destroy()
52 }
53 })
54
55 return promiseOrStream
56 }
57 ],
58
59 hooks: {
60 beforeRequest: [
61 options => {
62 const headers = options.headers || {}
63 headers['host'] = options.url.host
64 },
65
66 options => {
67 const httpSignatureOptions = options.context?.httpSignature
68
69 if (httpSignatureOptions) {
70 const method = options.method ?? 'GET'
71 const path = options.path ?? options.url.pathname
72
73 if (!method || !path) {
74 throw new Error(`Cannot sign request without method (${method}) or path (${path}) ${options}`)
75 }
76
77 httpSignature.signRequest({
78 getHeader: function (header) {
79 return options.headers[header]
80 },
81
82 setHeader: function (header, value) {
83 options.headers[header] = value
84 },
85
86 method,
87 path
88 }, httpSignatureOptions)
89 }
90 }
91 ]
92 }
93 })
94
95 function doRequest (url: string, options: PeerTubeRequestOptions = {}) {
96 const gotOptions = buildGotOptions(options)
97
98 return peertubeGot(url, gotOptions)
99 .catch(err => { throw buildRequestError(err) })
100 }
101
102 function doJSONRequest <T> (url: string, options: PeerTubeRequestOptions = {}) {
103 const gotOptions = buildGotOptions(options)
104
105 return peertubeGot<T>(url, { ...gotOptions, responseType: 'json' })
106 .catch(err => { throw buildRequestError(err) })
107 }
108
109 async function doRequestAndSaveToFile (
110 url: string,
111 destPath: string,
112 options: PeerTubeRequestOptions = {}
113 ) {
114 const gotOptions = buildGotOptions(options)
115
116 const outFile = createWriteStream(destPath)
117
118 try {
119 await pipelinePromise(
120 peertubeGot.stream(url, gotOptions),
121 outFile
122 )
123 } catch (err) {
124 remove(destPath)
125 .catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err }))
126
127 throw buildRequestError(err)
128 }
129 }
130
131 async function downloadImage (url: string, destDir: string, destName: string, size: { width: number, height: number }) {
132 const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName)
133 await doRequestAndSaveToFile(url, tmpPath)
134
135 const destPath = join(destDir, destName)
136
137 try {
138 await processImage(tmpPath, destPath, size)
139 } catch (err) {
140 await remove(tmpPath)
141
142 throw err
143 }
144 }
145
146 function getUserAgent () {
147 return `PeerTube/${PEERTUBE_VERSION} (+${WEBSERVER.URL})`
148 }
149
150 // ---------------------------------------------------------------------------
151
152 export {
153 doRequest,
154 doJSONRequest,
155 doRequestAndSaveToFile,
156 downloadImage
157 }
158
159 // ---------------------------------------------------------------------------
160
161 function buildGotOptions (options: PeerTubeRequestOptions) {
162 const { activityPub, bodyKBLimit = 1000 } = options
163
164 const context = { bodyKBLimit, httpSignature: options.httpSignature }
165
166 let headers = options.headers || {}
167
168 headers = { ...headers, date: new Date().toUTCString() }
169
170 if (activityPub) {
171 headers = { ...headers, accept: ACTIVITY_PUB.ACCEPT_HEADER }
172 }
173
174 return {
175 method: options.method,
176 json: options.json,
177 searchParams: options.searchParams,
178 headers,
179 context
180 }
181 }
182
183 function buildRequestError (error: any) {
184 const newError = new Error(error.message)
185 newError.name = error.name
186 newError.stack = error.stack
187
188 if (error.response?.body) {
189 error.responseBody = error.response.body
190 }
191
192 return error
193 }