@verdaccio/middleware 8.0.0-next-8.12 → 8.0.0-next-8.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -24
- package/package.json +12 -8
- package/.babelrc +0 -14
- package/CHANGELOG.md +0 -1324
- package/src/index.ts +0 -23
- package/src/middlewares/allow.ts +0 -66
- package/src/middlewares/antiLoop.ts +0 -41
- package/src/middlewares/api_urls.ts +0 -45
- package/src/middlewares/encode-pkg.ts +0 -39
- package/src/middlewares/error.ts +0 -79
- package/src/middlewares/final.ts +0 -71
- package/src/middlewares/json.ts +0 -15
- package/src/middlewares/log.ts +0 -105
- package/src/middlewares/make-url-relative.ts +0 -46
- package/src/middlewares/match.ts +0 -16
- package/src/middlewares/media.ts +0 -29
- package/src/middlewares/rate-limit.ts +0 -8
- package/src/middlewares/security-headers.ts +0 -21
- package/src/middlewares/user-agent.ts +0 -11
- package/src/middlewares/validation.ts +0 -31
- package/src/middlewares/web/index.ts +0 -4
- package/src/middlewares/web/render-web.ts +0 -107
- package/src/middlewares/web/security.ts +0 -13
- package/src/middlewares/web/utils/manifest.ts +0 -31
- package/src/middlewares/web/utils/renderHTML.ts +0 -157
- package/src/middlewares/web/utils/template.ts +0 -50
- package/src/middlewares/web/utils/web-utils.ts +0 -18
- package/src/middlewares/web/web-api.ts +0 -30
- package/src/middlewares/web/web-middleware.ts +0 -16
- package/src/middlewares/web/web-urls.ts +0 -25
- package/src/types.ts +0 -11
- package/test/__snapshots__/template.test.ts.snap +0 -131
- package/test/_helper.ts +0 -8
- package/test/allow.spec.ts +0 -110
- package/test/antiLoop.spec.ts +0 -90
- package/test/config/dark-logo.png +0 -0
- package/test/config/default-test.yaml +0 -28
- package/test/config/favicon.ico +0 -0
- package/test/config/file-logo.yaml +0 -26
- package/test/config/http-logo.yaml +0 -26
- package/test/config/login-disabled.yaml +0 -30
- package/test/config/no-logo.yaml +0 -25
- package/test/config/web.yaml +0 -24
- package/test/config/wrong-logo.yaml +0 -24
- package/test/encode.spec.ts +0 -131
- package/test/final.spec.ts +0 -62
- package/test/helper.ts +0 -14
- package/test/json.spec.ts +0 -33
- package/test/log.spec.ts +0 -29
- package/test/make-url-relative.spec.ts +0 -42
- package/test/manifest.test.ts +0 -25
- package/test/media.spec.ts +0 -56
- package/test/params.spec.ts +0 -38
- package/test/partials/htmlParser.ts +0 -1
- package/test/partials/manifest/manifest.json +0 -64
- package/test/render.spec.ts +0 -156
- package/test/security.spec.ts +0 -55
- package/test/static/main.js +0 -3
- package/test/static/runtime.js +0 -3
- package/test/static/vendors.js +0 -3
- package/test/template.test.ts +0 -71
- package/test/utils.spec.ts +0 -15
- package/test/validation.spec.ts +0 -93
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -26
- package/types/custom.d.ts +0 -20
package/src/index.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
export { match } from './middlewares/match';
|
|
2
|
-
export { setSecurityWebHeaders } from './middlewares/security-headers';
|
|
3
|
-
export { validateName, validatePackage } from './middlewares/validation';
|
|
4
|
-
export { media } from './middlewares/media';
|
|
5
|
-
export { makeURLrelative } from './middlewares/make-url-relative';
|
|
6
|
-
export { encodeScopePackage } from './middlewares/encode-pkg';
|
|
7
|
-
export { expectJson } from './middlewares/json';
|
|
8
|
-
export { antiLoop } from './middlewares/antiLoop';
|
|
9
|
-
export { final } from './middlewares/final';
|
|
10
|
-
export { allow } from './middlewares/allow';
|
|
11
|
-
export { rateLimit } from './middlewares/rate-limit';
|
|
12
|
-
export { userAgent } from './middlewares/user-agent';
|
|
13
|
-
export { webMiddleware, renderWebMiddleware } from './middlewares/web';
|
|
14
|
-
export { errorReportingMiddleware, handleError } from './middlewares/error';
|
|
15
|
-
export {
|
|
16
|
-
log,
|
|
17
|
-
LOG_STATUS_MESSAGE,
|
|
18
|
-
LOG_VERDACCIO_BYTES,
|
|
19
|
-
LOG_VERDACCIO_ERROR,
|
|
20
|
-
} from './middlewares/log';
|
|
21
|
-
export * from './types';
|
|
22
|
-
export * from './middlewares/api_urls';
|
|
23
|
-
export * from './middlewares/web/web-urls';
|
package/src/middlewares/allow.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import buildDebug from 'debug';
|
|
2
|
-
|
|
3
|
-
import { API_ERROR, errorUtils, tarballUtils } from '@verdaccio/core';
|
|
4
|
-
|
|
5
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
|
6
|
-
|
|
7
|
-
const debug = buildDebug('verdaccio:middleware:allow');
|
|
8
|
-
|
|
9
|
-
export function allow<T>(
|
|
10
|
-
auth: T,
|
|
11
|
-
options = {
|
|
12
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
13
|
-
beforeAll: (_a: any, _b: any) => {},
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
15
|
-
afterAll: (_a: any, _b: any) => {},
|
|
16
|
-
}
|
|
17
|
-
): Function {
|
|
18
|
-
const { beforeAll, afterAll } = options;
|
|
19
|
-
return function (action: string): Function {
|
|
20
|
-
return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
|
|
21
|
-
req.pause();
|
|
22
|
-
const packageName = req.params.scope
|
|
23
|
-
? `@${req.params.scope}/${req.params.package}`
|
|
24
|
-
: req.params.package;
|
|
25
|
-
const packageVersion = req.params.filename
|
|
26
|
-
? tarballUtils.getVersionFromTarball(req.params.filename)
|
|
27
|
-
: req.params.version
|
|
28
|
-
? req.params.version
|
|
29
|
-
: undefined;
|
|
30
|
-
const remote_user = req.remote_user;
|
|
31
|
-
debug(
|
|
32
|
-
'check if user %o can %o package %o version %o',
|
|
33
|
-
remote_user?.name,
|
|
34
|
-
action,
|
|
35
|
-
packageName,
|
|
36
|
-
packageVersion
|
|
37
|
-
);
|
|
38
|
-
beforeAll?.(
|
|
39
|
-
{ action, user: remote_user?.name },
|
|
40
|
-
`[middleware/allow][@{action}] allow for @{user}`
|
|
41
|
-
);
|
|
42
|
-
auth['allow_' + action](
|
|
43
|
-
{ packageName, packageVersion },
|
|
44
|
-
remote_user,
|
|
45
|
-
function (error, allowed): void {
|
|
46
|
-
req.resume();
|
|
47
|
-
if (error) {
|
|
48
|
-
debug('user is NOT allowed to %o', action);
|
|
49
|
-
next(error);
|
|
50
|
-
} else if (allowed) {
|
|
51
|
-
debug('user is allowed to %o', action);
|
|
52
|
-
afterAll?.(
|
|
53
|
-
{ action, user: remote_user?.name },
|
|
54
|
-
`[middleware/allow][@{action}] allowed for @{user}`
|
|
55
|
-
);
|
|
56
|
-
next();
|
|
57
|
-
} else {
|
|
58
|
-
// last plugin (that's our built-in one) returns either
|
|
59
|
-
// cb(err) or cb(null, true), so this should never happen
|
|
60
|
-
throw errorUtils.getInternalError(API_ERROR.PLUGIN_ERROR);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
);
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { HTTP_STATUS, errorUtils } from '@verdaccio/core';
|
|
2
|
-
import { Config } from '@verdaccio/types';
|
|
3
|
-
|
|
4
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A middleware that avoid a registry points itself as proxy and avoid create infinite loops.
|
|
8
|
-
* @param config
|
|
9
|
-
* @returns
|
|
10
|
-
*/
|
|
11
|
-
export function antiLoop(config: Config) {
|
|
12
|
-
return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
|
|
13
|
-
if (req?.headers?.via != null) {
|
|
14
|
-
const arr = req.get('via')?.split(',');
|
|
15
|
-
if (Array.isArray(arr)) {
|
|
16
|
-
for (let i = 0; i < arr.length; i++) {
|
|
17
|
-
// the "via" header must contain a specific value, this has to be in sync
|
|
18
|
-
// with the proxy request
|
|
19
|
-
// match eg: Server 1 or Server 2
|
|
20
|
-
|
|
21
|
-
// RFC 7230: Via = 1*( "," OWS Via-value )
|
|
22
|
-
// Via-value = received-protocol RWS received-by [ RWS comment ]
|
|
23
|
-
// received-protocol = [ protocol-name "/" ] protocol-version
|
|
24
|
-
// received-by = ( uri-host [ ":" port ] ) / pseudonym
|
|
25
|
-
|
|
26
|
-
// Split the trimmed header value into parts
|
|
27
|
-
const parts = arr[i].trim().split(/\s+/);
|
|
28
|
-
// Check if we have at least protocol/version and received-by parts
|
|
29
|
-
if (parts.length >= 2) {
|
|
30
|
-
// Get the received-by value (server id), removing any comment
|
|
31
|
-
const serverId = parts[1].split('(')[0].trim();
|
|
32
|
-
if (serverId === config.server_id) {
|
|
33
|
-
return next(errorUtils.getCode(HTTP_STATUS.LOOP_DETECTED, 'loop detected'));
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
next();
|
|
40
|
-
};
|
|
41
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
export enum USER_API_ENDPOINTS {
|
|
2
|
-
whoami = '/-/whoami',
|
|
3
|
-
get_user = '/-/user/:org_couchdb_user',
|
|
4
|
-
add_user = '/-/user/:org_couchdb_user/:_rev?/:revision?',
|
|
5
|
-
user_token = '/-/user/token/*',
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export enum STARS_API_ENDPOINTS {
|
|
9
|
-
get_user_starred_packages = '/-/_view/starredByUser',
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export enum SEARCH_API_ENDPOINTS {
|
|
13
|
-
search = '/-/v1/search',
|
|
14
|
-
deprecated_search = '/-/all(/since)?',
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export enum PUBLISH_API_ENDPOINTS {
|
|
18
|
-
add_package = '/:package',
|
|
19
|
-
publish_package = '/:package/-rev/:revision',
|
|
20
|
-
remove_tarball = '/:package/-/:filename/-rev/:revision',
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export enum PING_API_ENDPOINTS {
|
|
24
|
-
ping = '/-/ping',
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export enum PACKAGE_API_ENDPOINTS {
|
|
28
|
-
get_package_by_version = '/:package/:version?',
|
|
29
|
-
get_package_tarball = '/:package/-/:filename',
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export enum DIST_TAGS_API_ENDPOINTS {
|
|
33
|
-
tagging = '/:package/:tag',
|
|
34
|
-
tagging_package = '/-/package/:package/dist-tags/:tag',
|
|
35
|
-
get_dist_tags = '/-/package/:package/dist-tags',
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export enum PROFILE_API_ENDPOINTS {
|
|
39
|
-
get_profile = '/-/npm/v1/user',
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export enum TOKEN_API_ENDPOINTS {
|
|
43
|
-
get_tokens = '/-/npm/v1/tokens',
|
|
44
|
-
delete_token = '/-/npm/v1/tokens/token/:tokenKey',
|
|
45
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import buildDebug from 'debug';
|
|
2
|
-
|
|
3
|
-
import { errorUtils } from '@verdaccio/core';
|
|
4
|
-
|
|
5
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
|
6
|
-
|
|
7
|
-
const debug = buildDebug('verdaccio:middleware:encode');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Encode / in a scoped package name to be matched as a single parameter in routes
|
|
11
|
-
* @param req
|
|
12
|
-
* @param res
|
|
13
|
-
* @param next
|
|
14
|
-
*/
|
|
15
|
-
export function encodeScopePackage(
|
|
16
|
-
req: $RequestExtend,
|
|
17
|
-
res: $ResponseExtend,
|
|
18
|
-
next: $NextFunctionVer
|
|
19
|
-
): void {
|
|
20
|
-
const original = req.url;
|
|
21
|
-
|
|
22
|
-
// Expect relative URLs i.e. should call makeURLrelative before this middleware
|
|
23
|
-
if (!req.url.startsWith('/')) {
|
|
24
|
-
return next(errorUtils.getBadRequest(`Invalid URL: ${req.url} (must be relative)`));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// If the @ sign is encoded, we need to decode it first
|
|
28
|
-
// e.g.: /%40org/pkg/1.2.3 -> /@org/pkg/1.2.3
|
|
29
|
-
// For scoped packages, encode the slash to make it a single path segment/parameter
|
|
30
|
-
// e.g.: /@org/pkg/1.2.3 -> /@org%2Fpkg/1.2.3, /@org%2Fpkg/1.2.3 -> /@org%2Fpkg/1.2.3
|
|
31
|
-
req.url = req.url.replace(/^\/%40/, '/@').replace(/^(\/@[^\/%]+)\/(?!$)/, '$1%2F');
|
|
32
|
-
|
|
33
|
-
if (original !== req.url) {
|
|
34
|
-
debug('encodeScopePackage: %o -> %o', original, req.url);
|
|
35
|
-
} else {
|
|
36
|
-
debug('encodeScopePackage: %o (unchanged)', original);
|
|
37
|
-
}
|
|
38
|
-
next();
|
|
39
|
-
}
|
package/src/middlewares/error.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import buildDebug from 'debug';
|
|
2
|
-
import { HttpError } from 'http-errors';
|
|
3
|
-
import _ from 'lodash';
|
|
4
|
-
|
|
5
|
-
import { API_ERROR, HTTP_STATUS, VerdaccioError } from '@verdaccio/core';
|
|
6
|
-
|
|
7
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
|
8
|
-
|
|
9
|
-
const debug = buildDebug('verdaccio:middleware:error');
|
|
10
|
-
|
|
11
|
-
export const handleError = (logger) =>
|
|
12
|
-
function handleError(
|
|
13
|
-
err: HttpError,
|
|
14
|
-
req: $RequestExtend,
|
|
15
|
-
res: $ResponseExtend,
|
|
16
|
-
next: $NextFunctionVer
|
|
17
|
-
) {
|
|
18
|
-
debug('error handler init');
|
|
19
|
-
if (_.isError(err)) {
|
|
20
|
-
debug('is native error');
|
|
21
|
-
if (err.code === 'ECONNABORT' && res.statusCode === HTTP_STATUS.NOT_MODIFIED) {
|
|
22
|
-
return next();
|
|
23
|
-
}
|
|
24
|
-
if (_.isFunction(res.locals.report_error) === false) {
|
|
25
|
-
debug('is locals error report ref');
|
|
26
|
-
// in case of very early error this middleware may not be loaded before error is generated
|
|
27
|
-
// fixing that
|
|
28
|
-
errorReportingMiddleware(logger)(req, res, _.noop);
|
|
29
|
-
}
|
|
30
|
-
debug('set locals error report ref');
|
|
31
|
-
res.locals.report_error(err);
|
|
32
|
-
} else {
|
|
33
|
-
// Fall to Middleware.final
|
|
34
|
-
debug('no error to report, jump next layer');
|
|
35
|
-
return next(err);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// Middleware
|
|
40
|
-
export const errorReportingMiddleware = (logger) =>
|
|
41
|
-
function errorReportingMiddleware(
|
|
42
|
-
req: $RequestExtend,
|
|
43
|
-
res: $ResponseExtend,
|
|
44
|
-
next: $NextFunctionVer
|
|
45
|
-
): void {
|
|
46
|
-
debug('error report middleware start');
|
|
47
|
-
res.locals.report_error =
|
|
48
|
-
res.locals.report_error ||
|
|
49
|
-
function (err: VerdaccioError): void {
|
|
50
|
-
if (err.status && err.status >= HTTP_STATUS.BAD_REQUEST && err.status < 600) {
|
|
51
|
-
debug('is error > 409 %o', err?.status);
|
|
52
|
-
if (_.isNil(res.headersSent) === false) {
|
|
53
|
-
debug('send status %o', err?.status);
|
|
54
|
-
res.status(err.status);
|
|
55
|
-
debug('next layer %o', err?.message);
|
|
56
|
-
next({ error: err.message || API_ERROR.UNKNOWN_ERROR });
|
|
57
|
-
}
|
|
58
|
-
} else {
|
|
59
|
-
debug('is error < 409 %o', err?.status);
|
|
60
|
-
logger.error({ err: err }, 'unexpected error: @{!err.message}\n@{err.stack}');
|
|
61
|
-
if (!res.status || !res.send) {
|
|
62
|
-
// TODO: decide which debug keep
|
|
63
|
-
logger.error('this is an error in express.js, please report this');
|
|
64
|
-
debug('this is an error in express.js, please report this, destroy response %o', err);
|
|
65
|
-
res.destroy();
|
|
66
|
-
} else if (!res.headersSent) {
|
|
67
|
-
debug('send internal error %o', err);
|
|
68
|
-
res.status(HTTP_STATUS.INTERNAL_ERROR);
|
|
69
|
-
next({ error: API_ERROR.INTERNAL_SERVER_ERROR });
|
|
70
|
-
} else {
|
|
71
|
-
// socket should be already closed
|
|
72
|
-
debug('this should not happen, otherwise report %o', err);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
debug('error report middleware end (skip next layer) next()');
|
|
78
|
-
next();
|
|
79
|
-
};
|
package/src/middlewares/final.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import buildDebug from 'debug';
|
|
2
|
-
import _ from 'lodash';
|
|
3
|
-
|
|
4
|
-
import { HEADERS, HTTP_STATUS, TOKEN_BASIC, TOKEN_BEARER } from '@verdaccio/core';
|
|
5
|
-
import { Manifest } from '@verdaccio/types';
|
|
6
|
-
import { stringToMD5 } from '@verdaccio/utils';
|
|
7
|
-
|
|
8
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend, MiddlewareError } from '../types';
|
|
9
|
-
|
|
10
|
-
export type FinalBody = Manifest | MiddlewareError | string;
|
|
11
|
-
|
|
12
|
-
const debug = buildDebug('verdaccio:middleware:final');
|
|
13
|
-
|
|
14
|
-
export function final(
|
|
15
|
-
body: FinalBody,
|
|
16
|
-
req: $RequestExtend,
|
|
17
|
-
res: $ResponseExtend,
|
|
18
|
-
// if we remove `next` breaks test
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
20
|
-
next: $NextFunctionVer
|
|
21
|
-
): void {
|
|
22
|
-
if (res.statusCode === HTTP_STATUS.UNAUTHORIZED && !res.getHeader(HEADERS.WWW_AUTH)) {
|
|
23
|
-
debug('set auth header support');
|
|
24
|
-
res.header(HEADERS.WWW_AUTH, `${TOKEN_BASIC}, ${TOKEN_BEARER}`);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
if (_.isString(body) || _.isObject(body)) {
|
|
29
|
-
if (!res.get(HEADERS.CONTENT_TYPE)) {
|
|
30
|
-
debug('set json type header support');
|
|
31
|
-
res.header(HEADERS.CONTENT_TYPE, HEADERS.JSON);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (typeof body === 'object' && _.isNil(body) === false) {
|
|
35
|
-
if (typeof (body as MiddlewareError).error === 'string') {
|
|
36
|
-
debug('set verdaccio_error method');
|
|
37
|
-
res.locals._verdaccio_error = (body as MiddlewareError).error;
|
|
38
|
-
}
|
|
39
|
-
body = JSON.stringify(body, undefined, ' ') + '\n';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// don't send etags with errors
|
|
43
|
-
if (
|
|
44
|
-
!res.statusCode ||
|
|
45
|
-
(res.statusCode >= HTTP_STATUS.OK && res.statusCode < HTTP_STATUS.MULTIPLE_CHOICES)
|
|
46
|
-
) {
|
|
47
|
-
const etag = stringToMD5(body as string);
|
|
48
|
-
debug('set etag header %s', etag);
|
|
49
|
-
res.header(HEADERS.ETAG, '"' + etag + '"');
|
|
50
|
-
}
|
|
51
|
-
} else {
|
|
52
|
-
debug('this line should never be visible, if does report');
|
|
53
|
-
// send(null), send(204), etc.
|
|
54
|
-
}
|
|
55
|
-
} catch (err: any) {
|
|
56
|
-
// if verdaccio sends headers first, and then calls res.send()
|
|
57
|
-
// as an error handler, we can't report error properly,
|
|
58
|
-
// and should just close socket
|
|
59
|
-
if (err.message.match(/set headers after they are sent/)) {
|
|
60
|
-
debug('set headers after they are sent');
|
|
61
|
-
if (_.isNil(res.socket) === false) {
|
|
62
|
-
debug('force destroy socket');
|
|
63
|
-
res.socket?.destroy();
|
|
64
|
-
}
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
throw err;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
res.send(body);
|
|
71
|
-
}
|
package/src/middlewares/json.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { errorUtils } from '@verdaccio/core';
|
|
2
|
-
import { isObject } from '@verdaccio/utils';
|
|
3
|
-
|
|
4
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
|
5
|
-
|
|
6
|
-
export function expectJson(
|
|
7
|
-
req: $RequestExtend,
|
|
8
|
-
res: $ResponseExtend,
|
|
9
|
-
next: $NextFunctionVer
|
|
10
|
-
): void {
|
|
11
|
-
if (!isObject(req.body)) {
|
|
12
|
-
return next(errorUtils.getBadRequest("can't parse incoming json"));
|
|
13
|
-
}
|
|
14
|
-
next();
|
|
15
|
-
}
|
package/src/middlewares/log.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
|
-
|
|
3
|
-
import { HEADERS } from '@verdaccio/core';
|
|
4
|
-
|
|
5
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
|
6
|
-
|
|
7
|
-
// FIXME: deprecated, moved to @verdaccio/dev-commons
|
|
8
|
-
export const LOG_STATUS_MESSAGE =
|
|
9
|
-
"@{status}, user: @{user}(@{remoteIP}), req: '@{request.method} @{request.url}'";
|
|
10
|
-
export const LOG_VERDACCIO_ERROR = `${LOG_STATUS_MESSAGE}, error: @{!error}`;
|
|
11
|
-
export const LOG_VERDACCIO_BYTES = `${LOG_STATUS_MESSAGE}, bytes: @{bytes.in}/@{bytes.out}`;
|
|
12
|
-
|
|
13
|
-
export const log = (logger) => {
|
|
14
|
-
return function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
|
|
15
|
-
// logger
|
|
16
|
-
req.log = logger.child({ sub: 'in' });
|
|
17
|
-
|
|
18
|
-
const _auth = req.headers.authorization;
|
|
19
|
-
if (_.isNil(_auth) === false) {
|
|
20
|
-
req.headers.authorization = '<Classified>';
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const _cookie = req.get('cookie');
|
|
24
|
-
if (_.isNil(_cookie) === false) {
|
|
25
|
-
req.headers.cookie = '<Classified>';
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
req.url = req.originalUrl;
|
|
29
|
-
req.log.info({ req: req, ip: req.ip }, "@{ip} requested '@{req.method} @{req.url}'");
|
|
30
|
-
req.originalUrl = req.url;
|
|
31
|
-
|
|
32
|
-
if (_.isNil(_auth) === false) {
|
|
33
|
-
req.headers.authorization = _auth;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (_.isNil(_cookie) === false) {
|
|
37
|
-
req.headers.cookie = _cookie;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
let bytesin = 0;
|
|
41
|
-
req.on('data', function (chunk): void {
|
|
42
|
-
bytesin += chunk.length;
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
let bytesout = 0;
|
|
46
|
-
const _write = res.write;
|
|
47
|
-
// FIXME: res.write should return boolean
|
|
48
|
-
// @ts-ignore
|
|
49
|
-
res.write = function (buf): boolean {
|
|
50
|
-
bytesout += buf.length;
|
|
51
|
-
/* eslint prefer-rest-params: "off" */
|
|
52
|
-
// @ts-ignore
|
|
53
|
-
_write.apply(res, arguments);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const log = function (): void {
|
|
57
|
-
const forwardedFor = req.get(HEADERS.FORWARDED_FOR);
|
|
58
|
-
const remoteAddress = req.connection.remoteAddress;
|
|
59
|
-
const remoteIP = forwardedFor ? `${forwardedFor} via ${remoteAddress}` : remoteAddress;
|
|
60
|
-
let message;
|
|
61
|
-
if (res.locals._verdaccio_error) {
|
|
62
|
-
message = LOG_VERDACCIO_ERROR;
|
|
63
|
-
} else {
|
|
64
|
-
message = LOG_VERDACCIO_BYTES;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
req.url = req.originalUrl;
|
|
68
|
-
req.log.http(
|
|
69
|
-
{
|
|
70
|
-
request: {
|
|
71
|
-
method: req.method,
|
|
72
|
-
url: req.url,
|
|
73
|
-
},
|
|
74
|
-
user: req.remote_user?.name || null,
|
|
75
|
-
remoteIP,
|
|
76
|
-
status: res.statusCode,
|
|
77
|
-
error: res.locals._verdaccio_error,
|
|
78
|
-
bytes: {
|
|
79
|
-
in: bytesin,
|
|
80
|
-
out: bytesout,
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
message
|
|
84
|
-
);
|
|
85
|
-
req.originalUrl = req.url;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
req.on('close', function (): void {
|
|
89
|
-
log();
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
const _end = res.end;
|
|
93
|
-
// @ts-ignore
|
|
94
|
-
res.end = function (buf): void {
|
|
95
|
-
if (buf) {
|
|
96
|
-
bytesout += buf.length;
|
|
97
|
-
}
|
|
98
|
-
/* eslint prefer-rest-params: "off" */
|
|
99
|
-
// @ts-ignore
|
|
100
|
-
_end.apply(res, arguments);
|
|
101
|
-
log();
|
|
102
|
-
};
|
|
103
|
-
next();
|
|
104
|
-
};
|
|
105
|
-
};
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import buildDebug from 'debug';
|
|
2
|
-
import { URL } from 'node:url';
|
|
3
|
-
|
|
4
|
-
import { errorUtils } from '@verdaccio/core';
|
|
5
|
-
|
|
6
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
|
7
|
-
|
|
8
|
-
const debug = buildDebug('verdaccio:middleware:make-url-relative');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Removes the host from the URL and turns it into a relative URL.
|
|
12
|
-
* @param req
|
|
13
|
-
* @param res
|
|
14
|
-
* @param next
|
|
15
|
-
*/
|
|
16
|
-
export function makeURLrelative(
|
|
17
|
-
req: $RequestExtend,
|
|
18
|
-
res: $ResponseExtend,
|
|
19
|
-
next: $NextFunctionVer
|
|
20
|
-
): void {
|
|
21
|
-
const original = req.url;
|
|
22
|
-
|
|
23
|
-
// npm requests can contain the full URL, including the hostname, for example:
|
|
24
|
-
// tarball downloads. Removing the hostname makes the URL relative and allows
|
|
25
|
-
// the application to handle requests in a more consistent way.
|
|
26
|
-
|
|
27
|
-
let url;
|
|
28
|
-
try {
|
|
29
|
-
// In productive use, the URL is absolute (and base will be ignored)
|
|
30
|
-
// In tests, the URL might brelative (and base will be used)
|
|
31
|
-
// https://nodejs.org/docs/latest/api/url.html#new-urlinput-base
|
|
32
|
-
url = new URL(req.url, `${req.protocol}://${req.headers.host}/`);
|
|
33
|
-
} catch (error) {
|
|
34
|
-
return next(errorUtils.getBadRequest(`Invalid URL: ${req.url} (${error})`));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Rebuild the URL without hostname
|
|
38
|
-
req.url = url.pathname + url.search + url.hash;
|
|
39
|
-
|
|
40
|
-
if (original !== req.url) {
|
|
41
|
-
debug('makeURLrelative: %o -> %o', original, req.url);
|
|
42
|
-
} else {
|
|
43
|
-
debug('makeURLrelative: %o (unchanged)', original);
|
|
44
|
-
}
|
|
45
|
-
next();
|
|
46
|
-
}
|
package/src/middlewares/match.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
|
2
|
-
|
|
3
|
-
export function match(regexp: RegExp): any {
|
|
4
|
-
return function (
|
|
5
|
-
req: $RequestExtend,
|
|
6
|
-
res: $ResponseExtend,
|
|
7
|
-
next: $NextFunctionVer,
|
|
8
|
-
value: string
|
|
9
|
-
): void {
|
|
10
|
-
if (regexp.exec(value)) {
|
|
11
|
-
next();
|
|
12
|
-
} else {
|
|
13
|
-
next('route');
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
}
|
package/src/middlewares/media.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { HEADER_TYPE, HTTP_STATUS, errorUtils } from '@verdaccio/core';
|
|
2
|
-
|
|
3
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
|
4
|
-
|
|
5
|
-
export function media(expect: string | null): any {
|
|
6
|
-
return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
|
|
7
|
-
const header = req.headers[HEADER_TYPE.CONTENT_TYPE];
|
|
8
|
-
if (!header) {
|
|
9
|
-
next(
|
|
10
|
-
errorUtils.getCode(
|
|
11
|
-
HTTP_STATUS.UNSUPPORTED_MEDIA,
|
|
12
|
-
'content-type is missing, expect: ' + expect
|
|
13
|
-
)
|
|
14
|
-
);
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (typeof header !== 'string' || header.split(';')[0].trim() !== expect) {
|
|
19
|
-
next(
|
|
20
|
-
errorUtils.getCode(
|
|
21
|
-
HTTP_STATUS.UNSUPPORTED_MEDIA,
|
|
22
|
-
'wrong content-type, expect: ' + expect + ', got: ' + req.get[HEADER_TYPE.CONTENT_TYPE]
|
|
23
|
-
)
|
|
24
|
-
);
|
|
25
|
-
} else {
|
|
26
|
-
next();
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { HEADERS } from '@verdaccio/core';
|
|
2
|
-
|
|
3
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
|
4
|
-
|
|
5
|
-
// TODO: remove, was relocated to web package
|
|
6
|
-
// @ts-deprecated
|
|
7
|
-
export function setSecurityWebHeaders(
|
|
8
|
-
req: $RequestExtend,
|
|
9
|
-
res: $ResponseExtend,
|
|
10
|
-
next: $NextFunctionVer
|
|
11
|
-
): void {
|
|
12
|
-
// disable loading in frames (clickjacking, etc.)
|
|
13
|
-
res.header(HEADERS.FRAMES_OPTIONS, 'deny');
|
|
14
|
-
// avoid stablish connections outside of domain
|
|
15
|
-
res.header(HEADERS.CSP, "connect-src 'self'");
|
|
16
|
-
// https://stackoverflow.com/questions/18337630/what-is-x-content-type-options-nosniff
|
|
17
|
-
res.header(HEADERS.CTO, 'nosniff');
|
|
18
|
-
// https://stackoverflow.com/questions/9090577/what-is-the-http-header-x-xss-protection
|
|
19
|
-
res.header(HEADERS.XSS, '1; mode=block');
|
|
20
|
-
next();
|
|
21
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { getUserAgent } from '@verdaccio/config';
|
|
2
|
-
import { HEADERS } from '@verdaccio/core';
|
|
3
|
-
|
|
4
|
-
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
|
|
5
|
-
|
|
6
|
-
export function userAgent(config) {
|
|
7
|
-
return function (_req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
|
|
8
|
-
res.setHeader(HEADERS.POWERED_BY, getUserAgent(config?.user_agent));
|
|
9
|
-
next();
|
|
10
|
-
};
|
|
11
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { NextFunction, Request, Response } from 'express';
|
|
2
|
-
|
|
3
|
-
import { errorUtils, validationUtils } from '@verdaccio/core';
|
|
4
|
-
|
|
5
|
-
export function validateName(
|
|
6
|
-
_req: Request,
|
|
7
|
-
_res: Response,
|
|
8
|
-
next: NextFunction,
|
|
9
|
-
value: string,
|
|
10
|
-
name: string
|
|
11
|
-
) {
|
|
12
|
-
if (validationUtils.validateName(value)) {
|
|
13
|
-
next();
|
|
14
|
-
} else {
|
|
15
|
-
next(errorUtils.getBadRequest('invalid ' + name));
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function validatePackage(
|
|
20
|
-
_req: Request,
|
|
21
|
-
_res,
|
|
22
|
-
next: NextFunction,
|
|
23
|
-
value: string,
|
|
24
|
-
name: string
|
|
25
|
-
) {
|
|
26
|
-
if (validationUtils.validatePackage(value)) {
|
|
27
|
-
next();
|
|
28
|
-
} else {
|
|
29
|
-
next(errorUtils.getBadRequest('invalid ' + name));
|
|
30
|
-
}
|
|
31
|
-
}
|