Javascript application for fuzzing#2
Merged
0roman merged 5 commits intoCodeIntelligenceTesting:mainfrom Aug 23, 2023
Merged
Conversation
0xricksanchez
suggested changes
Jul 31, 2023
Comment on lines
+3
to
+5
| ## TODO application | ||
|
|
||
| ### Usage of application |
There was a problem hiding this comment.
Suggested change
| ## TODO application | |
| ### Usage of application | |
| ## Functionality |
| @@ -0,0 +1,28 @@ | |||
| # Nodejs JS demo application | |||
There was a problem hiding this comment.
Suggested change
| # Nodejs JS demo application | |
| # NodeJS demo application |
|
|
||
| ### Usage of application | ||
|
|
||
| It is simple nodejs express TODO application which has several functionalities, such as, adding, |
There was a problem hiding this comment.
Suggested change
| It is simple nodejs express TODO application which has several functionalities, such as, adding, | |
| It is simple nodeJS express TODO application, which has several functionalities, such as, adding, |
| ### Usage of application | ||
|
|
||
| It is simple nodejs express TODO application which has several functionalities, such as, adding, | ||
| deleting, listing TODOs, deleting whole json file and command execution in the server. The application creates |
There was a problem hiding this comment.
Suggested change
| deleting, listing TODOs, deleting whole json file and command execution in the server. The application creates | |
| deleting single TODOs, and listing TODOs. Additional functionality includes, deleting the whole TODO list json file and command execution on the server. The application creates |
|
|
||
| It is simple nodejs express TODO application which has several functionalities, such as, adding, | ||
| deleting, listing TODOs, deleting whole json file and command execution in the server. The application creates | ||
| todo.json file in the root folder of the application. |
There was a problem hiding this comment.
Suggested change
| todo.json file in the root folder of the application. | |
| a `todo.json` file in the root folder of the application as a database to save all added TODOs. |
Comment on lines
+11
to
+21
| Endpoints: | ||
|
|
||
| `/api/add?todo=<todo>&deadline=<deadline>` | ||
|
|
||
| `/api/delete?id=<id>` | ||
|
|
||
| `/api/list` | ||
|
|
||
| `/api/deleteList` | ||
|
|
||
| `/api/server?command=<command>` |
There was a problem hiding this comment.
Suggested change
| Endpoints: | |
| `/api/add?todo=<todo>&deadline=<deadline>` | |
| `/api/delete?id=<id>` | |
| `/api/list` | |
| `/api/deleteList` | |
| `/api/server?command=<command>` | |
| Available endpoints: | |
/api/add?todo=&deadline=
/api/delete?id=
/api/list
/api/deleteList
/api/server?command=
| { | ||
| "name": "nodejs", | ||
| "version": "1.0.0", | ||
| "description": "Nodejs Javascript tutorial example", |
There was a problem hiding this comment.
Suggested change
| "description": "Nodejs Javascript tutorial example", | |
| "description": "NodeJS tutorial example", |
| @@ -0,0 +1,16 @@ | |||
| { | |||
| "name": "nodejs", | |||
There was a problem hiding this comment.
Suggested change
| "name": "nodejs", | |
| "name": "TODO list server", |
Comment on lines
+1
to
+109
| const fs = require('fs') | ||
| const child_process = require('child_process') | ||
|
|
||
| class TODO { | ||
| id | ||
| todo | ||
| deadline | ||
|
|
||
| constructor(id, todo, deadline) { | ||
| this.id = id; | ||
| this.todo = todo | ||
| this.deadline = deadline | ||
| } | ||
| } | ||
|
|
||
| function fileIsPresent() { | ||
| return fs.existsSync('./todo.json'); | ||
| } | ||
|
|
||
|
|
||
| function writeToFile(data) { | ||
| try{ | ||
| fs.writeFileSync("./todo.json", JSON.stringify(data)); | ||
| }catch (e) { | ||
| throw new Error() | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function readFromFile() { | ||
| try { | ||
| return fs.readFileSync('./todo.json'); | ||
| } catch (e) { | ||
| throw new Error() | ||
| } | ||
| } | ||
|
|
||
| function createAndWrite(dataToWrite){ | ||
| let initial_arr = [] | ||
| dataToWrite.id = 1 | ||
| initial_arr.push(dataToWrite) | ||
| writeToFile(initial_arr); | ||
| } | ||
|
|
||
| function deleteEntry(id) { | ||
| try { | ||
| if (fileIsPresent()) { | ||
| const temp = readFromFile(); | ||
| let content = JSON.parse(temp.toString()); | ||
| let _index = -1; | ||
| content.forEach((element, index) => { | ||
| if (String(element.id) === id) { | ||
| _index = index; | ||
| } | ||
| }); | ||
| if (_index >= 0) { | ||
| content.splice(_index, 1); | ||
| writeToFile(content); | ||
| } else { | ||
| throw new Error(); | ||
| } | ||
| } else { | ||
| throw new Error(); | ||
| } | ||
|
|
||
| }catch (e) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| function addEntry(dataToWrite) { | ||
| try{ | ||
| if (fileIsPresent()) { | ||
| const data = readFromFile(); | ||
| let content = JSON.parse(data.toString()); | ||
| if (content.length > 0) { | ||
| let last_id = content[content.length - 1].id | ||
| dataToWrite.id = last_id + 1 | ||
| content.push(dataToWrite); | ||
| writeToFile(content); | ||
| } else { | ||
| createAndWrite(dataToWrite) | ||
| } | ||
| } else { | ||
| createAndWrite(dataToWrite) | ||
| } | ||
| }catch (e) { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| function deleteFile() { | ||
| try { | ||
| if (fileIsPresent()) { | ||
| fs.unlink('./todo.json', (err) => { | ||
| if (err) throw new Error() | ||
| }) | ||
| return true; | ||
| } | ||
| return false; | ||
| }catch (e) { | ||
| return false; | ||
| } | ||
| } | ||
| function listFile() { | ||
| if (fileIsPresent()) { | ||
| // read the file | ||
| const data = readFromFile(); | ||
| const content = JSON.parse(data.toString()); | ||
| let respond = ""; | ||
| content.forEach((element) => respond = respond + "Id: " + element.id + "\tTODO: " + element.todo + "\tDeadline: " + element.deadline + "\n"); | ||
| return[true, respond] | ||
| } | ||
| return [false, null] | ||
| } | ||
|
|
||
| function commandExecution(command, fn) { | ||
| child_process.exec(command, (err, stdout, stderr) => { | ||
| fn(stdout) | ||
| }); | ||
| } | ||
|
|
||
| module.exports = {TODO, commandExecution, addEntry, deleteEntry, deleteFile, listFile} No newline at end of file |
There was a problem hiding this comment.
Suggested change
| const fs = require('fs') | |
| const child_process = require('child_process') | |
| class TODO { | |
| id | |
| todo | |
| deadline | |
| constructor(id, todo, deadline) { | |
| this.id = id; | |
| this.todo = todo | |
| this.deadline = deadline | |
| } | |
| } | |
| function fileIsPresent() { | |
| return fs.existsSync('./todo.json'); | |
| } | |
| function writeToFile(data) { | |
| try{ | |
| fs.writeFileSync("./todo.json", JSON.stringify(data)); | |
| }catch (e) { | |
| throw new Error() | |
| } | |
| } | |
| function readFromFile() { | |
| try { | |
| return fs.readFileSync('./todo.json'); | |
| } catch (e) { | |
| throw new Error() | |
| } | |
| } | |
| function createAndWrite(dataToWrite){ | |
| let initial_arr = [] | |
| dataToWrite.id = 1 | |
| initial_arr.push(dataToWrite) | |
| writeToFile(initial_arr); | |
| } | |
| function deleteEntry(id) { | |
| try { | |
| if (fileIsPresent()) { | |
| const temp = readFromFile(); | |
| let content = JSON.parse(temp.toString()); | |
| let _index = -1; | |
| content.forEach((element, index) => { | |
| if (String(element.id) === id) { | |
| _index = index; | |
| } | |
| }); | |
| if (_index >= 0) { | |
| content.splice(_index, 1); | |
| writeToFile(content); | |
| } else { | |
| throw new Error(); | |
| } | |
| } else { | |
| throw new Error(); | |
| } | |
| }catch (e) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| function addEntry(dataToWrite) { | |
| try{ | |
| if (fileIsPresent()) { | |
| const data = readFromFile(); | |
| let content = JSON.parse(data.toString()); | |
| if (content.length > 0) { | |
| let last_id = content[content.length - 1].id | |
| dataToWrite.id = last_id + 1 | |
| content.push(dataToWrite); | |
| writeToFile(content); | |
| } else { | |
| createAndWrite(dataToWrite) | |
| } | |
| } else { | |
| createAndWrite(dataToWrite) | |
| } | |
| }catch (e) { | |
| return false | |
| } | |
| return true | |
| } | |
| function deleteFile() { | |
| try { | |
| if (fileIsPresent()) { | |
| fs.unlink('./todo.json', (err) => { | |
| if (err) throw new Error() | |
| }) | |
| return true; | |
| } | |
| return false; | |
| }catch (e) { | |
| return false; | |
| } | |
| } | |
| function listFile() { | |
| if (fileIsPresent()) { | |
| // read the file | |
| const data = readFromFile(); | |
| const content = JSON.parse(data.toString()); | |
| let respond = ""; | |
| content.forEach((element) => respond = respond + "Id: " + element.id + "\tTODO: " + element.todo + "\tDeadline: " + element.deadline + "\n"); | |
| return[true, respond] | |
| } | |
| return [false, null] | |
| } | |
| function commandExecution(command, fn) { | |
| child_process.exec(command, (err, stdout, stderr) => { | |
| fn(stdout) | |
| }); | |
| } | |
| module.exports = {TODO, commandExecution, addEntry, deleteEntry, deleteFile, listFile} | |
| const fs = require('fs'); | |
| const child_process = require('child_process') | |
| class TODO { | |
| constructor(id, todo, deadline) { | |
| this.id = id; | |
| this.todo = todo; | |
| this.deadline = deadline; | |
| } | |
| } | |
| const filePath = './todo.json'; | |
| function fileIsPresent() { | |
| return fs.existsSync(filePath); | |
| } | |
| function writeToFile(data) { | |
| try { | |
| fs.writeFileSync(filePath, JSON.stringify(data)); | |
| } catch (e) { | |
| console.error('Error while writing to file:', e); | |
| } | |
| } | |
| function readFromFile() { | |
| try { | |
| return JSON.parse(fs.readFileSync(filePath).toString()); | |
| } catch (e) { | |
| console.error('Error while reading from file:', e); | |
| } | |
| return null; | |
| } | |
| function createAndWrite(dataToWrite) { | |
| dataToWrite.id = 1; | |
| writeToFile([dataToWrite]); | |
| } | |
| function deleteEntry(id) { | |
| if (fileIsPresent()) { | |
| let content = readFromFile(); | |
| if (content) { | |
| let index = content.findIndex(element => element.id === id); | |
| if (index >= 0) { | |
| content.splice(index, 1); | |
| writeToFile(content); | |
| return true; | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| function addEntry(dataToWrite) { | |
| if (fileIsPresent()) { | |
| let content = readFromFile(); | |
| if (content) { | |
| let lastId = content[content.length - 1].id; | |
| dataToWrite.id = lastId + 1; | |
| content.push(dataToWrite); | |
| writeToFile(content); | |
| return true; | |
| } | |
| } else { | |
| createAndWrite(dataToWrite); | |
| return true; | |
| } | |
| return false; | |
| } | |
| function deleteFile() { | |
| try { | |
| if (fileIsPresent()) { | |
| fs.unlinkSync(filePath); | |
| return true; | |
| } | |
| } catch (e) { | |
| console.error('Error while deleting file:', e); | |
| } | |
| return false; | |
| } | |
| function listFile() { | |
| if (fileIsPresent()) { | |
| const content = readFromFile(); | |
| if (content) { | |
| return content.map(element => `Id: ${element.id}\tTODO: ${element.todo}\tDeadline: ${element.deadline}`).join('\n'); | |
| } | |
| } | |
| return null; | |
| } | |
| function commandExecution(command, fn) { | |
| child_process.exec(command, (err, stdout, stderr) => { | |
| fn(stdout) | |
| }); | |
| } | |
| module.exports = { TODO, addEntry, deleteEntry, deleteFile, listFile, commandExecution } | |
That should be more aligned with modern JavaScript.
| @@ -0,0 +1,2 @@ | |||
| package-lock.json | |||
There was a problem hiding this comment.
JS comments also apply to the TS side. I'll leave adding these to the TS side to you @turalsalamov
Contributor
|
Add an entry in the README for the new tutorials. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
It is a nodejs javascript demo application with vulnerability for fuzzing. Details of application are described in README.md file.