Skip to content

Commit

Permalink
🐛 fix issue concerning bad cors (#38)
Browse files Browse the repository at this point in the history
* Fix cors using webpack cli
  • Loading branch information
heavenboy8 committed Oct 5, 2022
1 parent f9c61ee commit 53aedf5
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 64 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

All notable changes to this project will be documented in this file.

---
## [3.0.1] - 2022-10-05
### Added
- Add MIT license
### Changed
- Make ABE/CC decryption workers exportable (converting worker.ts to JS and allow JS to be compiled)
- Uniformize encryption demos functions using `encryptUsersPerCountryAndDepartment`
### Fixed
- Fix cors problem by specifying port and public host in webpack cli
### Removed

---
## [3.0.0] - 2022-09-30
### Added
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ RUN npx webpack build
EXPOSE 8080

# Start the app
CMD ["npx", "webpack", "serve", "--host", "0.0.0.0", "--disable-host-check"]
CMD ["npx", "webpack", "serve", "--host", "0.0.0.0", "--disable-host-check", "--port", "8080","--public", "demo-cloudproof.cosmian.com"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Cosmian Tech SAS

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions bump_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

#
# Example:
#
# bash bump_version.sh 3.0.1 3.0.2

set -exEu

OLD_VERSION=$1
NEW_VERSION=$2
DATE=$(date +%F)

# Update CHANGELOG.md
sed -i "5 i ---" CHANGELOG.md
sed -i "6 i ## [$NEW_VERSION] - $DATE" CHANGELOG.md
sed -i "7 i ### Added" CHANGELOG.md
sed -i "8 i ### Changed" CHANGELOG.md
sed -i "9 i ### Fixed" CHANGELOG.md
sed -i "10 i ### Removed" CHANGELOG.md
sed -i "11 i \\\\" CHANGELOG.md

# Update version where it needs to be
sed -i "s/\"version\": \"$OLD_VERSION\"/\"version\": \"$NEW_VERSION\"/" package.json
sed -i "s/$OLD_VERSION/$NEW_VERSION/" site/index.html
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cosmian_js_lib",
"version": "3.0.0",
"version": "3.0.1",
"license": "MIT",
"description": "Cosmian javascript client library",
"main": "dist/cjs/index.js",
Expand Down
2 changes: 1 addition & 1 deletion site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<base href="/" />
<title>Cosmian JS Lib v3.0.0</title>
<title>Cosmian JS Lib v3.0.1</title>
<style>
body {
display: flex;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import {
DecryptionWorkerMessage,
HybridDecryption,
} from "crypto/abe/interfaces/decryption";
import { logger } from "utils/logger";
import { hexDecode } from "utils/utils";
import { ClearTextHeader } from "crypto/abe/interfaces/cleartext_header";
import { CoverCryptHybridDecryption } from "../cover_crypt/decryption";
import { GpswHybridDecryption } from "../gpsw/decryption";

const ctx: Worker = self as any;
const ctx = self;

class DecryptWorker {
hybridDecryption: HybridDecryption | null = null;
hybridDecryption = null;

init(asymmetricDecryptionKey: string, isGpswImplementation: boolean) {
init(asymmetricDecryptionKey, isGpswImplementation) {
if (isGpswImplementation) {
this.hybridDecryption = new GpswHybridDecryption(
hexDecode(asymmetricDecryptionKey)
Expand All @@ -28,21 +23,21 @@ class DecryptWorker {
/**
* Destroy the hybrid decryption crypto
*/
destroy(): void {
destroy() {
if (this.hybridDecryption == null) {
return;
}
this.hybridDecryption.destroyInstance();
}

decrypt(encryptedEntries: Array<{ ciphertextHex: string }>): Uint8Array[] {
decrypt(encryptedEntries) {
if (this.hybridDecryption === null) {
// TODO handle hybrid crypto not initialized here if needed
throw new Error("The hybrid decryption scheme is not initialized");
}
const dec: HybridDecryption = this.hybridDecryption;
const dec = this.hybridDecryption;

const cleartextValues: Uint8Array[] = [];
const cleartextValues = [];
for (let index = 0; index < encryptedEntries.length; index++) {
const { ciphertextHex } = encryptedEntries[index];

Expand All @@ -58,7 +53,7 @@ class DecryptWorker {
);

// HEADER decryption: asymmetric decryption
let cleartextHeader: ClearTextHeader;
let cleartextHeader;
try {
cleartextHeader = dec.decryptHybridHeader(asymmetricHeader);
} catch (error) {
Expand All @@ -67,7 +62,7 @@ class DecryptWorker {
}

// AES_DATA: AES Symmetric part decryption
let cleartext: Uint8Array;
let cleartext;
try {
cleartext = dec.decryptHybridBlock(
cleartextHeader.symmetricKey,
Expand All @@ -89,26 +84,26 @@ class DecryptWorker {
const decrypter = new DecryptWorker();

ctx.onmessage = (event) => {
const msg = event.data as DecryptionWorkerMessage;
const msg = event.data;
const msgName = msg.name;
const input = msg.value;
const isGpswImplementation = msg.isGpswImplementation;

if (msgName === "INIT") {
decrypter.init(input as string, isGpswImplementation);
decrypter.init(input, isGpswImplementation);
logger.log(() => "worker cache initialized");
ctx.postMessage({
name: "INIT",
value: "SUCCESS",
});
} else if (msgName == "DECRYPT") {
} else if (msgName === "DECRYPT") {
logger.log(() => "worker decrypting");
ctx.postMessage({
name: "DECRYPT",
value: decrypter.decrypt(input),
});
logger.log(() => "... done decrypting");
} else if (msgName == "DESTROY") {
} else if (msgName === "DESTROY") {
ctx.postMessage({
name: "DESTROY",
value: "SUCCESS",
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/abe/core/hybrid_crypto/worker/worker_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class WorkerPool {
this.workers = [];
for (let index = 0; index < numWorkers; index++) {
// !! webpack wants this on a single line; do not split
const worker = new Worker(new URL("./worker.ts", import.meta.url));
const worker = new Worker(new URL("./worker.js", import.meta.url));
this.workers.push(worker);
}
}
Expand Down
26 changes: 14 additions & 12 deletions src/site/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ async function upsert(location: string): Promise<void> {
}

try {
await FINDEX_DEMO.upsertUsersIndexes(FINDEX_MSK, LABEL, USERS, location, true);
await FINDEX_DEMO.upsertUsersIndexes(
FINDEX_MSK,
LABEL,
USERS,
location,
true
);
if (button) {
button.innerHTML = "Indexes created !";
button.style.backgroundColor = "#4CAF50";
Expand Down Expand Up @@ -491,7 +497,7 @@ const wnElt = document.getElementById("workers_number");
if (wnElt == null) {
// console.error("workers_number not found")
} else {
wnElt.innerHTML = NUM_WORKERS + "";
wnElt.innerHTML = `${NUM_WORKERS}`;
}

// The function called whn clicking the decrypt button
Expand All @@ -514,7 +520,7 @@ const decryptUsingWorker = (): void => {
isGpsw = false;
}

logger.log(() => "isGpsw: " + isGpsw);
logger.log(() => `isGpsw: ${isGpsw.toString()}`);

for (let index = 0; index < NUM_ENTRIES; index++) {
encryptedEntries.push({
Expand Down Expand Up @@ -550,7 +556,7 @@ const displayResults = (
startDate: number,
results: Uint8Array[],
encryptedEntriesLength: number
) => {
): void => {
// got results - stope time measurement
const endDate = new Date().getTime();
const milliseconds = (endDate - startDate) / encryptedEntriesLength;
Expand All @@ -561,13 +567,9 @@ const displayResults = (
console.error("workers_results_number not found");
return;
}
wrnElt.innerHTML =
results.length +
" in " +
(endDate - startDate) +
"ms i.e. " +
milliseconds +
"ms/record average";
wrnElt.innerHTML = `${results.length} in ${
endDate - startDate
}ms i.e. ${milliseconds}ms/record average`;

// the results themselves
const wrElt = document.getElementById("workers_result");
Expand All @@ -582,7 +584,7 @@ const displayResults = (
};

// display the decryption errors
const displayError = (err: string) => {
const displayError = (err: string): void => {
const wnElement = document.getElementById("workers_number");
if (wnElement == null) {
console.error("workers_number not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class CloudProofDemoRedis extends FindexDemo {
}

/// Construct the encrypted users DB
async encryptUsers(
async encryptUsersPerCountryAndDepartment(
users: Users,
metadataUid: Uint8Array,
policy: Policy,
Expand Down
10 changes: 9 additions & 1 deletion tests/crypto/sse/findex/implementations/redis/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export class RedisDB implements DBInterface {

constructor(localhost: string, port: number) {
this.instance = createClient({
url: `redis://localhost:${port}`,
socket: {
host: localhost,
port,
},
});

this.instance.on("error", (err: string) =>
Expand Down Expand Up @@ -178,6 +181,11 @@ export class RedisDB implements DBInterface {
return await this.getAllIndexes(2);
}

async getFirstEncryptedUsers(): Promise<Uint8Array[]> {
const allEncryptedUsers = await this.getAllIndexes(3);
return allEncryptedUsers.slice(0, 4);
}

async getEncryptedUsers(): Promise<Uint8Array[]> {
return await this.getAllIndexes(3);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test("upsert+search", async () => {
const findexDemo = new CloudProofDemoRedis(redisDb);
await findexDemo.redisDb.instance.flushAll();
await findexDemo.redisDb.deleteAllEncryptedUsers();
users = await findexDemo.encryptUsers(
users = await findexDemo.encryptUsersPerCountryAndDepartment(
users,
hexDecode("00000001"),
keys.abePolicy,
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dom.iterable",
"es2020",
], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
"allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
Expand Down Expand Up @@ -75,7 +75,7 @@
},
"include": [
"src",
"tests", "tests", "tests", "tests",
"tests",
],
"exclude": [
"node_modules",
Expand Down
Loading

0 comments on commit 53aedf5

Please sign in to comment.