Skip to content

Javascript application for fuzzing#2

Merged
0roman merged 5 commits intoCodeIntelligenceTesting:mainfrom
turalsalamov:js-ts
Aug 23, 2023
Merged

Javascript application for fuzzing#2
0roman merged 5 commits intoCodeIntelligenceTesting:mainfrom
turalsalamov:js-ts

Conversation

@turalsalamov
Copy link
Copy Markdown
Contributor

It is a nodejs javascript demo application with vulnerability for fuzzing. Details of application are described in README.md file.

Comment thread js_ts/nodejs-js/README.md Outdated
Comment on lines +3 to +5
## TODO application

### Usage of application
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## TODO application
### Usage of application
## Functionality

Comment thread js_ts/nodejs-js/README.md Outdated
@@ -0,0 +1,28 @@
# Nodejs JS demo application
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Nodejs JS demo application
# NodeJS demo application

Comment thread js_ts/nodejs-js/README.md Outdated

### Usage of application

It is simple nodejs express TODO application which has several functionalities, such as, adding,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Comment thread js_ts/nodejs-js/README.md Outdated
### 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread js_ts/nodejs-js/README.md Outdated

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.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thread js_ts/nodejs-js/README.md Outdated
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>`
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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=


Comment thread js_ts/nodejs-js/package.json Outdated
{
"name": "nodejs",
"version": "1.0.0",
"description": "Nodejs Javascript tutorial example",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"description": "Nodejs Javascript tutorial example",
"description": "NodeJS tutorial example",

Comment thread js_ts/nodejs-js/package.json Outdated
@@ -0,0 +1,16 @@
{
"name": "nodejs",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"name": "nodejs",
"name": "TODO list server",

Comment thread js_ts/nodejs-js/utils.js
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS comments also apply to the TS side. I'll leave adding these to the TS side to you @turalsalamov

@0roman 0roman self-requested a review August 7, 2023 13:44
@0roman
Copy link
Copy Markdown
Contributor

0roman commented Aug 7, 2023

Add an entry in the README for the new tutorials.

@0roman 0roman removed their request for review August 7, 2023 13:46
@0roman 0roman merged commit bbe8ef8 into CodeIntelligenceTesting:main Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants