Skip to content

Commit 8c37678

Browse files
committed
fix: harden invalid graphml import handling
1 parent 0678858 commit 8c37678

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

src/graph-builder/graph-core/5-load-save.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ class GraphLoadSave extends GraphUndoRedo {
231231
localStorageManager.save(this.id, graphObject);
232232
this.loadGraphFromLocalStorage();
233233
this.lastSavedActionIndex = this.curActionIndex;
234+
}).catch(() => {
235+
toast.error('Invalid GraphML file.');
234236
});
235237
}
236238

src/graph-builder/graphml/parser/index.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@ import {
44
parseNode, parseEdge, parseDetails, parseActionHistory,
55
} from './parseProperties';
66

7-
const parser = (graphMlCnt) => new Promise((resolve) => {
7+
const parser = (graphMlCnt) => new Promise((resolve, reject) => {
88
new xml2js.Parser().parseString(graphMlCnt, (err, grahMLObj) => {
9-
const grahML = new PropFromArr(grahMLObj);
10-
const nodes = grahML.parseProps('graphml.graph.node', 1).map(parseNode);
11-
const edges = grahML.parseProps('graphml.graph.edge', 1).map(parseEdge);
12-
const {
13-
id, projectName, serverID, authorName,
14-
} = parseDetails(grahML);
15-
const actionHistory = parseActionHistory(grahML);
16-
resolve({
17-
id, projectName, edges, nodes, actionHistory, serverID, authorName,
18-
});
9+
if (err || !grahMLObj) {
10+
reject(err || new Error('Invalid GraphML file.'));
11+
return;
12+
}
13+
try {
14+
const grahML = new PropFromArr(grahMLObj);
15+
const nodes = grahML.parseProps('graphml.graph.node', 1).map(parseNode);
16+
const edges = grahML.parseProps('graphml.graph.edge', 1).map(parseEdge);
17+
const {
18+
id, projectName, serverID, authorName,
19+
} = parseDetails(grahML);
20+
const actionHistory = parseActionHistory(grahML);
21+
resolve({
22+
id, projectName, edges, nodes, actionHistory, serverID, authorName,
23+
});
24+
} catch {
25+
reject(new Error('Invalid GraphML file.'));
26+
}
1927
});
2028
});
2129
export default parser;

src/toolbarActions/toolbarFunctions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ const readFile = async (state, setState, file, fileHandle) => {
160160
projectName, graphML: x.target.result, fileHandle, fileName: file.name, authorName,
161161
},
162162
});
163+
}).catch(() => {
164+
toast.error('Invalid GraphML file.');
163165
});
164166
};
165167
if (fileHandle) fr.readAsText(await fileHandle.getFile());

0 commit comments

Comments
 (0)