Skip to content
Open
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
15 changes: 15 additions & 0 deletions pxtlib/browserutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,21 @@ namespace pxt.BrowserUtils {
return url;
}

export function getCopilotServerParam(): "ppe" | "prod" | undefined {
if (typeof window === "undefined") return undefined;
const query = pxt.Util.parseQueryString(window.location.search || "");
const value = (query["useCopilotServer"] || "").toLowerCase();
return value === "ppe" || value === "prod" ? value : undefined;
}

export function appendCopilotServerQueryParam(url: string): string {
const value = getCopilotServerParam();
if (!value) return url;
const params = new URLSearchParams();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering for extra security if we should check if the user is logged in at this point? Like if there's no login, don't append the query param?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any security for backend endpoints on the front end is fake; a malicious user will just curl or make the request themselves in the dev tools anyways. no harm in the query parameter existing as the back end is the one that determines whether it is a valid request or not.

(besides that all of our copilot integrations are gated on auth already)

params.set("useCopilotServer", value);
return appendUrlQueryParams(url, params);
}

export function legacyCopyText(element: HTMLInputElement | HTMLTextAreaElement) {
element.focus();
element.setSelectionRange(0, 9999);
Expand Down
2 changes: 1 addition & 1 deletion teachertool/src/services/backendRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export async function loadTestableCollectionFromDocsAsync<T>(fileNames: string[]
}

export async function askCopilotQuestionAsync(shareId: string, question: string): Promise<string | undefined> {
const url = `/api/copilot/question`;
const url = pxt.BrowserUtils.appendCopilotServerQueryParam(`/api/copilot/question`);

question = pxt.Util.cleanData(question);

Expand Down
4 changes: 2 additions & 2 deletions webapp/src/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,8 @@ export async function aiErrorExplainRequest(
outputFormat: "tour_json" | "text",
locale: string
): Promise<string | undefined> {
const startUrl = `/api/copilot/startexplainerror`;
const statusUrl = `/api/copilot/explainerrorstatus`;
const startUrl = pxt.BrowserUtils.appendCopilotServerQueryParam(`/api/copilot/startexplainerror`);
const statusUrl = pxt.BrowserUtils.appendCopilotServerQueryParam(`/api/copilot/explainerrorstatus`);

const data = { lang, code, errors, target, outputFormat, locale };

Expand Down
Loading