Skip to content
Merged
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
43 changes: 33 additions & 10 deletions src/community/pages/Project.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import RoturConsentModal from '../components/RoturConsentModal.jsx';
import GameMarketplaceModal from '../components/GameMarketplaceModal.jsx';
import {getBalance} from '../../lib/rotur/client.js';
import {
hasFullGrant, commitGrant, callRotur,
activityAllowed, rememberActivityDecision, isActivityMethod
hasFullGrant, commitGrant, callRotur, grantedScopesFor,
activityAllowed, rememberActivityDecision
} from '../../lib/rotur/extension-bridge.js';
import {
authorizeProjectCall, grantsSilently, validateProjectScopes
} from '../../lib/rotur/project-methods.js';
import {getRoturSettings, setRoturSetting} from '../../lib/rotur/settings.js';
import ProjectActivityScope from '../../lib/rotur/project-activity-scope.js';
import {getUsernameOverride} from '../../lib/rotur/cloud-sync.js';
Expand Down Expand Up @@ -817,9 +820,20 @@ const Project = () => {
return;
}

// The grant key, the project name and whether to ask all come from
// this page. Anything the frame sends besides the scopes and the
// method is ignored, because custom extensions can post here too.
const meta = {
projectId: gamesProjectId,
name: (project && (project.title || project.name)) || ''
};

if (data.kind === 'consent') {
const scopes = data.scopes || [];
const meta = data.meta || {};
const scopes = validateProjectScopes(data.scopes);
if (!scopes) {
reply({id: data.id, ok: false, error: 'Projects cannot ask for that Rotur permission'});
return;
}
if (!identity.loggedIn) {
reply({id: data.id, ok: true, result: false});
return;
Expand All @@ -828,7 +842,7 @@ const Project = () => {
reply({id: data.id, ok: true, result: true});
return;
}
if (meta.authenticatedOnly) {
if (grantsSilently(scopes)) {
try {
await commitGrant(meta, scopes);
reply({id: data.id, ok: true, result: true});
Expand Down Expand Up @@ -867,7 +881,16 @@ const Project = () => {
}

if (data.kind === 'call') {
const {method, args, opts} = data;
const method = data.method;
let call;
try {
if (!identity.loggedIn) throw new Error('Log in to Rotur to use this block');
call = authorizeProjectCall(method, data.args, grantedScopesFor(meta), gamesProjectId);
} catch (e) {
reply({id: data.id, ok: false, error: String((e && e.message) || e)});
return;
}
const args = call.args;
const perform = async () => {
try {
const scope = roturActivityScope.current;
Expand All @@ -877,7 +900,7 @@ const Project = () => {
reply({id: data.id, ok: false, error: String((e && e.message) || e)});
}
};
if (isActivityMethod(method)) {
if (call.spec.activity) {
const key = (project && project.id) || id || `name:${(project && project.title) || ''}`;
const decision = activityAllowed(getRoturSettings().activitySharing, key);
if (decision === true) {
Expand Down Expand Up @@ -919,16 +942,16 @@ const Project = () => {
});
return;
}
if (opts && opts.sensitive) {
if (call.confirm) {
if (isProjectPromptBlocked({id: gamesProjectId})) {
reply({id: data.id, ok: false, error: 'You blocked prompts from this project'});
return;
}
setRoturModal({
type: 'confirm',
data: {
label: (opts && opts.label) || method,
confirmation: (opts && opts.confirmation) || null,
label: call.confirm.label,
confirmation: call.confirm.confirmation || null,
username: identity.username
},
onAllow: () => {
Expand Down
44 changes: 32 additions & 12 deletions src/containers/rotur-extension-host.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import bindAll from 'lodash.bindall';
import RoturConsentModal from '../components/mw-rotur-consent-modal/consent-modal.jsx';
import {
hasFullGrant, commitGrant, grantedScopesFor, callRotur,
activityAllowed, rememberActivityDecision, isActivityMethod
activityAllowed, rememberActivityDecision
} from '../lib/rotur/extension-bridge.js';
import {
authorizeProjectCall, grantsSilently, validateProjectScopes
} from '../lib/rotur/project-methods.js';
import {getRoturSettings, setRoturSetting} from '../lib/rotur/settings.js';
import {isLoggedIn} from '../lib/rotur/client.js';
import {getState as getRoturIdentityState} from '../lib/rotur/identity.js';
Expand All @@ -19,13 +22,15 @@ import {
// Attaches a Rotur "host" onto vm.runtime so builtin Rotur extensions can act as
// the logged-in user without ever seeing the token. The token stays inside the
// GUI's Rotur client (lib/rotur/client.js); this host only exposes identity,
// per-project consent, and gated calls. Mirrors tw-security-manager's modal-lock.
// per-project consent, and allowlisted calls (lib/rotur/project-methods.js).
// The host, not the caller, decides which calls need a grant or a confirmation.
// Mirrors tw-security-manager's modal-lock.
class RoturExtensionHost extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'handleAllowed', 'handleBlocked', 'handleDenied', 'handleShareThis', 'handleShareAll', 'handleShareNo',
'getUser', 'ensureConsent', 'ensureActivitySharing', 'call', 'getProjectId'
'getUser', 'ensureConsent', 'ensureActivitySharing', 'call', 'getProjectId', 'grantMeta'
]);
this.nextModalCallbacks = [];
this.modalLocked = false;
Expand All @@ -46,7 +51,7 @@ class RoturExtensionHost extends React.Component {
const id = this.getProjectId();
return id ? `https://api.mistwarp.org/thumbnails/${encodeURIComponent(id)}.png` : '';
},
grantedScopes: () => grantedScopesFor({name: this.props.vm.runtime.projectName || ''})
grantedScopes: () => grantedScopesFor(this.grantMeta())
};
}

Expand Down Expand Up @@ -192,14 +197,23 @@ class RoturExtensionHost extends React.Component {
return this.currentUser();
}

async ensureConsent (scopes, meta) {
grantMeta () {
return {name: this.props.vm.runtime.projectName || ''};
}

async ensureConsent (requested) {
const scopes = validateProjectScopes(requested);
if (!scopes) {
throw new Error('Projects cannot ask for that Rotur permission');
}
if (!this.currentUser().loggedIn) {
throw new Error('Log in to Rotur to let this project connect');
}
const meta = this.grantMeta();
if (hasFullGrant(meta, scopes)) {
return true;
}
if (meta && meta.authenticatedOnly) {
if (grantsSilently(scopes)) {
await commitGrant(meta, scopes);
return true;
}
Expand All @@ -220,25 +234,31 @@ class RoturExtensionHost extends React.Component {
return true;
}

async call (method, args, opts) {
if (isActivityMethod(method)) {
async call (method, args) {
const call = authorizeProjectCall(
method,
args,
grantedScopesFor(this.grantMeta()),
this.getProjectId() || 'mistwarp'
);
if (call.spec.activity) {
const allowed = await this.ensureActivitySharing();
if (!allowed) {
return '';
}
}
if (opts && opts.sensitive) {
if (call.confirm) {
const {showModal} = await this.acquireModalLock();
const confirmed = await showModal('confirm', {
label: opts.label || method,
confirmation: opts.confirmation || null,
label: call.confirm.label,
confirmation: call.confirm.confirmation || null,
username: this.currentUser().username
});
if (!confirmed) {
throw new Error('You cancelled this Rotur action');
}
}
return this.activityScope.call(method, args);
return this.activityScope.call(method, call.args);
}

render () {
Expand Down
41 changes: 13 additions & 28 deletions src/lib/rotur/extension-bridge.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {getRotur, ensureScopes} from './client.js';
import {invokeProjectMethod} from './project-methods.js';

const GRANTS_KEY = 'mw:rotur-grants';

// Per-project consent grants: {[projectKey]: string[] scopes}. The key is the
// platform project id when known, else a name-based fallback.
// platform project id when known, else a name-based fallback. Hosts that know
// the project (the community project page) pass meta.projectId themselves, so
// the project cannot pick which grant it reads.
const readGrants = () => {
try {
return JSON.parse(localStorage.getItem(GRANTS_KEY) || '{}') || {};
Expand All @@ -21,6 +24,9 @@ const writeGrants = grants => {
};

const projectKey = meta => {
if (meta && meta.projectId) {
return `id:${meta.projectId}`;
}
try {
const stored = sessionStorage.getItem('mw:mistwarp-current-project');
if (stored) {
Expand Down Expand Up @@ -104,36 +110,15 @@ const isActivityMethod = method => (
method === 'socket.removeActivity'
);

// Dispatch a dotted method path (e.g. "me.transfer", "posts.feed",
// "socket.setStatus") on the shared Rotur client. The token lives inside the
// client and is never returned. socket.* methods lazily connect the status
// websocket first.
const callRotur = async (method, args) => {
// Run an allowlisted Rotur method (see project-methods.js) on the shared
// client. The token lives inside the client and is never returned. Anything
// outside the allowlist, such as "_http.getToken", is rejected.
const callRotur = (method, args) => {
const rotur = getRotur();
if (!rotur.loggedIn) {
throw new Error('Log in to Rotur to use this block');
}
const parts = method.split('.');
if (parts[0] === 'socket') {
if (!rotur.socket || !rotur.socket.connected) {
await rotur.connectSocket();
}
const fn = rotur.socket && rotur.socket[parts[1]];
if (typeof fn !== 'function') {
throw new Error(`Unknown Rotur socket method: ${method}`);
}
return fn.apply(rotur.socket, args);
}
let ctx = rotur;
let fn = rotur;
for (const part of parts) {
ctx = fn;
fn = fn && fn[part];
}
if (typeof fn !== 'function') {
throw new Error(`Unknown Rotur method: ${method}`);
return Promise.reject(new Error('Log in to Rotur to use this block'));
}
return fn.apply(ctx, args || []);
return invokeProjectMethod(rotur, method, args || []);
};

export {
Expand Down
Loading
Loading