Skip to content

Commit

Permalink
makeCLists: change key encoding scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
warner committed May 16, 2019
1 parent 9a94e77 commit b7f311e
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/kernel/commsSlots/state/makeCLists.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
export function makeCLists() {
const state = new Map();

function checkIfAlreadyExists(incomingWireMessageObj, kernelToMeSlot) {
const slot = state.get(JSON.stringify(incomingWireMessageObj));
const outgoing = state.get(JSON.stringify(kernelToMeSlot));
function checkIfAlreadyExists(incomingWireMessageKey, kernelToMeKey) {
const slot = state.get(incomingWireMessageKey);
const outgoing = state.get(kernelToMeKey);
if (slot || outgoing) {
throw new Error(
`${JSON.stringify(kernelToMeSlot)} already exists in clist`,
);
throw new Error(`${kernelToMeKey} already exists in clist`);
}
}

Expand All @@ -42,11 +40,20 @@ export function makeCLists() {
};
}

function createIncomingWireMessageObj(otherMachineName, youToMeSlot) {
return {
otherMachineName,
youToMeSlot, // could be a your-ingress, your-egress
};
function createIncomingWireMessageKey(otherMachineName, youToMeSlot) {
// TODO: we need an encoding scheme that is both stable and
// collision-free under adversarial attack. Any encoding scheme that can
// be decoded unambiguously is sufficiently collision-free. This first
// used JSON.stringify, which is collision-free, but its stability
// depends upon the order in which the object keys were added. "djson" (a
// library that provides deterministic JSON encoding) would be stable,
// but importing it into a SES environment sounds like an adventure. For
// now, we use a cheap concatenation that is stable but not
// collision-free. However, 'otherMachineName' will generally be a public
// key, which won't have hyphens, so the attacker is not likely to be
// able to force a collision with other machines, which is the only kind
// of collision that could get them unintended access. See #45 for more.
return `incoming-${otherMachineName}-${youToMeSlot.type}-${youToMeSlot.id}`;
}

function createOutgoingWireMessageObj(otherMachineName, meToYouSlot) {
Expand All @@ -56,19 +63,23 @@ export function makeCLists() {
};
}

function createKernelToMeKey(kernelToMeSlot) {
return `kernel-${kernelToMeSlot.type}-${kernelToMeSlot.id}`;
}

// takes youToMeSlot, returns kernelToMeSlot
function mapIncomingWireMessageToKernelSlot(otherMachineName, youToMeSlot) {
return state.get(
JSON.stringify(
createIncomingWireMessageObj(otherMachineName, youToMeSlot),
),
);
const key = createIncomingWireMessageKey(otherMachineName, youToMeSlot);
if (!state.has(key)) {
console.log(`unable to find key ${key}`);
}
return state.get(key);
}

// takes kernelToMeSlot, returns meToYouSlot and machineName
// we don't know the otherMachineName
function mapKernelSlotToOutgoingWireMessage(kernelToMeSlot) {
return state.get(JSON.stringify(kernelToMeSlot));
return state.get(createKernelToMeKey(kernelToMeSlot));
}

// kernelToMeSlot can have type: import, export or promise
Expand All @@ -83,24 +94,18 @@ export function makeCLists() {
// 2) to translate something that we get over the wire (youToMeSlot)
// into a kernelToMeSlot.
function add(otherMachineName, kernelToMeSlot, youToMeSlot, meToYouSlot) {
const incomingWireMessageObj = createIncomingWireMessageObj(
const incomingWireMessageKey = createIncomingWireMessageKey(
otherMachineName,
youToMeSlot,
);
const outgoingWireMessageObj = createOutgoingWireMessageObj(
otherMachineName,
meToYouSlot,
);
checkIfAlreadyExists(
incomingWireMessageObj,
outgoingWireMessageObj,
kernelToMeSlot,
);
// TODO: serialize these more stably, since JSON will depend on the order
// in which the properties were added. Maybe `${type}-${id}` or
// djson.stringify.
state.set(JSON.stringify(kernelToMeSlot), outgoingWireMessageObj);
state.set(JSON.stringify(incomingWireMessageObj), kernelToMeSlot);
const kernelToMeKey = createKernelToMeKey(kernelToMeSlot);
checkIfAlreadyExists(incomingWireMessageKey, kernelToMeKey);
state.set(kernelToMeKey, outgoingWireMessageObj);
state.set(incomingWireMessageKey, kernelToMeSlot);
}

return {
Expand Down

0 comments on commit b7f311e

Please sign in to comment.