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: 23 additions & 20 deletions apps/sim/app/api/files/public/[token]/content/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
authorizePublicFileShare,
checkPublicFileShareContent,
readPublicFileShareContent,
retryOnSharedFileRevisionChange,
} from '@/lib/public-shares/application'
import { enforcePublicFileRateLimit } from '@/lib/public-shares/rate-limit'
import { FILE_CACHE_CONTROL } from '@/lib/uploads/server/delivery'
Expand All @@ -39,34 +40,36 @@ export const GET = withRouteHandler(
})
if (!auth.authorized) return publicFileAuthDenied(auth)
if (request.method === 'HEAD') {
await checkPublicFileShareContent(auth.grant)
await retryOnSharedFileRevisionChange(() => checkPublicFileShareContent(auth.grant))
return new NextResponse(null, {
headers: {
'Cache-Control': FILE_CACHE_CONTROL.revalidate,
'X-Content-Type-Options': 'nosniff',
},
})
}
const result = await readPublicFileShareContent({
grant: auth.grant,
preview: parsed.data.query.preview === '1',
request,
})
if (result.kind === 'stream') {
return await createStoredFileStreamResponse(
{ ...result, cacheControl: FILE_CACHE_CONTROL.revalidate },
request.headers
return await retryOnSharedFileRevisionChange(async () => {
const result = await readPublicFileShareContent({
grant: auth.grant,
preview: parsed.data.query.preview === '1',
request,
})
if (result.kind === 'stream') {
return await createStoredFileStreamResponse(
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved
{ ...result, cacheControl: FILE_CACHE_CONTROL.revalidate },
request.headers
)
}
return createConditionalFileResponse(
{
buffer: result.buffer,
contentType: result.contentType,
filename: result.file.originalName,
cacheControl: FILE_CACHE_CONTROL.revalidate,
},
request.headers.get('if-none-match')
)
}
return createConditionalFileResponse(
{
buffer: result.buffer,
contentType: result.contentType,
filename: result.file.originalName,
cacheControl: FILE_CACHE_CONTROL.revalidate,
},
request.headers.get('if-none-match')
)
})
} catch (error) {
logPublicFileError(logger, 'Error serving public shared file:', error)
return publicFileBinaryErrorResponse(error, 'Failed to serve file')
Expand Down
9 changes: 4 additions & 5 deletions apps/sim/app/api/files/public/[token]/inline/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import {
authorizePublicFileShare,
readPublicFileShareInline,
retryOnSharedFileRevisionChange,
} from '@/lib/public-shares/application'
import { enforcePublicFileRateLimit } from '@/lib/public-shares/rate-limit'
import { FILE_CACHE_CONTROL } from '@/lib/uploads/server/delivery'
Expand All @@ -34,11 +35,9 @@ export const GET = withRouteHandler(
credential: await publicFileShareCredential(request.cookies.getAll(), getClientIp(request)),
})
if (!auth.authorized) return publicFileAuthDenied(auth)
const result = await readPublicFileShareInline({
grant: auth.grant,
...parsed.data.query,
request,
})
const result = await retryOnSharedFileRevisionChange(() =>
readPublicFileShareInline({ grant: auth.grant, ...parsed.data.query, request })
)
return createConditionalFileResponse(
{
buffer: result.buffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,90 @@ describe('Public file shares against PostgreSQL and private storage', () => {
}
)

check(
'public share content saved during an object read is delivered from a fresh snapshot, never mixed',
async () => {
const { updateProjectFileShare } = await import('@/lib/projects/files/application/shares')
const {
authorizePublicFileShare,
readPublicFileShareContent,
retryOnSharedFileRevisionChange,
} = await import('@/lib/public-shares/application')
// A Markdown file streams from storage; a page is rendered whole. Both see the save.
const saved = 'Saved while read'
const shared = {
streamed: { input: createInput, source: '' },
rendered: {
input: (projectId: string) => ({
...createInput(projectId, '---\ntitle: Architecture\n---\n\nShared page'),
name: 'Architecture',
contentType: 'text/x-sim-page',
}),
source: '---\ntitle: Architecture\n---\n\n',
},
}
for (const { input, source } of Object.values(shared))
for (const saves of ['once', 'every read'] as const) {
const f = await fixture()
const { file } = await createProjectFile.execute({
principal: f.principal,
input: input(f.projectId),
})
const { share } = await updateProjectFileShare.execute({
principal: f.principal,
input: { projectId: f.projectId, fileId: file.id, isActive: true },
})
const access = await authorizePublicFileShare({
token: share.token,
credential: { method: 'GET' },
})
if (!access.authorized) throw new Error('Public fixture rejected')
// A collaborative save commits while the object is being read, buffered or streamed alike.
let written = 0
let saving = false
const save = async () => {
if (saving || (saves === 'once' && written > 0)) return
saving = true
written++
try {
await updateProjectFileContent.execute({
principal: f.principal,
input: {
projectId: f.projectId,
fileId: file.id,
content: `${source}${saved} ${written}`,
encoding: 'utf-8',
},
})
} finally {
saving = false
}
}
const download = storage.downloadFile
const downloadStream = storage.downloadFileStream
const spies = [
vi.spyOn(storage, 'downloadFile').mockImplementation(async (options) => {
const result = await download(options)
await save()
return result
}),
vi.spyOn(storage, 'downloadFileStream').mockImplementation(async (options) => {
const result = await downloadStream(options)
await save()
return result
}),
]
const delivered = retryOnSharedFileRevisionChange(() =>
sharedBytes(readPublicFileShareContent({ grant: access.grant }))
)
if (saves === 'once') expect((await delivered).toString()).toContain(`${saved} 1`)
else await expect(delivered).rejects.toMatchObject({ code: 'conflict' })
expect(written).toBe(saves === 'once' ? 1 : 3)
for (const spy of spies) spy.mockRestore()
}
}
)

check(
'public share grants cannot be cloned or reused after Project archive and preserve workspace links',
async () => {
Expand Down
1 change: 1 addition & 0 deletions apps/sim/lib/public-shares/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export {
readPublicFileShare,
readPublicFileShareContent,
readPublicFileShareInline,
retryOnSharedFileRevisionChange,
} from './read'
30 changes: 29 additions & 1 deletion apps/sim/lib/public-shares/application/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,41 @@ function renderSharedPage(source: string, snapshot: PublicFileShareSnapshot) {
throw error
}
}
/** A content write (a collaborative save, an upload) committed between a read's snapshot and its delivery. */
class SharedFileRevisionChangedError extends OrchestrationError {
constructor() {
super('conflict', 'Shared file changed during the read')
}
}
const MAX_SHARED_FILE_READ_ATTEMPTS = 3
function requireRevision(current: WorkspaceFileRow, expected: WorkspaceFileRow) {
if (
current.id !== expected.id ||
current.key !== expected.key ||
current.contentUpdatedAt.getTime() !== expected.contentUpdatedAt.getTime()
)
throw new OrchestrationError('conflict', 'Shared file changed during the read')
throw new SharedFileRevisionChangedError()
}
/**
* Runs one public delivery again from a fresh snapshot when the shared file, or an input it embeds,
* is rewritten mid-read, as a collaborative save of a file being edited does. Every attempt is a
* complete read that still refuses mixed revisions, so this only spares the viewer a benign race; a
* file rewritten on every attempt still refuses. `deliver` must cover the whole delivery, including
* opening a stream, because a stream rechecks the revision on open. Nothing is reported or audited
* for a refused attempt.
*/
export async function retryOnSharedFileRevisionChange<T>(deliver: () => Promise<T>): Promise<T> {
for (let attempt = 1; ; attempt++) {
try {
return await deliver()
} catch (error) {
if (
attempt >= MAX_SHARED_FILE_READ_ATTEMPTS ||
!(asOrchestrationError(error) instanceof SharedFileRevisionChangedError)
)
throw error
}
}
}
function requirePublicBytes(provenance: WorkspaceFileSecretProvenance) {
if (
Expand Down
Loading