-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Js to Ts : simulator/src/backupCircuits.ts #417
Draft
ThatDeparted2061
wants to merge
6
commits into
CircuitVerse:main
Choose a base branch
from
ThatDeparted2061:Js-to-Ts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−85
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
78cfbb5
Js to Ts
ThatDeparted2061 f9b91a0
fix
ThatDeparted2061 3fb6950
Merge branch 'CircuitVerse:main' into Js-to-Ts
ThatDeparted2061 e9cf6ce
resolve
ThatDeparted2061 5447152
resolve
ThatDeparted2061 95ccf1a
resolve
ThatDeparted2061 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { projectSavedSet } from './project'; | ||
import { moduleList, updateOrder } from '../metadata'; | ||
import { LayoutData,VerilogMetadata,TestbenchData,Scope, | ||
RestrictedCircuitElements,SaveableObject } from './data.types' | ||
|
||
declare const globalScope: Scope; | ||
|
||
// Helper function to extract data from an object | ||
function extract(obj: SaveableObject): unknown { | ||
return obj.saveObject(); | ||
} | ||
|
||
/** | ||
* Check if backup is available | ||
* @param {Scope} scope | ||
* @return {boolean} | ||
* @category data | ||
*/ | ||
export function checkIfBackup(scope: Scope): boolean { | ||
for (let i = 0; i < updateOrder.length; i++) { | ||
if ((scope[updateOrder[i]] as unknown[]).length) return true; | ||
} | ||
return false; | ||
} | ||
|
||
interface BackupData { | ||
layout: LayoutData; | ||
verilogMetadata: VerilogMetadata; | ||
allNodes: unknown[]; | ||
testbenchData: TestbenchData; | ||
id: string; | ||
name: string; | ||
nodes: number[]; | ||
restrictedCircuitElementsUsed: RestrictedCircuitElements; | ||
[key: string]: unknown; | ||
} | ||
|
||
export function backUp(scope: Scope = globalScope): BackupData { | ||
// Disconnection of subcircuits are needed because these are the connections between nodes | ||
// in current scope and those in the subcircuit's scope | ||
for (let i = 0; i < scope.SubCircuit.length; i++) { | ||
scope.SubCircuit[i].removeConnections(); | ||
} | ||
|
||
const data: BackupData = { | ||
layout: scope.layout, | ||
verilogMetadata: scope.verilogMetadata, | ||
allNodes: scope.allNodes.map(extract), | ||
testbenchData: scope.testbenchData, | ||
id: scope.id, | ||
name: scope.name, | ||
nodes: [], | ||
restrictedCircuitElementsUsed: scope.restrictedCircuitElementsUsed | ||
}; | ||
|
||
// Storing details of all module objects | ||
for (let i = 0; i < moduleList.length; i++) { | ||
if ((scope[moduleList[i]] as unknown[]).length) { | ||
data[moduleList[i]] = (scope[moduleList[i]] as SaveableObject[]).map(extract); | ||
} | ||
} | ||
|
||
// Storing intermediate nodes (nodes in wires) | ||
data.nodes = scope.nodes.map(node => scope.allNodes.indexOf(node)); | ||
|
||
// Restoring the connections | ||
for (let i = 0; i < scope.SubCircuit.length; i++) { | ||
scope.SubCircuit[i].makeConnections(); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
export function scheduleBackup(scope: Scope = globalScope): string { | ||
const backup = JSON.stringify(backUp(scope)); | ||
|
||
// Check if the backup is valid and doesn't contain malicious properties | ||
if (isValidBackup(backup)) { | ||
if ( | ||
scope.backups.length === 0 || | ||
scope.backups[scope.backups.length - 1] !== backup | ||
) { | ||
// Safely assign the backup to scope.backups | ||
scope.backups = [...scope.backups, backup]; | ||
|
||
// Safely assign an empty array to scope.history | ||
scope.history = []; | ||
Check warning Code scanning / CodeQL Prototype-polluting assignment Medium
This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
user controlled input Error loading related location Loading |
||
|
||
// Safely assign the current timestamp | ||
scope.timeStamp = new Date().getTime(); | ||
Check warning Code scanning / CodeQL Prototype-polluting assignment Medium
This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
user controlled input Error loading related location Loading |
||
|
||
// Mark the project as unsaved | ||
projectSavedSet(false); | ||
} | ||
} else { | ||
console.error("Invalid backup data detected. Backup aborted."); | ||
} | ||
|
||
return backup; | ||
} | ||
|
||
/** | ||
* Validate the backup data to prevent prototype pollution. | ||
* @param backup The backup data to validate. | ||
* @returns True if the backup is valid, false otherwise. | ||
*/ | ||
function isValidBackup(backup: string): boolean { | ||
try { | ||
const parsedBackup = JSON.parse(backup); | ||
|
||
// Check if the parsed backup contains any malicious properties | ||
if (parsedBackup && typeof parsedBackup === "object") { | ||
for (const key in parsedBackup) { | ||
if (key === "__proto__" || key === "constructor" || key === "prototype") { | ||
return false; // Malicious property detected | ||
} | ||
} | ||
} | ||
|
||
return true; // Backup is valid | ||
} catch (error) { | ||
console.error("Failed to parse backup data:", error); | ||
return false; // Backup is invalid | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
export interface LayoutData { | ||
width: number; | ||
height: number; | ||
} | ||
|
||
export interface VerilogMetadata { | ||
code: string; | ||
} | ||
|
||
export interface TestbenchData { | ||
testCases: TestCase[]; | ||
} | ||
|
||
interface TestCase { | ||
inputs: Record<string, boolean>; | ||
outputs: Record<string, boolean>; | ||
} | ||
|
||
export interface RestrictedCircuitElements { | ||
[elementName: string]: boolean; | ||
} | ||
|
||
export interface Scope { | ||
layout: LayoutData; | ||
verilogMetadata: VerilogMetadata; | ||
allNodes: Node[]; | ||
testbenchData: TestbenchData; | ||
id: string; | ||
name: string; | ||
SubCircuit: SubCircuit[]; | ||
nodes: Node[]; | ||
backups: string[]; | ||
history: unknown[]; | ||
timeStamp: number; | ||
restrictedCircuitElementsUsed: RestrictedCircuitElements; | ||
[key: string]: unknown; | ||
} | ||
|
||
interface SubCircuit { | ||
removeConnections(): void; | ||
makeConnections(): void; | ||
saveObject(): SavedSubCircuit; | ||
} | ||
|
||
interface SavedSubCircuit { | ||
id: string; | ||
} | ||
|
||
export interface SaveableObject { | ||
saveObject(): unknown; | ||
} | ||
interface Node extends SaveableObject { | ||
id: string; | ||
connections: string[]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Prototype-polluting assignment Medium