Skip to content

Commit

Permalink
Make it os the first time a fetch happens between a remote seed packe…
Browse files Browse the repository at this point in the history
…t and a domain it asks the user if they want to allow it.

Part of #45.
  • Loading branch information
jkomoros committed Jul 22, 2023
1 parent 609d225 commit 0afb4a9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/grow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {

import {
isLocalLocation,
locationDomain,
makeAbsolute,
packSeedReference,
unpackSeedReference
Expand Down Expand Up @@ -432,6 +433,12 @@ const growFetch = async (seed : Seed<SeedDataFetch>, env : Environment) : Promis
let body = null;
if (method != 'GET') body = extractString(await getProperty(seed, env, data.body, ''));

if (!isLocalLocation(seed.location)) {
const domain = locationDomain(resource);
const allowFetch = await seed.garden.profile.allowFetch(seed.location, domain);
if (!allowFetch) throw new Error(`User did not allow fetch from ${seed.location} to ${domain}`);
}

if (env.getKnownProtectedKey('mock')) {
const data = {
resource,
Expand Down
24 changes: 23 additions & 1 deletion src/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import {
EmbeddingModelID,
LeafValue,
MemoryID,
SeedPacketAbsoluteRemoteLocation,
StoreID,
StoreKey,
StoreValue
StoreValue,
URLDomain
} from './types.js';

//When changing this also change environment.SAMPLE.json
Expand Down Expand Up @@ -64,6 +66,12 @@ export class Profile{

_garden : Garden | undefined;

_allowedFetches : {
[packet : SeedPacketAbsoluteRemoteLocation]: {
[resourceDomain : string] : true
}
};

_memories : {
[id : MemoryID]: {
embeddings: Embedding[]
Expand All @@ -80,6 +88,7 @@ export class Profile{
constructor() {
this._memories = {};
this._stores = {};
this._allowedFetches = {};
}

set garden(val : Garden) {
Expand All @@ -99,6 +108,19 @@ export class Profile{
console.log(message, ...optionalParams);
}

//Whether to allow fetch of a given location.
async allowFetch(remotePacketLocation : SeedPacketAbsoluteRemoteLocation, domain : URLDomain) : Promise<boolean> {
if (this._allowedFetches[remotePacketLocation]) {
if (this._allowedFetches[remotePacketLocation][domain]) return true;
}
const question = `Do you want to allow a seed in ${remotePacketLocation} to fetch from domain ${domain}?`;
if (!confirm(question)) return false;
if (!confirm(`Do you want to save the choice to allow ${remotePacketLocation} to fetch from domain ${domain}?`)) return true;
if (!this._allowedFetches[remotePacketLocation]) this._allowedFetches[remotePacketLocation] = {};
this._allowedFetches[remotePacketLocation][domain] = true;
return true;
}

async localFetch(_location : string) : Promise<string> {
throw new Error('localFetch is not supported on this profile type');
}
Expand Down

0 comments on commit 0afb4a9

Please sign in to comment.