Skip to content

Commit

Permalink
return 404 if accessing a workspace path when workspace is disabled
Browse files Browse the repository at this point in the history
Signed-off-by: Yulong Ruan <ruanyl@amazon.com>
  • Loading branch information
ruanyl committed Jul 13, 2023
1 parent 55ddf6b commit 54c7321
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
8 changes: 1 addition & 7 deletions src/core/public/workspace/workspaces_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { WorkspacesClient, WorkspacesClientContract } from './workspaces_client'
import type { WorkspaceAttribute } from '../../server/types';
import { HttpSetup } from '../http';
import { IUiSettingsClient } from '../ui_settings';
import { getWorkspaceIdFromUrl } from '../utils/workspace';

/**
* @public
Expand All @@ -32,12 +31,7 @@ export class WorkspacesService implements CoreService<WorkspacesSetup, Workspace
this.client = new WorkspacesClient(http);

// If workspace was disabled while opening a workspace url, navigate to basePath
if (uiSettings.get('workspace:enabled') === false) {
const workspaceId = getWorkspaceIdFromUrl(window.location.href);
if (workspaceId) {
window.location.href = http.basePath.getBasePath();
}
} else {
if (uiSettings.get('workspace:enabled') === true) {
this.client.init();
}

Expand Down
1 change: 1 addition & 0 deletions src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export class Server {
});
await this.workspaces.start({
savedObjects: savedObjectsStart,
uiSettings: uiSettingsStart,
});

this.coreStart = {
Expand Down
28 changes: 23 additions & 5 deletions src/core/server/workspaces/workspaces_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../saved_objects';
import { IWorkspaceDBImpl } from './types';
import { WorkspacesClientWithSavedObject } from './workspaces_client';
import { UiSettingsServiceStart } from '../ui_settings/types';

export interface WorkspacesServiceSetup {
client: IWorkspaceDBImpl;
Expand All @@ -34,28 +35,44 @@ export type InternalWorkspacesServiceStart = WorkspacesServiceStart;
/** @internal */
export interface WorkspacesStartDeps {
savedObjects: InternalSavedObjectsServiceStart;
uiSettings: UiSettingsServiceStart;
}

export class WorkspacesService
implements CoreService<WorkspacesServiceSetup, WorkspacesServiceStart> {
private logger: Logger;
private client?: IWorkspaceDBImpl;
private startDeps?: WorkspacesStartDeps;
constructor(coreContext: CoreContext) {
this.logger = coreContext.logger.get('workspaces-service');
}

private proxyWorkspaceTrafficToRealHandler(setupDeps: WorkspacesSetupDeps) {
/**
* Proxy all {basePath}/w/{workspaceId}{osdPath*} paths to
* {basePath}{osdPath*}
* {basePath}{osdPath*} when workspace is enabled
*
* Return HTTP 404 if accessing {basePath}/w/{workspaceId} when workspace is disabled
*/
setupDeps.http.registerOnPreRouting((request, response, toolkit) => {
setupDeps.http.registerOnPreRouting(async (request, response, toolkit) => {
const regexp = /\/w\/([^\/]*)/;
const matchedResult = request.url.pathname.match(regexp);

if (matchedResult) {
const requestUrl = new URL(request.url.toString());
requestUrl.pathname = requestUrl.pathname.replace(regexp, '');
return toolkit.rewriteUrl(requestUrl.toString());
if (this.startDeps) {
const savedObjectsClient = this.startDeps.savedObjects.getScopedClient(request);
const uiSettingsClient = this.startDeps.uiSettings.asScopedToClient(savedObjectsClient);
const workspacesEnabled = await uiSettingsClient.get<boolean>('workspace:enabled');

if (workspacesEnabled) {
const requestUrl = new URL(request.url.toString());
requestUrl.pathname = requestUrl.pathname.replace(regexp, '');
return toolkit.rewriteUrl(requestUrl.toString());
} else {
// If workspace was disable, return HTTP 404
return response.notFound();
}
}
}
return toolkit.next();
});
Expand All @@ -80,6 +97,7 @@ export class WorkspacesService
}

public async start(deps: WorkspacesStartDeps): Promise<InternalWorkspacesServiceStart> {
this.startDeps = deps;
this.logger.debug('Starting SavedObjects service');

return {
Expand Down

0 comments on commit 54c7321

Please sign in to comment.