Skip to content

Commit

Permalink
fix: add guardrail to prevent fiddle create from overwriting an exist…
Browse files Browse the repository at this point in the history
…ing fiddle
  • Loading branch information
alexkli committed Feb 6, 2025
1 parent d7ad92e commit 5dedaba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/fastly/api/fiddle-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ export class FastlyFiddleApi {
}

async create(fiddle) {
// ensure no id field is present to force creation of a new fiddle
const { id, ...fiddleBody } = fiddle;

const resp = await this.fetch('POST /fiddle', {
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(fiddle),
body: JSON.stringify(fiddleBody),
});
if (!resp.ok) {
throw new Error(`Failed to create fiddle: ${resp.status} - ${await resp.text()}`);
Expand Down
6 changes: 2 additions & 4 deletions src/fastly/fiddle-mgr.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ export class FastlyFiddleManager {
};

// map snippets
const { src } = fiddle;

const subs = service.snippetsBySubroutine();
for (const type in subs) {
fiddle.src[type] = snippetsToFiddleSrc(subs[type]);
Expand All @@ -224,8 +222,8 @@ export class FastlyFiddleManager {

// replace occurrences of F_backend with F_0, F_1, ...
const reg = new RegExp(`\\bF_${backend.name}\\b`, 'g');
for (const type in src) {
src[type] = src[type].replaceAll(reg, `F_origin_${i}`);
for (const type in fiddle.src) {
fiddle.src[type] = fiddle.src[type].replaceAll(reg, `F_origin_${i}`);
}

backendMapping.push(`# F_origin_${i} => F_${backend.name}`);
Expand Down

0 comments on commit 5dedaba

Please sign in to comment.