Skip to content

Commit

Permalink
feat: add migrateToSecureLegacySignature property (#4621)
Browse files Browse the repository at this point in the history
* feat: add migrateToSecureLegacySignature property

* Update config.ts

* changeset

* Update ci.yml

* Update config.spec.ts
  • Loading branch information
juanpicado committed May 5, 2024
1 parent 7400830 commit bd8703e
Show file tree
Hide file tree
Showing 28 changed files with 440 additions and 391 deletions.
10 changes: 10 additions & 0 deletions .changeset/wet-balloons-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@verdaccio/types': minor
'@verdaccio/core': minor
'@verdaccio/signature': minor
'@verdaccio/node-api': minor
'@verdaccio/config': minor
'@verdaccio/auth': minor
---

feat: add migrateToSecureLegacySignature and remove enhancedLegacySignature property
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
node_version: [18, 20, 21]
node_version: [18, 20, 21, 22]
name: ${{ matrix.os }} / Node ${{ matrix.node_version }}
runs-on: ${{ matrix.os }}
steps:
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ name: Verdaccio Website CI

on:
workflow_dispatch:
schedule:
- cron: '0 0 * * *'

permissions:
contents: read # to fetch code (actions/checkout)
Expand Down Expand Up @@ -69,6 +67,7 @@ jobs:
CONTEXT: production
run: pnpm --filter @verdaccio/website netlify:build
- name: Deploy to Netlify
if: (github.event_name == 'push' && github.ref == 'refs/heads/master') || github.event_name == 'workflow_dispatch'
env:
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
Expand Down
7 changes: 4 additions & 3 deletions docs/env.variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ internal features.

#### VERDACCIO_LEGACY_ALGORITHM

Allows to define the specific algorithm for the token
signature which by default is `aes-256-ctr`
Allows to define the specific algorithm for the token signature which by default is `aes-256-ctr`. The algorithm must be supported by `crypto.createCipheriv` and `crypto.createDecipheriv`.
Read more here: https://nodejs.org/api/crypto.html#crypto_crypto_createcipheriv_algorithm_key_iv_options

#### VERDACCIO_LEGACY_ENCRYPTION_KEY

By default, the token stores in the database, but using this variable allows to get it from memory
By default, the token stores in the database, but using this variable allows to get it from memory, the length must be 32 characters otherwise will throw an error.
Read more here: https://nodejs.org/api/crypto.html#crypto_crypto_createcipheriv_algorithm_key_iv_options

#### VERDACCIO_PUBLIC_URL

Expand Down
18 changes: 6 additions & 12 deletions packages/auth/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import {
pluginUtils,
warningUtils,
} from '@verdaccio/core';
import '@verdaccio/core';
import { asyncLoadPlugin } from '@verdaccio/loaders';
import { logger } from '@verdaccio/logger';
import {
aesEncrypt,
aesEncryptDeprecated,
parseBasicPayload,
signPayload,
utils as signatureUtils,
} from '@verdaccio/signature';
import {
AllowAccess,
Expand Down Expand Up @@ -481,14 +481,9 @@ class Auth implements IAuthMiddleware, TokenEncryption, pluginUtils.IBasicAuth {
next: Function
): void {
debug('handle legacy api middleware');
debug('api middleware secret %o', typeof secret === 'string');
debug('api middleware has a secret? %o', typeof secret === 'string');
debug('api middleware authorization %o', typeof authorization === 'string');
const credentials: any = getMiddlewareCredentials(
security,
secret,
authorization,
this.config?.getEnhancedLegacySignature()
);
const credentials: any = getMiddlewareCredentials(security, secret, authorization);
debug('api middleware credentials %o', credentials?.name);
if (credentials) {
const { user, password } = credentials;
Expand Down Expand Up @@ -588,13 +583,12 @@ class Auth implements IAuthMiddleware, TokenEncryption, pluginUtils.IBasicAuth {
* Encrypt a string.
*/
public aesEncrypt(value: string): string | void {
// enhancedLegacySignature enables modern aes192 algorithm signature
if (this.config?.getEnhancedLegacySignature()) {
debug('signing with enhaced aes legacy');
if (this.secret.length === signatureUtils.TOKEN_VALID_LENGTH) {
debug('signing with enhanced aes legacy');
const token = aesEncrypt(value, this.secret);
return token;
} else {
debug('signing with enhaced aes deprecated legacy');
debug('signing with enhanced aes deprecated legacy');
// deprecated aes (legacy) signature, only must be used for legacy version
const token = aesEncryptDeprecated(Buffer.from(value), this.secret).toString('base64');
return token;
Expand Down
66 changes: 0 additions & 66 deletions packages/auth/src/signature.ts

This file was deleted.

32 changes: 15 additions & 17 deletions packages/auth/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ export function parseAuthTokenHeader(authorizationHeader: string): AuthTokenHead
return { scheme, token };
}

export function parseAESCredentials(
authorizationHeader: string,
secret: string,
enhanced: boolean
) {
debug('parseAESCredentials');
export function parseAESCredentials(authorizationHeader: string, secret: string) {
debug('parseAESCredentials init');
const { scheme, token } = parseAuthTokenHeader(authorizationHeader);

// basic is deprecated and should not be enforced
Expand All @@ -57,27 +53,29 @@ export function parseAESCredentials(
return credentials;
} else if (scheme.toUpperCase() === TOKEN_BEARER.toUpperCase()) {
debug('legacy header bearer');
debug('legacy header enhanced?', enhanced);
const credentials = enhanced
? aesDecrypt(token.toString(), secret)
: // FUTURE: once deprecated legacy is removed this logic won't be longer need it
aesDecryptDeprecated(convertPayloadToBase64(token), secret).toString('utf-8');

return credentials;
debug('secret length %o', secret.length);
const isLegacyUnsecure = secret.length > 32;
debug('is legacy unsecure %o', isLegacyUnsecure);
if (isLegacyUnsecure) {
debug('legacy unsecure enabled');
return aesDecryptDeprecated(convertPayloadToBase64(token), secret).toString('utf-8');
} else {
debug('legacy secure enabled');
return aesDecrypt(token.toString(), secret);
}
}
}

export function getMiddlewareCredentials(
security: Security,
secretKey: string,
authorizationHeader: string,
enhanced: boolean = true
authorizationHeader: string
): AuthMiddlewarePayload {
debug('getMiddlewareCredentials');
debug('getMiddlewareCredentials init');
// comment out for debugging purposes
if (isAESLegacy(security)) {
debug('is legacy');
const credentials = parseAESCredentials(authorizationHeader, secretKey, enhanced);
const credentials = parseAESCredentials(authorizationHeader, secretKey);
if (!credentials) {
debug('parse legacy credentials failed');
return;
Expand Down
19 changes: 6 additions & 13 deletions packages/auth/test/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,16 +601,14 @@ describe('AuthTest', () => {
});
});

describe('deprecated legacy handling forceEnhancedLegacySignature=false', () => {
describe('deprecated legacy handling', () => {
test('should handle valid auth token', async () => {
const payload = 'juan:password';
// const token = await signPayload(remoteUser, '12345');
const config: Config = new AppConfig(
{ ...authProfileConf },
{ forceEnhancedLegacySignature: false }
);
const config: Config = new AppConfig({ ...authProfileConf });
// intended to force key generator (associated with mocks above)
config.checkSecretKey(undefined);
// 64 characters secret long
config.checkSecretKey('35fabdd29b820d39125e76e6d85cc294');
const auth = new Auth(config);
await auth.init();
const token = auth.aesEncrypt(payload) as string;
Expand All @@ -624,10 +622,7 @@ describe('AuthTest', () => {

test('should handle invalid auth token', async () => {
const payload = 'juan:password';
const config: Config = new AppConfig(
{ ...authPluginFailureConf },
{ forceEnhancedLegacySignature: false }
);
const config: Config = new AppConfig({ ...authPluginFailureConf });
// intended to force key generator (associated with mocks above)
config.checkSecretKey(undefined);
const auth = new Auth(config);
Expand Down Expand Up @@ -691,16 +686,14 @@ describe('AuthTest', () => {
{
...authProfileConf,
...{ security: { api: { jwt: { sign: { expiresIn: '29d' } } } } },
},
{ forceEnhancedLegacySignature: false }
}
);
// intended to force key generator (associated with mocks above)
config.checkSecretKey(undefined);
const auth = new Auth(config);
await auth.init();
const token = (await auth.jwtEncrypt(
createRemoteUser('jwt_user', [ROLES.ALL]),
// @ts-expect-error
config.security.api.jwt.sign
)) as string;
const app = await getServer(auth);
Expand Down

0 comments on commit bd8703e

Please sign in to comment.