Skip to content

Fix path traversal vulnerability in init_from_code template file download - #6830

Merged
trangevi merged 3 commits into
trangevi/init-from-codefrom
copilot/sub-pr-6828-again
Feb 20, 2026
Merged

Fix path traversal vulnerability in init_from_code template file download#6830
trangevi merged 3 commits into
trangevi/init-from-codefrom
copilot/sub-pr-6828-again

Conversation

Copilot AI commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

f.Path from the GitHub API tree response was written directly via os.WriteFile with only a weak strings.Contains(entry.Path, "..") guard, which doesn't catch normalized traversal patterns like infra/./../../etc/passwd.

Changes

  • Replace strings.Contains(entry.Path, "..") with filepath.Clean() normalization followed by strings.HasPrefix(cleanPath, "..") and filepath.IsAbs(cleanPath) checks — catches traversal attempts that survive raw string matching
  • Store the cleaned path in templateFileInfo.Path so all downstream uses (os.Stat, download URL construction, os.MkdirAll, os.WriteFile) operate on the normalized value rather than the raw API response string
// Before
if strings.Contains(entry.Path, "..") || filepath.IsAbs(entry.Path) {
    return fmt.Errorf("invalid path in repository tree: %s", entry.Path)
}

// After
cleanPath := filepath.Clean(entry.Path)
if filepath.IsAbs(cleanPath) || strings.HasPrefix(cleanPath, "..") {
    return fmt.Errorf("invalid path in repository tree: %s", entry.Path)
}

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 20, 2026 22:33
…tacks

Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>
Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>
Copilot AI changed the title [WIP] WIP Address feedback on 'init from code' flow implementation Fix path traversal vulnerability in init_from_code template file download Feb 20, 2026
Copilot AI requested a review from trangevi February 20, 2026 22:34
@trangevi
trangevi merged commit f6a8301 into trangevi/init-from-code Feb 20, 2026
13 checks passed
trangevi added a commit that referenced this pull request Feb 24, 2026
* Add support for init from code

Signed-off-by: trangevi <trangevi@microsoft.com>

* Code comment

Signed-off-by: trangevi <trangevi@microsoft.com>

* Addressing some comments

Signed-off-by: trangevi <trangevi@microsoft.com>

* make sure src is provided for local code story for now

Signed-off-by: trangevi <trangevi@microsoft.com>

* some fixes

Signed-off-by: trangevi <trangevi@microsoft.com>

* Add manual defaulting

Signed-off-by: trangevi <trangevi@microsoft.com>

* Refactoring a bunch based on expected experience

Signed-off-by: trangevi <trangevi@microsoft.com>

* broken spinner

Signed-off-by: trangevi <trangevi@microsoft.com>

* Remove env var print

* Add support for --project-id

Signed-off-by: trangevi <trangevi@microsoft.com>

* Missing env var

Signed-off-by: trangevi <trangevi@microsoft.com>

* Address feedback

Signed-off-by: trangevi <trangevi@microsoft.com>

* Switch to public repo

Signed-off-by: trangevi <trangevi@microsoft.com>

* Changes for existing ACR

Signed-off-by: trangevi <trangevi@microsoft.com>

* More for existing ACR support

Signed-off-by: trangevi <trangevi@microsoft.com>

* app insights changes

Signed-off-by: trangevi <trangevi@microsoft.com>

* Point to branch for now

Signed-off-by: trangevi <trangevi@microsoft.com>

* tests

Signed-off-by: trangevi <trangevi@microsoft.com>

* Integrate with azd extension model handling

Signed-off-by: trangevi <trangevi@microsoft.com>

* Move model handling

Signed-off-by: trangevi <trangevi@microsoft.com>

* Update cli/azd/extensions/azure.ai.agents/internal/cmd/init_from_code.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Proper http client handling

Signed-off-by: trangevi <trangevi@microsoft.com>

* Remove custom string matching helpers in favor of strings.Contains (#6829)

* Initial plan

* Replace containsString/searchString with strings.Contains in test file

Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>

* Revert unrelated go.mod/go.sum changes

Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>

* cspell

Signed-off-by: trangevi <trangevi@microsoft.com>

* Fix path traversal vulnerability in init_from_code template file download (#6830)

* Initial plan

* Use filepath.Clean() for path validation to prevent path traversal attacks

Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>

* Revert unrelated go.mod/go.sum changes

Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>

* fix: capture error from loadAiCatalog in selectNewModel (#6831)

* Initial plan

* fix: capture error from loadAiCatalog in selectNewModel

Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>

* fix: revert go.mod/go.sum changes and add user-facing error message for loadAiCatalog failure

Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>

* fix: remove fmt.Printf from loadAiCatalog error path

Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>

* Code review

Signed-off-by: trangevi <trangevi@microsoft.com>

* Put the status check before attempting to use the response

Signed-off-by: trangevi <trangevi@microsoft.com>

* Fix path handling

Signed-off-by: trangevi <trangevi@microsoft.com>

* Remove unused code

Signed-off-by: trangevi <trangevi@microsoft.com>

* cspell

Signed-off-by: trangevi <trangevi@microsoft.com>

* Address PR comment

Signed-off-by: trangevi <trangevi@microsoft.com>

---------

Signed-off-by: trangevi <trangevi@microsoft.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: trangevi <26490000+trangevi@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants