bump libs (#211)

This commit is contained in:
Simon J
2024-11-30 21:30:30 +11:00
committed by GitHub
parent 0488f398c1
commit 996582ce93
5 changed files with 2025 additions and 848 deletions

2744
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -6,53 +6,53 @@
"author": "simojenki <simojenki@users.noreply.github.com>", "author": "simojenki <simojenki@users.noreply.github.com>",
"license": "GPL-3.0-only", "license": "GPL-3.0-only",
"dependencies": { "dependencies": {
"@svrooij/sonos": "^2.6.0-beta.7", "@svrooij/sonos": "^2.6.0-beta.11",
"@types/express": "^4.17.21", "@types/express": "^4.17.21",
"@types/fs-extra": "^11.0.4", "@types/fs-extra": "^11.0.4",
"@types/jsonwebtoken": "^9.0.5", "@types/jsonwebtoken": "^9.0.7",
"@types/jws": "^3.2.9", "@types/jws": "^3.2.10",
"@types/morgan": "^1.9.9", "@types/morgan": "^1.9.9",
"@types/node": "^20.11.5", "@types/node": "^20.11.5",
"@types/randomstring": "^1.1.11", "@types/randomstring": "^1.3.0",
"@types/underscore": "^1.11.15", "@types/underscore": "^1.13.0",
"@types/uuid": "^9.0.7", "@types/uuid": "^10.0.0",
"@types/xmldom": "0.1.34", "@types/xmldom": "0.1.34",
"axios": "^1.6.5", "axios": "^1.7.8",
"dayjs": "^1.11.10", "dayjs": "^1.11.13",
"eta": "^2.2.0", "eta": "^2.2.0",
"express": "^4.18.2", "express": "^4.18.3",
"fp-ts": "^2.16.2", "fp-ts": "^2.16.9",
"fs-extra": "^11.2.0", "fs-extra": "^11.2.0",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
"jws": "^4.0.0", "jws": "^4.0.0",
"libxmljs2": "^0.33.0", "libxmljs2": "^0.35.0",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"node-html-parser": "^6.1.12", "node-html-parser": "^6.1.13",
"randomstring": "^1.3.0", "randomstring": "^1.3.0",
"sharp": "^0.33.2", "sharp": "^0.33.5",
"soap": "^1.0.0", "soap": "^1.1.6",
"ts-md5": "^1.3.1", "ts-md5": "^1.3.1",
"typescript": "^5.3.3", "typescript": "^5.7.2",
"underscore": "^1.13.6", "underscore": "^1.13.7",
"urn-lib": "^2.0.0", "urn-lib": "^2.0.0",
"uuid": "^9.0.1", "uuid": "^11.0.3",
"winston": "^3.11.0", "winston": "^3.17.0",
"xmldom-ts": "^0.3.1" "xmldom-ts": "^0.3.1"
}, },
"devDependencies": { "devDependencies": {
"@types/chai": "^4.3.11", "@types/chai": "^5.0.1",
"@types/jest": "^29.5.11", "@types/jest": "^29.5.14",
"@types/mocha": "^10.0.6", "@types/mocha": "^10.0.10",
"@types/supertest": "^6.0.2", "@types/supertest": "^6.0.2",
"@types/tmp": "^0.2.6", "@types/tmp": "^0.2.6",
"chai": "^5.0.0", "chai": "^5.1.2",
"get-port": "^7.0.0", "get-port": "^7.1.0",
"image-js": "^0.35.5", "image-js": "^0.35.6",
"jest": "^29.7.0", "jest": "^29.7.0",
"nodemon": "^3.0.3", "nodemon": "^3.1.7",
"supertest": "^6.3.4", "supertest": "^7.0.0",
"tmp": "^0.2.1", "tmp": "^0.2.3",
"ts-jest": "^29.1.2", "ts-jest": "^29.2.5",
"ts-mockito": "^2.6.1", "ts-mockito": "^2.6.1",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"xmldom-ts": "^0.3.1", "xmldom-ts": "^0.3.1",

View File

@@ -1,6 +1,8 @@
import _ from "underscore"; import _ from "underscore";
import { createUrnUtil } from "urn-lib"; import { createUrnUtil } from "urn-lib";
import randomstring from "randomstring"; import randomstring from "randomstring";
import { pipe } from "fp-ts/lib/function";
import { either as E } from "fp-ts";
import jwsEncryption from "./encryption"; import jwsEncryption from "./encryption";
@@ -78,7 +80,13 @@ export const parse = (burn: string): BUrn => {
resource: result.resource as string, resource: result.resource as string,
}; };
if(x.system == "encrypted") { if(x.system == "encrypted") {
return parse(encryptor.decrypt(x.resource)); return pipe(
encryptor.decrypt(x.resource),
E.match(
(err) => { throw new Error(err) },
(z) => parse(z)
)
);
} else { } else {
return x; return x;
} }

View File

@@ -4,13 +4,14 @@ import {
randomBytes, randomBytes,
createHash, createHash,
} from "crypto"; } from "crypto";
import { option as O, either as E } from "fp-ts";
import { Either, left, right } from 'fp-ts/Either'
import { pipe } from "fp-ts/lib/function";
import jws from "jws"; import jws from "jws";
const ALGORITHM = "aes-256-cbc"; const ALGORITHM = "aes-256-cbc";
const IV = randomBytes(16); const IV = randomBytes(16);
export type Hash = { export type Hash = {
iv: string; iv: string;
encryptedData: string; encryptedData: string;
@@ -18,7 +19,7 @@ export type Hash = {
export type Encryption = { export type Encryption = {
encrypt: (value: string) => string; encrypt: (value: string) => string;
decrypt: (value: string) => string; decrypt: (value: string) => Either<string, string>;
}; };
export const jwsEncryption = (secret: string): Encryption => { export const jwsEncryption = (secret: string): Encryption => {
@@ -28,7 +29,15 @@ export const jwsEncryption = (secret: string): Encryption => {
payload: value, payload: value,
secret: secret, secret: secret,
}), }),
decrypt: (value: string) => jws.decode(value).payload decrypt: (value: string) => pipe(
jws.decode(value),
O.fromNullable,
O.map(it => it.payload),
O.match(
() => left("Failed to decrypt jws"),
(payload) => right(payload)
)
)
} }
} }
@@ -36,7 +45,8 @@ export const cryptoEncryption = (secret: string): Encryption => {
const key = createHash("sha256") const key = createHash("sha256")
.update(String(secret)) .update(String(secret))
.digest("base64") .digest("base64")
.substr(0, 32); .substring(0, 32);
return { return {
encrypt: (value: string) => { encrypt: (value: string) => {
const cipher = createCipheriv(ALGORITHM, key, IV); const cipher = createCipheriv(ALGORITHM, key, IV);
@@ -45,20 +55,23 @@ export const cryptoEncryption = (secret: string): Encryption => {
cipher.final(), cipher.final(),
]).toString("hex")}`; ]).toString("hex")}`;
}, },
decrypt: (value: string) => { decrypt: (value: string) => pipe(
const parts = value.split("."); right(value),
if(parts.length != 2) throw `Invalid value to decrypt`; E.map(it => it.split(".")),
E.flatMap(it => it.length == 2 ? right({ iv: it[0]!, data: it[1]! }) : left("Invalid value to decrypt")),
const decipher = createDecipheriv( E.map(it => ({
hash: it,
decipher: createDecipheriv(
ALGORITHM, ALGORITHM,
key, key,
Buffer.from(parts[0]!, "hex") Buffer.from(it.iv, "hex")
); )
return Buffer.concat([ })),
decipher.update(Buffer.from(parts[1]!, "hex")), E.map(it => Buffer.concat([
decipher.final(), it.decipher.update(Buffer.from(it.hash.data, "hex")),
]).toString(); it.decipher.final(),
}, ]).toString())
),
}; };
}; };

View File

@@ -1,3 +1,5 @@
import { left, right } from 'fp-ts/Either'
import { cryptoEncryption, jwsEncryption } from '../src/encryption'; import { cryptoEncryption, jwsEncryption } from '../src/encryption';
describe("jwsEncryption", () => { describe("jwsEncryption", () => {
@@ -7,7 +9,7 @@ describe("jwsEncryption", () => {
const value = "bobs your uncle" const value = "bobs your uncle"
const hash = e.encrypt(value) const hash = e.encrypt(value)
expect(hash).not.toContain(value); expect(hash).not.toContain(value);
expect(e.decrypt(hash)).toEqual(value); expect(e.decrypt(hash)).toEqual(right(value));
}); });
it("returns different values for different secrets", () => { it("returns different values for different secrets", () => {
@@ -29,7 +31,7 @@ describe("cryptoEncryption", () => {
const value = "bobs your uncle" const value = "bobs your uncle"
const hash = e.encrypt(value) const hash = e.encrypt(value)
expect(hash).not.toContain(value); expect(hash).not.toContain(value);
expect(e.decrypt(hash)).toEqual(value); expect(e.decrypt(hash)).toEqual(right(value));
}); });
it("returns different values for different secrets", () => { it("returns different values for different secrets", () => {
@@ -42,4 +44,10 @@ describe("cryptoEncryption", () => {
expect(h1).not.toEqual(h2); expect(h1).not.toEqual(h2);
}); });
it("should return left on invalid value", () => {
const e = cryptoEncryption("secret squirrel");
expect(e.decrypt("not-valid")).toEqual(left("Invalid value to decrypt"));
});
}) })