Devsoc-BPGC / Short-Me

3 stars 14 forks source link

Block urls that are present in the added hosts file #27

Closed riskycase closed 2 years ago

riskycase commented 3 years ago

Need to test this but the intended behavior is now added

Pull request checklist

Please check if your PR :rocket: fulfills the following requirements:

Pull request type

Please check the type of change your PR introduces:

What is the current behavior?

There is no checking for URLs that have NSFW contents

What is the new behavior?

Original URL hostname is now checked against an included hosts file and then blocked if found in it

Does this introduce a breaking change?

Other information

URL is blocked if present in the hosts file, added a sample response but not sure how to test it in frontend.

riskycase commented 3 years ago

Benchmarks:

Parameter Time 1 Time 2 Time 3 Average
Read file to memory 642.766ms 645.003ms 632.697ms 640.159ms
Additional overhead for checking URL in hostname file 1.458ms 1.456ms 1.461ms 1.459ms

Test system: AMD Ryzen 4800HS 16 GB 3200MHz RAM 1 TB WD SN530 SSD

Ran each test multiple times and took the last 3 readings

Code for testing file read:

console.time('read');
const fileRead = require('fs').promises.readFile('./hosts/hosts.txt', 'utf-8').then(contents => {
  console.timeEnd('read');
  return contents;
});

Code for testing lookup

  console.time('check');

  // Use the hosts file to check if any unsafe URL is being shortened
  const contents = await fileRead;
  const urlObj = new URL(longUrl);

  if(contents.indexOf(urlObj.hostname) >= 0) {
    return res.status(400).json({"error": "This URL will not be shortened"});
  }
  console.timeEnd('check');

Lookup code was run for an URL that wasn't present in the file so time taken is likely for the entire file to be checked

riskycase commented 3 years ago

@sameshl @ishantgupta777 bumping this

Devesh21700Kumar commented 3 years ago

@sameshl @ishantgupta777 bumping this