Skip to content

Commit

Permalink
[Lint] Fix no-prototype-builtins and no-namespace violations (#178)
Browse files Browse the repository at this point in the history
import/export is preferred over namespaces [1].

A malicious client could send a JSON value like `{"hasOwnProperty": 1}`, which would
crash the server. Instead, use methods like `hasOwnProperty` from the
`Object.prototype`. [2]

[1]: https://typescript-eslint.io/rules/no-namespace/
[2]: https://eslint.org/docs/latest/rules/no-prototype-builtins
  • Loading branch information
dchege711 authored Jun 16, 2024
1 parent 83c61d3 commit b0d2071
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
14 changes: 9 additions & 5 deletions src/public/src/models/core/graph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { MaxPriorityQueue } from "./max-priority-queue.js";
import {
MaxPriorityQueue,
MaxPriorityQueueKey,
MaxPriorityQueueKeyAndWeight,
} from "./max-priority-queue.js";

/**
* @description A minimal graph data type
Expand Down Expand Up @@ -45,12 +49,12 @@ export class UndirectedGraph {
* values are the respective distances
*/
kNearNeighbors(nodeIDs: string[], k: number = 7): string[] {
const kNeighbors: Map<MaxPriorityQueue.T, number> = new Map([]);
const alreadySeenIDs: Set<MaxPriorityQueue.T> = new Set(nodeIDs);
const kNeighbors: Map<MaxPriorityQueueKey, number> = new Map([]);
const alreadySeenIDs: Set<MaxPriorityQueueKey> = new Set(nodeIDs);
let pq = new MaxPriorityQueue<string>();

const enqueue = (
node: MaxPriorityQueue.T,
node: MaxPriorityQueueKey,
currentPQ: MaxPriorityQueue<string>,
): void => {
if (!this.adj[node]) { return; }
Expand All @@ -65,7 +69,7 @@ export class UndirectedGraph {
enqueue(nodeID, pq);
});

let nodeAndNegatedDistance: MaxPriorityQueue.KeyAndWeight | null;
let nodeAndNegatedDistance: MaxPriorityQueueKeyAndWeight | null;
let nextPQ: MaxPriorityQueue<string>;
while (kNeighbors.size < k && !pq.is_empty()) {
nextPQ = new MaxPriorityQueue<string>();
Expand Down
34 changes: 20 additions & 14 deletions src/public/src/models/core/max-priority-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
* @class
*/

export module MaxPriorityQueue {
export type T = string | number;
export type KeyAndWeight = [T, number];
}

export class MaxPriorityQueue<T extends MaxPriorityQueue.T> {
pq: MaxPriorityQueue.KeyAndWeight[] = [];
export type MaxPriorityQueueKey = string | number;
export type MaxPriorityQueueWeight = number;
export type MaxPriorityQueueKeyAndWeight = [
MaxPriorityQueueKey,
MaxPriorityQueueWeight,
];

export class MaxPriorityQueue<T extends MaxPriorityQueueKey> {
pq: MaxPriorityQueueKeyAndWeight[] = [];
n = 0;
keys_insertion_order: { [key: MaxPriorityQueue.T]: number } = {};
keys_insertion_order: { [key: MaxPriorityQueueKey]: number } = {};

/* An ever-increasing variable that allows stable sorting in the PQ */
insert_order_id = 0; // We're not famous enough for an overflow problem
Expand All @@ -23,7 +25,7 @@ export class MaxPriorityQueue<T extends MaxPriorityQueue.T> {
* @description Initialize the max PQ data structure.
* @param {Array} keys_and_weights Array of (key, weight) tuples
*/
initialize(keys_and_weights: MaxPriorityQueue.KeyAndWeight[]) {
initialize(keys_and_weights: MaxPriorityQueueKeyAndWeight[]) {
this.n = keys_and_weights.length;
this.pq = Array(this.n + 1).fill([null, Number.NEGATIVE_INFINITY]);

Expand Down Expand Up @@ -61,19 +63,23 @@ export class MaxPriorityQueue<T extends MaxPriorityQueue.T> {
* @param {String} key The key of the item being queried
*/
contains_key(key: T): boolean {
return this.keys_insertion_order.hasOwnProperty(key);
return Object.prototype.hasOwnProperty.call(this.keys_insertion_order, key);
}

/**
* @description Insert `key_and_weight` into the priority queue.
* @param {Array} key_and_weight A tuple representing the key of the value
* to be inserted, and its weight relative to what's in the PQ.
*/
insert(key_and_weight: MaxPriorityQueue.KeyAndWeight) {
insert(key_and_weight: MaxPriorityQueueKeyAndWeight) {
if (this.n === this.pq.length - 1) { this.resize(2 * this.pq.length); }

this.n += 1;
if (!this.keys_insertion_order.hasOwnProperty(key_and_weight[0])) {
const keyExists = Object.prototype.hasOwnProperty.call(
this.keys_insertion_order,
key_and_weight[0],
);
if (!keyExists) {
this.keys_insertion_order[key_and_weight[0]] = this.insert_order_id;
this.insert_order_id += 1;
}
Expand All @@ -85,7 +91,7 @@ export class MaxPriorityQueue<T extends MaxPriorityQueue.T> {
/**
* @description Delete the item that has the most weight in the PQ.
*/
del_max(): MaxPriorityQueue.KeyAndWeight | null {
del_max(): MaxPriorityQueueKeyAndWeight | null {
if (this.is_empty()) { return null; }

const max = this.pq[1];
Expand All @@ -107,7 +113,7 @@ export class MaxPriorityQueue<T extends MaxPriorityQueue.T> {
* @description Return the item at the top of the PQ, but don't
* delete it.
*/
peek(): MaxPriorityQueue.KeyAndWeight | null {
peek(): MaxPriorityQueueKeyAndWeight | null {
if (this.n === 0) { return null; }
return this.pq[1];
}
Expand Down

0 comments on commit b0d2071

Please sign in to comment.