Skip to content

Commit

Permalink
feat: start directory index frame
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelDeimos committed Jun 19, 2024
1 parent 581964b commit fb1e2f2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
37 changes: 35 additions & 2 deletions packages/backend/src/routers/hosting/puter-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ const { LLRead } = require("../../filesystem/ll_operations/ll_read");
const { Actor, UserActorType, SiteActorType } = require("../../services/auth/Actor");
const APIError = require("../../api/APIError");

const AT_DIRECTORY_NAMESPACE = '4aa6dc52-34c1-4b8a-b63c-a62b27f727cf';

class PuterSiteMiddleware extends AdvancedBase {
static MODULES = {
path: require('path'),
mime: require('mime-types'),
uuidv5: require('uuid').v5,
}
install (app) {
app.use(this.run.bind(this));
Expand Down Expand Up @@ -66,9 +69,39 @@ class PuterSiteMiddleware extends AdvancedBase {

const context = Context.get();
const services = context.get('services');

const get_username_site = (async () => {
if ( ! subdomain.endsWith('.at') ) return;
const parts = subdomain.split('.');
if ( parts.length !== 2 ) return;
const username = parts[0];
if ( ! username.match(config.username_regex) ) {
return;
}
const svc_fs = services.get('filesystem');
const index_node = await svc_fs.node(new NodePathSelector(
`/${username}/Public/index.html`
));
const node = await svc_fs.node(new NodePathSelector(
`/${username}/Public`
));
if ( ! await index_node.exists() ) return;

return {
name: username + '.at',
uuid: this.modules.uuidv5(username, AT_DIRECTORY_NAMESPACE),
root_dir_id: await node.get('mysql-id'),
};
})

const site =
await get_username_site() ||
await (async () => {
const svc_puterSite = services.get('puter-site');
const site = await svc_puterSite.get_subdomain(subdomain);
return site;
})();

const svc_puterSite = services.get('puter-site');
const site = await svc_puterSite.get_subdomain(subdomain);
if ( site === null ) {
return res.status(404).send('Subdomain not found');
}
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/services/PuterHomepageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class PuterHomepageService extends BaseService {
app_name_regex: config.app_name_regex,
app_name_max_length: config.app_name_max_length,
app_title_max_length: config.app_title_max_length,
hosting_domain: config.static_hosting_domain +
(config.pub_port !== 80 && config.pub_port !== 443 ? ':' + config.pub_port : ''),
subdomain_regex: config.subdomain_regex,
subdomain_max_length: config.subdomain_max_length,
domain: config.domain,
Expand Down
6 changes: 5 additions & 1 deletion packages/backend/src/services/WebServerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,11 @@ class WebServerService extends BaseService {
// Validate host header against allowed domains to prevent host header injection
// https://www.owasp.org/index.php/Host_Header_Injection
app.use((req, res, next)=>{
const allowedDomains = [config.domain.toLowerCase(), config.static_hosting_domain.toLowerCase()];
const allowedDomains = [
config.domain.toLowerCase(),
config.static_hosting_domain.toLowerCase(),
'at.' + config.static_hosting_domain.toLowerCase(),
];

// Retrieve the Host header and ensure it's in a valid format
const hostHeader = req.headers.host;
Expand Down

0 comments on commit fb1e2f2

Please sign in to comment.