Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to serve static files from filesystem #40

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/node_modules
/.nyc_output
/coverage
/dist
/node_modules
/test_dist
/coverage
/.nyc_output
*.tgz
14 changes: 14 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.github/
.nyc_output/
coverage/
dist/
lib/
sample/
test/
test_dist/
.editorconfig
.eslintrc.js
.nycrc.json
.travis.yml
tsconfig.*
*.tgz
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://npm.pkg.github.com
2 changes: 2 additions & 0 deletions lib/fast-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class FastTracker implements Tracker {

// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #swarms = new Map<string, Swarm>();

// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #peers = new Map<string, PeerContext>();

Expand Down Expand Up @@ -367,6 +368,7 @@ class Swarm {

// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #peers: PeerContext[] = [];

private completedPeers?: Set<string>;

public constructor(public readonly infoHash: string) { }
Expand Down
39 changes: 37 additions & 2 deletions lib/run-uws-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
*/

import { readFileSync } from "fs";
import { HttpResponse, HttpRequest } from "uWebSockets.js";
import { extname } from "path";

import * as Debug from "debug";
import { UWebSocketsTracker } from "./uws-tracker";
import { contentType } from "mime-types";
import { HttpResponse, HttpRequest } from "uWebSockets.js";

import { FastTracker } from "./fast-tracker";
import { Tracker } from "./tracker";
import { UWebSocketsTracker } from "./uws-tracker";


// eslint-disable-next-line new-cap
const debugRequests = Debug("wt-tracker:uws-tracker-requests");
Expand Down Expand Up @@ -203,6 +208,7 @@ function buildServer(
const status = "404 Not Found";
response.writeStatus(status).end(status);
} else {
response.writeHeader("Content-Type", "text/html; charset=utf-8");
response.end(indexHtml);
}
},
Expand Down Expand Up @@ -235,6 +241,35 @@ function buildServer(
memory: process.memoryUsage(),
}));
},
).get(
"/*",
(response: HttpResponse, request: HttpRequest) => {
debugRequest(server, request);

const path = request.getUrl();

let data = undefined;

try {
data = readFileSync(`${process.cwd()}${path}`);
Copy link
Member

Choose a reason for hiding this comment

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

Serving files is very responsible. This line is a potential vulnerability that allows reading arbitrary files in the file system.

Copy link
Author

Choose a reason for hiding this comment

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

Instead of just clossing the PR, you can suggest a proper fix for this... I think resolve the absolute path and ensure falls under the current working dir would be enough.

} catch (err) {
console.warn(err);
}

if (data === undefined) {
const status = "404 Not Found";
response.writeStatus(status).end(status);
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const type = contentType(extname(path)) as string | false;

if (type !== false) {
response.writeHeader("Content-Type", type);
}

response.end(data);
}
},
).any(
"/*",
(response: HttpResponse, request: HttpRequest) => {
Expand Down
26 changes: 15 additions & 11 deletions lib/uws-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export class UWebSocketsTracker {
readonly #app: TemplatedApp;

private webSocketsCount = 0;

private validateOrigin = false;

private readonly maxConnections: number;

public get app(): TemplatedApp {
Expand Down Expand Up @@ -174,17 +176,21 @@ export class UWebSocketsTracker {
private readonly onOpen = (ws: WebSocket, request: HttpRequest): void => {
this.webSocketsCount++;

const url = request.getUrl();
const query = request.getQuery();
const origin = request.getHeader("origin");

if ((this.maxConnections !== 0) && (this.webSocketsCount > this.maxConnections)) {
if (debugRequestsEnabled) {
debugRequests(
this.settings.server.host,
this.settings.server.port,
"ws-denied-max-connections url:",
request.getUrl(),
url,
"query:",
request.getQuery(),
query,
"origin:",
request.getHeader("origin"),
origin,
"total:",
this.webSocketsCount,
);
Expand All @@ -194,12 +200,10 @@ export class UWebSocketsTracker {
}

if (debugWebSocketsEnabled) {
debugWebSockets("connected via URL", request.getUrl());
debugWebSockets("connected via URL", url);
}

if (this.validateOrigin) {
const origin = request.getHeader("origin");

const shoulDeny = (
(this.settings.access.denyEmptyOrigin && (origin.length === 0))
|| (this.settings.access.denyOrigins?.includes(origin) === true)
Expand All @@ -212,9 +216,9 @@ export class UWebSocketsTracker {
this.settings.server.host,
this.settings.server.port,
"ws-denied url:",
request.getUrl(),
url,
"query:",
request.getQuery(),
query,
"origin:",
origin,
"total:",
Expand All @@ -231,11 +235,11 @@ export class UWebSocketsTracker {
this.settings.server.host,
this.settings.server.port,
"ws-open url:",
request.getUrl(),
url,
"query:",
request.getQuery(),
query,
"origin:",
request.getHeader("origin"),
origin,
"total:",
this.webSocketsCount,
);
Expand Down
Loading