MCPcopy Index your code
hub / github.com/scality/cloudserver / wrapCallbackWithErrorCorsHeaders

Function wrapCallbackWithErrorCorsHeaders

lib/api/api.js:135–182  ·  view source on GitHub ↗
(callback, request, response, log)

Source from the content-addressed store, hash-verified

133// improve that ceiling; the unit test in tests/unit/api/corsErrorHeaders.js
134// asserts it.
135function wrapCallbackWithErrorCorsHeaders(callback, request, response, log) {
136 function applyHeadersFromBucket(bucket) {
137 if (!bucket || response.headersSent) {
138 return;
139 }
140 const headers = collectCorsHeaders(
141 request.headers.origin, request.method, bucket);
142 Object.keys(headers).forEach(key => {
143 try {
144 response.setHeader(key, headers[key]);
145 } catch (e) {
146 log.debug('could not set CORS header on error', {
147 header: key, error: e.message,
148 });
149 }
150 });
151 }
152
153 return (err, ...callbackArgs) => {
154 if (!err || !request.headers || !request.headers.origin
155 || !request.bucketName) {
156 return callback(err, ...callbackArgs);
157 }
158 // Fast path: most post-auth failures come back with corsHeaders
159 // already computed by the handler. The route will forward them
160 // to responseXMLBody/responseNoBody, so no extra lookup is needed.
161 if (hasCorsHeaders(callbackArgs)) {
162 return callback(err, ...callbackArgs);
163 }
164 // Fallback: either the bucket was never loaded (pre-handler
165 // failure like auth.server.doAuth denial, header-size check,
166 // copy-source parse error) or the handler returned without
167 // surfacing CORS headers. Re-fetch the bucket using the
168 // request's bucketName (the destination on copy operations) so
169 // we evaluate CORS against the right bucket.
170 return metadata.getBucket(request.bucketName, log, (mdErr, bucket) => {
171 if (mdErr) {
172 log.warn('could not fetch bucket CORS config for error response', {
173 bucketName: request.bucketName,
174 error: mdErr.code || mdErr.message,
175 });
176 return callback(err, ...callbackArgs);
177 }
178 applyHeadersFromBucket(bucket);
179 return callback(err, ...callbackArgs);
180 });
181 };
182}
183
184function checkAuthResults(authResults, apiMethod, log) {
185 let returnTagCount = true;

Callers 1

callApiMethodFunction · 0.85

Calls 3

hasCorsHeadersFunction · 0.85
applyHeadersFromBucketFunction · 0.85
warnMethod · 0.80

Tested by

no test coverage detected